hanneshier
hanneshier

Reputation: 49

Wordpress Docker "Error establishing a database connection"

I have set up a server to host multiple websites according to this tutorial: https://blog.ssdnodes.com/blog/host-multiple-ssl-websites-docker-nginx/

I have also configured a docker-compose.yml for wordpress like they did in example 2 in the same tutorial. But when I open the website, I get an "Error establishing a database connection" Error. I remember doing this a few months back with everything working fine, but I cant remember what I did differently.

This is the error message I receive (multiple times) after typing docker-compose up

wp_test           | MySQL Connection Error: (1045) Access denied for user 'wordpress'@'172.20.0.9' (using password: YES)
wp_test_db        | 2020-02-23T21:00:45.772510Z 11 [Note] Access denied for user 'wordpress'@'172.20.0.9' (using password: YES)

My docker-compose.yml file looks like this:

version: "3"

services:
   db_node_domain:
     image: mysql:5.7
     volumes:
        - db_data:/var/lib/mysql
     restart: always
     environment:
        MYSQL_ROOT_PASSWORD: somewordpress
        MYSQL_DATABASE: wordpress
        MYSQL_USER: wordpress
        MYSQL_PASSWORD: wordpress
     container_name: wp_test_db

   wordpress:
     depends_on:
        - db_node_domain
     image: wordpress:latest
     expose:
        - 80
     restart: always
     environment:
        VIRTUAL_HOST: blog.example.com
        LETSENCRYPT_HOST: blog.example.com
        LETSENCRYPT_EMAIL: [email protected]
        WORDPRESS_DB_HOST: db_node_domain:3306
        WORDPRESS_DB_USER: wordpress
        WORDPRESS_DB_PASSWORD: wordpress
     container_name: wp_test
volumes:
  db_data:

networks:
  default:
    external:
      name: nginx-proxy

I have tried to stop and start the the image, as suggested here: https://www.reddit.com/r/docker/comments/ahzv5d/wordpress_cant_connect_to_mysql/

I have also tried another wordpress docker-config.yml and added the nginx paramaters myselve, but got the same error message https://docs.docker.com/compose/wordpress/

EDIT I got new error messages

