Issue getting docker to access my database properl

2019-08-10 05:37发布

问题:

I'm new to docker all together - but am trying to setup a local test environment to play with some wordpress things.

So I went to the docker site and pulled up a default docker .yml file on how to get it going easily.

I've made just a couple changes, but mostly this is a straight forward document.

version: '3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql2
     restart: always
     ports:
         - "3306:3306"
     environment:
       MYSQL_ROOT_PASSWORD: somerootwordpresspw
       MYSQL_DATABASE: testdatabase
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     volumes:
         - ./WP-TEST/:/var/www/html/
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "80:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
volumes:
    db_data:

When I run docker-compose up with the above .yml file, I see this error:

MySQL "CREATE DATABASE" Error: Access denied for user 'wordpress'@'%' to database 'wordpress'

Which I find odd, because I'm naming the database testdatabase, so why is it trying to create a database named wordpress?

When I connected with SQL Pro, I could see testdatabase, but according to the console it's trying to create wordpress db.

How do I get it to connect to my named DB, instead of constantly failing to create wordpress?

回答1:

So I think I got it.

It was really simple. In my wordpress portion of my .yml file I needed to include WP_DB_NAME: testdatabase

By doing that, it used my named testdatabase to install wordpress to.

Hope this helps people who might stumble across this.

Now the .yml file looks like this:

version: '3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql2
     restart: always
     ports:
         - "3306:3306"
     environment:
       MYSQL_ROOT_PASSWORD: somerootwordpresspw
       MYSQL_DATABASE: testdatabase
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     volumes:
         - ./WP-TEST/:/var/www/html/
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "80:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_NAME: testdatabase
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
volumes:
    db_data: