【问题标题】:WordPress (non-Docker) cannot connect to MySQL (Docker)WordPress(非 Docker)无法连接到 MySQL(Docker)
【发布时间】:2021-01-02 08:38:25
【问题描述】:

实际上我只是将我的数据库移动到 Docker 容器中。

这是我的 MySQL docker-compose 配置:

version: '3.1'

services:

  db:
    image: mysql:5.7
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    volumes:
      - ./data:/var/lib/mysql
    ports:
      - 336:3306
    environment:
      MYSQL_ROOT_PASSWORD: password

WordPress 配置(非 Docker):

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' );

/** MySQL database username */
define( 'DB_USER', 'root' );

/** MySQL database password */
define( 'DB_PASSWORD', 'password' );

/** MySQL hostname */
define( 'DB_HOST', 'localhost:336' );

/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );

/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );

我可以在主机上使用命令mysql -u root -P 336 -h localhost -p 连接到 MySQL,但 WordPress 显示:

警告:mysqli_real_connect(): (HY000/1698): 第 1626 行 /var/www/dev/wp-includes/wp-db.php 中用户 'root'@'localhost' 的访问被拒绝

Access denied for user 'root'@'localhost'

请帮忙,谢谢!

【问题讨论】:

  • 如果将 mysql 端口 docker-compose 更改为 3306:3306 和 define( 'DB_HOST', 'localhost:3306' ); ,你会得到什么?

标签: php mysql wordpress docker


【解决方案1】:

也许您必须创建首先在您的 Wordpress 设置中使用的“wordpress”数据库。您可以直接从 docker-composer 创建一个,也可以使用 mysql cli。

version: '3.1'

services:

  db:
    image: mysql:5.7
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    volumes:
      - ./data:/var/lib/mysql
    ports:
      - 336:3306
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_ROOT_PASSWORD: password

【讨论】:

  • emmm,我已经用docker exec导入了数据库,db wordpress已经存在了……
  • root 用户可以访问它吗?你确定你可以通过 mysql cli 访问它吗?
猜你喜欢
  • 2018-10-02
  • 1970-01-01
  • 1970-01-01
  • 2017-07-05
  • 2019-09-18
  • 1970-01-01
  • 2020-12-30
  • 2020-07-29
  • 2020-02-12
相关资源
最近更新 更多