wp_test_db        | 2020-02-23T22:09:40.654580Z 0 [Note] mysqld (mysqld 5.7.29) starting as process 1 ...
wp_test_db        | 2020-02-23T22:09:40.659627Z 0 [Note] InnoDB: PUNCH HOLE support available
wp_test_db        | 2020-02-23T22:09:40.659883Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
wp_test_db        | 2020-02-23T22:09:40.659958Z 0 [Note] InnoDB: Uses event mutexes
wp_test_db        | 2020-02-23T22:09:40.660412Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
wp_test_db        | 2020-02-23T22:09:40.660451Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
wp_test_db        | 2020-02-23T22:09:40.660811Z 0 [Note] InnoDB: Using Linux native AIO
wp_test_db        | 2020-02-23T22:09:40.661814Z 0 [Note] InnoDB: Number of pools: 1
wp_test_db        | 2020-02-23T22:09:40.662418Z 0 [Note] InnoDB: Using CPU crc32 instructions
wp_test_db        | 2020-02-23T22:09:40.667522Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
wp_test_db        | 2020-02-23T22:09:40.681674Z 0 [Note] InnoDB: Completed initialization of buffer pool
wp_test_db        | 2020-02-23T22:09:40.687033Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
wp_test_db        | 2020-02-23T22:09:40.701423Z 0 [ERROR] [FATAL] InnoDB: Table flags are 0 in the data dictionary but the flags in file ./ibdata1 are 0x4000!
wp_test_db        | 2020-02-23 22:09:40 0x7f7ce62ee740  InnoDB: Assertion failure in thread 140174414505792 in file ut0ut.cc line 918
wp_test_db        | InnoDB: We intentionally generate a memory trap.
wp_test_db        | InnoDB: Submit a detailed bug report to http://bugs.mysql.com.
wp_test_db        | InnoDB: If you get repeated assertion failures or crashes, even
wp_test_db        | InnoDB: immediately after the mysqld startup, there may be
wp_test_db        | InnoDB: corruption in the InnoDB tablespace. Please refer to
wp_test_db        | InnoDB: http://dev.mysql.com/doc/refman/5.7/en/forcing-innodb-recovery.html
wp_test_db        | InnoDB: about forcing recovery.
wp_test_db        | 22:09:40 UTC - mysqld got signal 6 ;
wp_test_db        | This could be because you hit a bug. It is also possible that this binary
wp_test_db        | or one of the libraries it was linked against is corrupt, improperly built,
wp_test_db        | or misconfigured. This error can also be caused by malfunctioning hardware.
wp_test_db        | Attempting to collect some information that could help diagnose the problem.
wp_test_db        | As this is a crash and something is definitely wrong, the information
wp_test_db        | collection process might fail.
wp_test_db        | 
wp_test_db        | key_buffer_size=8388608
wp_test_db        | read_buffer_size=131072
wp_test_db        | max_used_connections=0
wp_test_db        | max_threads=151
wp_test_db        | thread_count=0
wp_test_db        | connection_count=0
wp_test_db        | It is possible that mysqld could use up to 
wp_test_db        | key_buffer_size + (read_buffer_size + sort_buffer_size)*max_threads = 68196 K  bytes of memory
wp_test_db        | Hope that's ok; if not, decrease some variables in the equation.
wp_test_db        | 
wp_test_db        | Thread pointer: 0x0
wp_test_db        | Attempting backtrace. You can use the following information to find out
wp_test_db        | where mysqld died. If you see no messages after this, something went
wp_test_db        | terribly wrong...
wp_test_db        | stack_bottom = 0 thread_stack 0x40000
wp_test_db        | mysqld(my_print_stacktrace+0x2c)[0x5643574c60cc]
wp_test_db        | mysqld(handle_fatal_signal+0x479)[0x564356de87b9]
wp_test_db        | /lib/x86_64-linux-gnu/libpthread.so.0(+0x110e0)[0x7f7ce5ecb0e0]
wp_test_db        | /lib/x86_64-linux-gnu/libc.so.6(gsignal+0xcf)[0x7f7ce4657fff]
wp_test_db        | /lib/x86_64-linux-gnu/libc.so.6(abort+0x16a)[0x7f7ce465942a]
wp_test_db        | mysqld(+0x699b25)[0x564356dbeb25]
wp_test_db        | mysqld(_ZN2ib5fatalD1Ev+0x12d)[0x56435782ed8d]
wp_test_db        | mysqld(+0x11b68f1)[0x5643578db8f1]
wp_test_db        | mysqld(+0x11b6f28)[0x5643578dbf28]
wp_test_db        | mysqld(_Z6fil_ioRK9IORequestbRK9page_id_tRK11page_size_tmmPvS8_+0x2b0)[0x5643578e5110]
wp_test_db        | mysqld(_Z13buf_read_pageRK9page_id_tRK11page_size_t+0xce)[0x56435789a33e]
wp_test_db        | mysqld(_Z16buf_page_get_genRK9page_id_tRK11page_size_tmP11buf_block_tmPKcmP5mtr_tb+0x4aa)[0x56435786957a]
wp_test_db        | mysqld(_Z31trx_rseg_get_n_undo_tablespacesPm+0x143)[0x56435780d1f3]
wp_test_db        | mysqld(+0x698c99)[0x564356dbdc99]
wp_test_db        | mysqld(_Z34innobase_start_or_create_for_mysqlv+0x2f3d)[0x5643577da06d]
wp_test_db        | mysqld(+0xf80438)[0x5643576a5438]
wp_test_db        | mysqld(_Z24ha_initialize_handlertonP13st_plugin_int+0x4f)[0x564356e390ff]
wp_test_db        | mysqld(+0xb8c0f6)[0x5643572b10f6]
wp_test_db        | mysqld(_Z40plugin_register_builtin_and_init_core_sePiPPc+0x2f0)[0x5643572b4300]
wp_test_db        | mysqld(+0x6bbece)[0x564356de0ece]
wp_test_db        | mysqld(_Z11mysqld_mainiPPc+0xc71)[0x564356de2a71]
wp_test_db        | /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1)[0x7f7ce46452e1]
wp_test_db        | mysqld(_start+0x2a)[0x564356dd8eaa]
wp_test_db        | The manual page at http://dev.mysql.com/doc/mysql/en/crashing.html contains
wp_test_db        | information that should help you find out what is causing the crash.

Upvotes: 4

Views: 21028

Answers (3)

Al-waleed Shihadeh
Al-waleed Shihadeh

Reputation: 2855

You are getting the errors because the WordPress container is trying to connect the MySQL database before it is ready and the user is created. if you give it some time after starting the services the user will be created and the WordPress will be able to access the database. This compose file works for me

version: "3"

services:
   db_node_domain:
     image: mysql:5.7
     volumes:
        - db_data:/var/lib/mysql
     restart: always
     environment:
        MYSQL_ROOT_PASSWORD: somewordpress
        MYSQL_DATABASE: wordpress
        MYSQL_USER: wordpress
        MYSQL_PASSWORD: wordpress
     container_name: wp_test_db

   wordpress:
     depends_on:
        - db_node_domain
     image: wordpress:latest
     expose:
        - 80
     ports:
        - 8090:80
     restart: always
     environment:
        VIRTUAL_HOST: blog.example.com
        LETSENCRYPT_HOST: blog.example.com
        LETSENCRYPT_EMAIL: [email protected]
        WORDPRESS_DB_HOST: db_node_domain:3306
        WORDPRESS_DB_USER: wordpress
        WORDPRESS_DB_PASSWORD: wordpress
     container_name: wp_test
volumes:
  db_data:

networks:
  default:
    external:
      name: nginx-proxy

if you would like to avoid these errors, you need to implement a wait for function as described here https://docs.docker.com/compose/startup-order/.

for the new error that you get, try to remove the volumes used

docker volume ls
docker volume rm db_data

Upvotes: 3

Wenhao Zhao
Wenhao Zhao

Reputation: 21

Execute "docker volume wordpress_db_data" might help.

I encountered the same problem when I created WordPress with docker-compose. Here is some tips for u:

  1. Have u ever failed to start the services of this compose project? Especially the mysql service.
  2. If so, trying removing the persistent volume for mysql might help.

This will help mysql service recreate.

SO, Just run:

docker volume wordpress_db_data

Upvotes: 2

Andy Mardell
Andy Mardell

Reputation: 1191

Your yaml file works fine for me. The only thing I noticed is there's a missing database name variable in wordpress.environment (WORDPRESS_DB_NAME=wordpress) but this defaults to wordpress if not found. I'm pointing this out in case your actual copy has a db name other than wordpress.

This is the compose file which works fine for me:

version: "3"

services:
   db_node_domain:
     image: mysql:5.7
     volumes:
        - db_data:/var/lib/mysql
     restart: always
     environment:
        MYSQL_ROOT_PASSWORD: somewordpress
        MYSQL_DATABASE: wordpress
        MYSQL_USER: wordpress
        MYSQL_PASSWORD: wordpress
     container_name: wp_test_db

   wordpress:
     depends_on:
        - db_node_domain
     image: wordpress:latest
     ports:
        - '8000:80'
     expose:
        - 80
     restart: always
     environment:
        VIRTUAL_HOST: blog.example.com
        LETSENCRYPT_HOST: blog.example.com
        LETSENCRYPT_EMAIL: [email protected]
        WORDPRESS_DB_HOST: db_node_domain:3306
        WORDPRESS_DB_USER: wordpress
        WORDPRESS_DB_PASSWORD: wordpress
        WORDPRESS_DB_NAME: wordpress # THIS WAS MISSING?
     container_name: wp_test
volumes:
  db_data:

networks:
  default:
    external:
      name: nginx-proxy

If you want to start afresh and try again you could run docker-compose down and then docker-compose up. THIS WILL DESTROY THINGS THOUGH so be careful :)

Upvotes: 4

Related Questions