MySQL Docker image keeps shutting down - mysql

I am trying to build two docker images for the two components of by project. One is a node application and the other is the MySQL DB. I am using a script to create the databases and fill up the tables in the DB docker image because I couldn't create stored procedures using the ORM. My configuration for it is like this:
FROM mysql:8.0.26
ADD initialization.sql /docker-entrypoint-initdb.d
ENV MYSQL_ALLOW_EMPTY_PASSWORD=thisisfine
EXPOSE 3306
When I run docker-compose up --build, the database image shuts down which causes issues with my other image. I am unable to figure out why. This is what is present in the logs:
Creating libraryly_db_1 ... done
Creating libraryly_backend_1 ... done
Attaching to libraryly_db_1, libraryly_backend_1
backend_1 | Waiting for the DB to start...
db_1 | 2021-08-15 17:10:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started.
db_1 | 2021-08-15 17:10:58+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1 | 2021-08-15 17:10:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started.
db_1 | 2021-08-15 17:10:58+00:00 [Note] [Entrypoint]: Initializing database files
db_1 | 2021-08-15T17:10:58.655968Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.26) initializing of server in progress as process 42
db_1 | 2021-08-15T17:10:58.676504Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
db_1 | 2021-08-15T17:10:59.618507Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
db_1 | 2021-08-15T17:11:01.811035Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
db_1 | 2021-08-15T17:11:01.811791Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
db_1 | 2021-08-15T17:11:02.140665Z 6 [Warning] [MY-010453] [Server] root#localhost is created with an empty password ! Please consider switching off
the --initialize-insecure option.
db_1 | 2021-08-15 17:11:07+00:00 [Note] [Entrypoint]: Database files initialized
db_1 | 2021-08-15 17:11:07+00:00 [Note] [Entrypoint]: Starting temporary server
db_1 | 2021-08-15T17:11:07.714680Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.26) starting as process 91
db_1 | 2021-08-15T17:11:07.750557Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
db_1 | 2021-08-15T17:11:08.000431Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
db_1 | 2021-08-15T17:11:08.397505Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
db_1 | 2021-08-15T17:11:08.397776Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
db_1 | 2021-08-15T17:11:08.399964Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
db_1 | 2021-08-15T17:11:08.400702Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now suppor
ted for this channel.
db_1 | 2021-08-15T17:11:08.407431Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is
accessible to all OS users. Consider choosing a different directory.
db_1 | 2021-08-15T17:11:08.463387Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
db_1 | 2021-08-15T17:11:08.463648Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.26' socket: '/var/run/mysq
ld/mysqld.sock' port: 0 MySQL Community Server - GPL.
db_1 | 2021-08-15 17:11:08+00:00 [Note] [Entrypoint]: Temporary server started.
db_1 | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
db_1 | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
db_1 | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
db_1 | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
backend_1 | Operation timed out
backend_1 | Starting the server...
db_1 |
db_1 | 2021-08-15 17:11:15+00:00 [Note] [Entrypoint]: /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/initialization.sql
backend_1 |
backend_1 | > libraryly#1.0.0 start /app
backend_1 | > node app
backend_1 |
db_1 |
db_1 |
db_1 | 2021-08-15 17:11:16+00:00 [Note] [Entrypoint]: Stopping temporary server
db_1 | 2021-08-15T17:11:16.132929Z 11 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.26).
backend_1 | Listening at: http://localhost:5000
backend_1 | ConnectionRefusedError [SequelizeConnectionRefusedError]: connect ECONNREFUSED 172.23.0.2:3306
backend_1 | at ConnectionManager.connect (/app/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:116:17)
backend_1 | at processTicksAndRejections (internal/process/task_queues.js:93:5)
backend_1 | at async ConnectionManager._connect (/app/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:318:24)
backend_1 | at async /app/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:250:32
backend_1 | at async ConnectionManager.getConnection (/app/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:280:7)
backend_1 | at async /app/node_modules/sequelize/lib/sequelize.js:613:26
backend_1 | at async Sequelize.authenticate (/app/node_modules/sequelize/lib/sequelize.js:867:5) {
backend_1 | parent: Error: connect ECONNREFUSED 172.23.0.2:3306
backend_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
backend_1 | errno: -111,
backend_1 | code: 'ECONNREFUSED',
backend_1 | syscall: 'connect',
backend_1 | address: '172.23.0.2',
backend_1 | port: 3306,
backend_1 | fatal: true
I can't figure out what is causing the root user to send a shutdown request. Please help.

in this case You don't need a dockerfile if you're using docker-compose, you can configure your docker-compose file like this
version: "3.7"
services:
mysql:
image: mysql:8.0.17
container_name: localmysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: initialDatabaseName
ports:
- 3306:3306
volumes:
- /your_local/path:/var/lib/mysql
if you want to initialize your db I recommend you to use an ORM what allow you to use database migrations, or run your script from your backend

Related

First startup of docker container mysql is slow

I couldn't find anything online about this.
When I start a docker container with mysql for the first time, it results very slow at startup (it takes up to 5 minutes to start). Then the next few times I start it it takes very little time and everything is normal.
The docker-compose.yml is like this:
version: "3.9"
services:
db:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
env_file:
- .env
environment:
MYSQL_RANDOM_ROOT_PASSWORD: ${MYSQL_RANDOM_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_DATABASE_HOST: ${MYSQL_DATABASE_HOST}
MYSQL_DATABASE_PORT: ${MYSQL_DATABASE_PORT}
ports:
- 3307:${MYSQL_DATABASE_PORT}
volumes:
- mysql_data:/var/lib/mysql
volumes:
mysql_data:
Instead, here are the logs obtained in the first phase of mysql container initialization (on the left you can see the timestamp of the operations):
6-testdjangomysqlnginx-db-1 | 2023-01-28 09:47:07+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.32-1.el8 started.
6-testdjangomysqlnginx-db-1 | 2023-01-28 09:47:07+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
6-testdjangomysqlnginx-db-1 | 2023-01-28 09:47:07+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.32-1.el8 started.
6-testdjangomysqlnginx-db-1 | 2023-01-28 09:47:08+00:00 [Note] [Entrypoint]: Initializing database files
6-testdjangomysqlnginx-db-1 | 2023-01-28T09:47:08.040438Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
6-testdjangomysqlnginx-db-1 | 2023-01-28T09:47:08.040525Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead.
6-testdjangomysqlnginx-db-1 | 2023-01-28T09:47:08.040543Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.32) initializing of server in progress as process 80
6-testdjangomysqlnginx-db-1 | 2023-01-28T09:47:08.089681Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
6-testdjangomysqlnginx-db-1 | 2023-01-28T09:47:25.337581Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
6-testdjangomysqlnginx-db-1 | 2023-01-28T09:48:11.724097Z 6 [Warning] [MY-010453] [Server] root#localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
6-testdjangomysqlnginx-db-1 | 2023-01-28 09:50:11+00:00 [Note] [Entrypoint]: Database files initialized
6-testdjangomysqlnginx-db-1 | 2023-01-28 09:50:11+00:00 [Note] [Entrypoint]: Starting temporary server
6-testdjangomysqlnginx-db-1 | 2023-01-28T09:50:11.764823Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
6-testdjangomysqlnginx-db-1 | 2023-01-28T09:50:11.766216Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead.
6-testdjangomysqlnginx-db-1 | 2023-01-28T09:50:11.766238Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.32) starting as process 125
6-testdjangomysqlnginx-db-1 | 2023-01-28T09:50:12.515012Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
6-testdjangomysqlnginx-db-1 | 2023-01-28T09:50:14.505933Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
6-testdjangomysqlnginx-db-1 | 2023-01-28T09:50:19.710452Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
6-testdjangomysqlnginx-db-1 | 2023-01-28T09:50:19.710543Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
6-testdjangomysqlnginx-db-1 | 2023-01-28T09:50:19.821511Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
6-testdjangomysqlnginx-db-1 | 2023-01-28T09:50:19.837273Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.32' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server - GPL.
6-testdjangomysqlnginx-db-1 | 2023-01-28T09:50:19.837274Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
6-testdjangomysqlnginx-db-1 | 2023-01-28 09:50:19+00:00 [Note] [Entrypoint]: Temporary server started.
6-testdjangomysqlnginx-db-1 | '/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
6-testdjangomysqlnginx-db-1 | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
6-testdjangomysqlnginx-db-1 | Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
6-testdjangomysqlnginx-db-1 | Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
6-testdjangomysqlnginx-db-1 | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
6-testdjangomysqlnginx-db-1 | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
6-testdjangomysqlnginx-db-1 | 2023-01-28 09:50:29+00:00 [Note] [Entrypoint]: GENERATED ROOT PASSWORD: xxxxxxxxxxxxxxxxxxxxxxx
6-testdjangomysqlnginx-db-1 | 2023-01-28 09:50:31+00:00 [Note] [Entrypoint]: Creating database blog
6-testdjangomysqlnginx-db-1 | 2023-01-28 09:50:31+00:00 [Note] [Entrypoint]: Creating user django
6-testdjangomysqlnginx-db-1 | 2023-01-28 09:50:32+00:00 [Note] [Entrypoint]: Giving user django access to schema blog
6-testdjangomysqlnginx-db-1 |
6-testdjangomysqlnginx-db-1 | 2023-01-28 09:50:33+00:00 [Note] [Entrypoint]: Stopping temporary server
6-testdjangomysqlnginx-db-1 | 2023-01-28T09:50:33.252374Z 13 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.32).
It seems that the operation that takes so long is [Entrypoint]: Database files initialized, is it normal?

How to connect to docker-compose mysql after container has initilized?

I have a project that I have been working on and now I am experimenting with docker-compose to build it.
Before running my own custom scripts to create tables and such I thought I would just give it a go and see if it builds and I can connect to it.
I found a suitable docker-compose for mysql:
version: "3.8"
services:
mysql:
image: mysql:latest
ports:
- 3307:3307
environment:
MYSQL_ROOT_PASSWORD: SomeRootPassword1!
MYSQL_USER: someuser
MYSQL_PASSWORD: Password1!
MYSQL_DATABASE: wedding
After running docker-compose-up in gitbash I see:
mysql_1 | 2020-08-26 20:00:14+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
mysql_1 | 2020-08-26 20:00:14+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql_1 | 2020-08-26 20:00:14+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
mysql_1 | 2020-08-26 20:00:14+00:00 [Note] [Entrypoint]: Initializing database files
mysql_1 | 2020-08-26T20:00:14.502438Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.21) initializing of server in progress as process 43
mysql_1 | 2020-08-26T20:00:14.507639Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql_1 | 2020-08-26T20:00:15.519301Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql_1 | 2020-08-26T20:00:17.309017Z 6 [Warning] [MY-010453] [Server] root#localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
mysql_1 | 2020-08-26 20:00:21+00:00 [Note] [Entrypoint]: Database files initialized
mysql_1 | 2020-08-26 20:00:21+00:00 [Note] [Entrypoint]: Starting temporary server
mysql_1 | 2020-08-26T20:00:22.149193Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.21) starting as process 90
mysql_1 | 2020-08-26T20:00:22.170767Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql_1 | 2020-08-26T20:00:22.378726Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql_1 | 2020-08-26T20:00:22.457272Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
mysql_1 | 2020-08-26T20:00:22.549270Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql_1 | 2020-08-26T20:00:22.549394Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
mysql_1 | 2020-08-26T20:00:22.553380Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
mysql_1 | 2020-08-26T20:00:22.564741Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.21' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server - GPL.
mysql_1 | 2020-08-26 20:00:22+00:00 [Note] [Entrypoint]: Temporary server started.
mysql_1 | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
mysql_1 | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
mysql_1 | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
mysql_1 | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
mysql_1 | 2020-08-26 20:00:24+00:00 [Note] [Entrypoint]: Creating database wedding
mysql_1 | 2020-08-26 20:00:24+00:00 [Note] [Entrypoint]: Creating user someuser
mysql_1 | 2020-08-26 20:00:24+00:00 [Note] [Entrypoint]: Giving user someuser access to schema wedding
mysql_1 |
mysql_1 | 2020-08-26 20:00:24+00:00 [Note] [Entrypoint]: Stopping temporary server
mysql_1 | 2020-08-26T20:00:24.697408Z 14 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.21).
mysql_1 | 2020-08-26T20:00:27.792197Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.21) MySQL Community Server - GPL.
mysql_1 | 2020-08-26 20:00:28+00:00 [Note] [Entrypoint]: Temporary server stopped
mysql_1 |
mysql_1 | 2020-08-26 20:00:28+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
mysql_1 |
mysql_1 | 2020-08-26T20:00:28.931847Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.21) starting as process 1
mysql_1 | 2020-08-26T20:00:28.947655Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql_1 | 2020-08-26T20:00:29.162873Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql_1 | 2020-08-26T20:00:29.250374Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
mysql_1 | 2020-08-26T20:00:29.322041Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql_1 | 2020-08-26T20:00:29.322181Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
mysql_1 | 2020-08-26T20:00:29.328435Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
mysql_1 | 2020-08-26T20:00:29.343021Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.21' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
The problem
Now that above is finished(Not sure if all is correct) I thought I would try and connect to it via mysql workbench but Im getting the following:
I also tried changing my binding address to :
What am I doing wrong?
In the compose file, under ports: you creating a forwarding rule from localhost:3307 to container:3307.
While you can choose any source port that is not in use on your host machine, at the other end you must hit the port on which the container is listening: in this case it would be 3306.
Your docker-compose.yml file should look like this:
version: "3.8"
services:
mysql:
image: mysql:latest
ports:
- 3307:3306
environment:
MYSQL_ROOT_PASSWORD: SomeRootPassword1!
MYSQL_USER: someuser
MYSQL_PASSWORD: Password1!
MYSQL_DATABASE: wedding
...then you should be able to connect at localhost:3307
You need to use docker internal network to bind address, instead of localhost (if you are not exposing the port), replacing 0.0.0.0 for host.docker.internal. This should do the trick.

MySQL connection refused from Node on first run only (docker-compose)

I'm trying to build out a NodeJS/MySQL stack, but am experiencing an intermittent issue.
Here is my docker compose file:
version: '3'
services:
db:
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
container_name: 'db'
networks:
- db-network
volumes:
- db-data:/var/lib/mysql
- ./db:/docker-entrypoint-initdb.d
forum:
image: "node:12"
user: "node"
working_dir: /home/node/app
depends_on:
- db
ports:
- 7000:3000
environment:
- NODE_ENV=development
- PORT=3000
volumes:
- ./server:/home/node/app
command: "npm start"
networks:
- db-network
adminer:
image: adminer
restart: always
networks:
- db-network
depends_on:
- db
ports:
- 7777:8080
environment:
ADMINER_DEFAULT_DB_DRIVER: mysql
ADMINER_DEFAULT_DB_HOST: db
volumes:
db-data:
networks:
db-network:
You can see that I've got an SQL script mapped into the initdb entrypoint. This is really simple SQL, it simply creates the db: CREATE DATABASE IF NOT EXISTS forum;
Inside the Node container I'm using knex to talk to the database. When I instantiate knex, I also run any outstanding migrations:
const environment = process.env.ENVIRONMENT || 'development'
const Knex = require('knex');
const knexConfig = require('../knexfile');
const knex = Knex(knexConfig[environment]);
async function init() {
try {
return await knex.migrate.latest();
} catch (error) {
console.log(error);
} finally {
return;
}
}
init();
module.exports = knex;
The problem i'm getting only appears on 'first up' o docker-compose (or more specifically when it needs to create the db-data volume). If I drop the volume docker-compose down -V I get the error when 'up-ing' again. The problem is your classic ECONNREFUSED:
Creating network "forum_db-network" with the default driver
Creating volume "forum_db-data" with default driver
Creating db ... done
Creating forum_adminer_1 ... done
Creating forum_forum_1 ... done
Attaching to db, forum_adminer_1, forum_forum_1
db | Initializing database
db | 2020-02-20T09:50:46.701629Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
db | 2020-02-20T09:50:47.004757Z 0 [Warning] InnoDB: New log files created, LSN=45790
db | 2020-02-20T09:50:47.062521Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
db | 2020-02-20T09:50:47.122917Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 7810f3c2-53c6-11ea-ba00-0242c0a81002.
db | 2020-02-20T09:50:47.126689Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
adminer_1 | [Thu Feb 20 09:50:47 2020] PHP 7.4.2 Development Server (http://[::]:8080) started
db | 2020-02-20T09:50:47.127637Z 1 [Warning] root#localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
db | 2020-02-20T09:50:47.955961Z 1 [Warning] 'user' entry 'root#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:47.956454Z 1 [Warning] 'user' entry 'mysql.session#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:47.957026Z 1 [Warning] 'user' entry 'mysql.sys#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:47.957747Z 1 [Warning] 'db' entry 'performance_schema mysql.session#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:47.958212Z 1 [Warning] 'db' entry 'sys mysql.sys#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:47.958797Z 1 [Warning] 'proxies_priv' entry '# root#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:47.959520Z 1 [Warning] 'tables_priv' entry 'user mysql.session#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:47.960113Z 1 [Warning] 'tables_priv' entry 'sys_config mysql.sys#localhost' ignored in --skip-name-resolve mode.
forum_1 |
forum_1 | > express-oauth-vuex-starter#1.0.0 start /home/node/app
forum_1 | > nodemon index.js
forum_1 |
forum_1 | [nodemon] 2.0.2
forum_1 | [nodemon] to restart at any time, enter `rs`
forum_1 | [nodemon] watching dir(s): *.*
forum_1 | [nodemon] watching extensions: js,mjs,json
forum_1 | [nodemon] starting `node index.js`
db | Database initialized
db | Initializing certificates
db | Generating a RSA private key
db | ........................................................................................................................+++++
db | ..........+++++
db | unable to write 'random state'
db | writing new private key to 'ca-key.pem'
db | -----
db | Generating a RSA private key
db | .........................................................................................+++++
db | .............+++++
db | unable to write 'random state'
db | writing new private key to 'server-key.pem'
db | -----
db | Generating a RSA private key
db | .....................................................+++++
db | .+++++
db | unable to write 'random state'
db | writing new private key to 'client-key.pem'
db | -----
db | Certificates initialized
db | MySQL init process in progress...
db | 2020-02-20T09:50:51.037907Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
db | 2020-02-20T09:50:51.039423Z 0 [Note] mysqld (mysqld 5.7.24) starting as process 89 ...
db | 2020-02-20T09:50:51.044545Z 0 [Note] InnoDB: PUNCH HOLE support available
db | 2020-02-20T09:50:51.044653Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db | 2020-02-20T09:50:51.044701Z 0 [Note] InnoDB: Uses event mutexes
db | 2020-02-20T09:50:51.044745Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
db | 2020-02-20T09:50:51.044786Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
db | 2020-02-20T09:50:51.044825Z 0 [Note] InnoDB: Using Linux native AIO
db | 2020-02-20T09:50:51.045705Z 0 [Note] InnoDB: Number of pools: 1
db | 2020-02-20T09:50:51.046113Z 0 [Note] InnoDB: Using CPU crc32 instructions
db | 2020-02-20T09:50:51.048701Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
db | 2020-02-20T09:50:51.060691Z 0 [Note] InnoDB: Completed initialization of buffer pool
db | 2020-02-20T09:50:51.064263Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
db | 2020-02-20T09:50:51.076676Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
db | 2020-02-20T09:50:51.097380Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
db | 2020-02-20T09:50:51.097533Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
db | 2020-02-20T09:50:51.131620Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
db | 2020-02-20T09:50:51.134052Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
db | 2020-02-20T09:50:51.134142Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
db | 2020-02-20T09:50:51.135177Z 0 [Note] InnoDB: Waiting for purge to start
db | 2020-02-20T09:50:51.185530Z 0 [Note] InnoDB: 5.7.24 started; log sequence number 2591440
db | 2020-02-20T09:50:51.186329Z 0 [Note] Plugin 'FEDERATED' is disabled.
db | 2020-02-20T09:50:51.192075Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
db | 2020-02-20T09:50:51.192591Z 0 [Warning] CA certificate ca.pem is self signed.
db | 2020-02-20T09:50:51.194602Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
db | 2020-02-20T09:50:51.197793Z 0 [Note] InnoDB: Buffer pool(s) load completed at 200220 9:50:51
db | 2020-02-20T09:50:51.198483Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
db | 2020-02-20T09:50:51.200316Z 0 [Warning] 'user' entry 'root#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:51.200460Z 0 [Warning] 'user' entry 'mysql.session#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:51.200532Z 0 [Warning] 'user' entry 'mysql.sys#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:51.200606Z 0 [Warning] 'db' entry 'performance_schema mysql.session#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:51.200656Z 0 [Warning] 'db' entry 'sys mysql.sys#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:51.200717Z 0 [Warning] 'proxies_priv' entry '# root#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:51.202731Z 0 [Warning] 'tables_priv' entry 'user mysql.session#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:51.202816Z 0 [Warning] 'tables_priv' entry 'sys_config mysql.sys#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:51.219832Z 0 [Note] Event Scheduler: Loaded 0 events
db | 2020-02-20T09:50:51.221248Z 0 [Note] mysqld: ready for connections.
db | Version: '5.7.24' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server (GPL)
forum_1 | Listening...
forum_1 | Error: connect ECONNREFUSED 192.168.16.2:3306
forum_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16)
forum_1 | at Protocol._enqueue (/home/node/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
forum_1 | at Protocol.handshake (/home/node/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
forum_1 | at Connection.connect (/home/node/app/node_modules/mysql/lib/Connection.js:116:18)
forum_1 | at /home/node/app/node_modules/knex/lib/dialects/mysql/index.js:69:18
forum_1 | From previous event:
forum_1 | at Client_MySQL.acquireRawConnection (/home/node/app/node_modules/knex/lib/dialects/mysql/index.js:64:12)
forum_1 | at create (/home/node/app/node_modules/knex/lib/client.js:291:39)
forum_1 | at processTicksAndRejections (internal/process/task_queues.js:97:5) {
forum_1 | errno: 'ECONNREFUSED',
forum_1 | code: 'ECONNREFUSED',
forum_1 | syscall: 'connect',
forum_1 | address: '192.168.16.2',
forum_1 | port: 3306,
forum_1 | fatal: true
forum_1 | }
db | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
db | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
db | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
db | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
db | 2020-02-20T09:50:54.889060Z 4 [Warning] 'user' entry 'root#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:54.889116Z 4 [Warning] 'user' entry 'mysql.session#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:54.889137Z 4 [Warning] 'user' entry 'mysql.sys#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:54.889164Z 4 [Warning] 'db' entry 'performance_schema mysql.session#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:54.889172Z 4 [Warning] 'db' entry 'sys mysql.sys#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:54.889188Z 4 [Warning] 'proxies_priv' entry '# root#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:54.890011Z 4 [Warning] 'tables_priv' entry 'user mysql.session#localhost' ignored in --skip-name-resolve mode.
db | 2020-02-20T09:50:54.890057Z 4 [Warning] 'tables_priv' entry 'sys_config mysql.sys#localhost' ignored in --skip-name-resolve mode.
db |
db | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
db | mysql: [Warning] Using a password on the command line interface can be insecure.
db |
db |
db | 2020-02-20T09:50:54.915362Z 0 [Note] Giving 0 client threads a chance to die gracefully
db | 2020-02-20T09:50:54.915705Z 0 [Note] Shutting down slave threads
db | 2020-02-20T09:50:54.916456Z 0 [Note] Forcefully disconnecting 0 remaining clients
db | 2020-02-20T09:50:54.917013Z 0 [Note] Event Scheduler: Purging the queue. 0 events
db | 2020-02-20T09:50:54.917696Z 0 [Note] Binlog end
db | 2020-02-20T09:50:54.918665Z 0 [Note] Shutting down plugin 'ngram'
db | 2020-02-20T09:50:54.919138Z 0 [Note] Shutting down plugin 'partition'
db | 2020-02-20T09:50:54.919357Z 0 [Note] Shutting down plugin 'BLACKHOLE'
db | 2020-02-20T09:50:54.920107Z 0 [Note] Shutting down plugin 'ARCHIVE'
db | 2020-02-20T09:50:54.920322Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
db | 2020-02-20T09:50:54.921325Z 0 [Note] Shutting down plugin 'MRG_MYISAM'
db | 2020-02-20T09:50:54.921840Z 0 [Note] Shutting down plugin 'MyISAM'
db | 2020-02-20T09:50:54.922751Z 0 [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL'
db | 2020-02-20T09:50:54.923300Z 0 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
db | 2020-02-20T09:50:54.923360Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
db | 2020-02-20T09:50:54.923589Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
db | 2020-02-20T09:50:54.923959Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
db | 2020-02-20T09:50:54.924112Z 0 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
db | 2020-02-20T09:50:54.924269Z 0 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
db | 2020-02-20T09:50:54.924754Z 0 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
db | 2020-02-20T09:50:54.925454Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
db | 2020-02-20T09:50:54.925535Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
db | 2020-02-20T09:50:54.925688Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
db | 2020-02-20T09:50:54.925728Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
db | 2020-02-20T09:50:54.926261Z 0 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
db | 2020-02-20T09:50:54.926443Z 0 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
db | 2020-02-20T09:50:54.926836Z 0 [Note] Shutting down plugin 'INNODB_FT_DELETED'
db | 2020-02-20T09:50:54.927513Z 0 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
db | 2020-02-20T09:50:54.927560Z 0 [Note] Shutting down plugin 'INNODB_METRICS'
db | 2020-02-20T09:50:54.927718Z 0 [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO'
db | 2020-02-20T09:50:54.927806Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
db | 2020-02-20T09:50:54.927847Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
db | 2020-02-20T09:50:54.928201Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
db | 2020-02-20T09:50:54.928360Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
db | 2020-02-20T09:50:54.928817Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
db | 2020-02-20T09:50:54.929031Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
db | 2020-02-20T09:50:54.929717Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM'
db | 2020-02-20T09:50:54.929740Z 0 [Note] Shutting down plugin 'INNODB_CMP_RESET'
db | 2020-02-20T09:50:54.929898Z 0 [Note] Shutting down plugin 'INNODB_CMP'
db | 2020-02-20T09:50:54.929912Z 0 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
db | 2020-02-20T09:50:54.930509Z 0 [Note] Shutting down plugin 'INNODB_LOCKS'
db | 2020-02-20T09:50:54.930535Z 0 [Note] Shutting down plugin 'INNODB_TRX'
db | 2020-02-20T09:50:54.930549Z 0 [Note] Shutting down plugin 'InnoDB'
db | 2020-02-20T09:50:54.931131Z 0 [Note] InnoDB: FTS optimize thread exiting.
db | 2020-02-20T09:50:54.931746Z 0 [Note] InnoDB: Starting shutdown...
db | 2020-02-20T09:50:55.032188Z 0 [Note] InnoDB: Dumping buffer pool(s) to /var/lib/mysql/ib_buffer_pool
db | 2020-02-20T09:50:55.032448Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 200220 9:50:55
If the volume already exists, I get no errors, knex connects and the migrations are run.
In the error log you can see that the Node container starts up directly after mysqld: ready for connections. However, you can see that after the Node error, the db container then runs init.sql. Presumably this means that knex is trying to connect to a specific database before it exists, which I guess could be the cause of the refused connection.
So I guess I'm looking for a way to defer the instantiation of the node container until after the MySQL container has run init.sql. I'm already using the depends_on docker compose property - are there any other docker options available to me?
There's no native solution that I'm aware of, I'm afraid.
For waiting for a service to become available in general, I've used wait-for-it, but in your case you'd need something to verify MySQL also contains the database your app requires.
Maybe a simple Node script that loops a connection and attempts to select the database before running your app proper.
In the end I solved this by setting the MYSQL_DATABASE environment variable for the MySQL container. From the MySQL image docs:
MYSQL_DATABASE
This variable is optional and allows you to specify the
name of a database to be created on image startup. If a user/password
was supplied (see below) then that user will be granted superuser
access (corresponding to GRANT ALL) to this database
db:
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: forum
MYSQL_ROOT_PASSWORD: root
container_name: 'db'
networks:
- db-network
volumes:
- db-data:/var/lib/mysql
In addition to this, I used is-port-reachable to hold initial migrations until the MySQL container is available.
const isPortReachable = require('is-port-reachable');
const environment = process.env.ENVIRONMENT || 'development'
const Knex = require('knex');
const knexConfig = require('../knexfile');
const knex = Knex(knexConfig[environment]);
async function init() {
let dbStatus = false;
while (!dbStatus) {
dbStatus = await isPortReachable(knexConfig[environment].connection.port, {host: knexConfig[environment].connection.host});
}
try {
return await knex.migrate.latest();
} catch (error) {
console.log(error);
}
}
init();
module.exports = knex;
Update
While the above did solve this specific problem, there was a raft of other issues that kept popping up - all to do with MySQL configuration. In the end I got tired of playing whack-a-mole and switched to Postgres (incredibly simple thanks to Knex). Span the image up and everything just worked...
If you are having issues with docker-compose, knex and mysql, my advice would be to just use Postgres!

Why i'm can start container with volumes param

My problem is that when I start the images without specifying the volumes parameter, everything is fine. But when I specify the container cannot start with the following error ...
This is my docker-compose:
version: '3.7'
# Use root/example as user/password credentials
services:
db:
image: mysql:8.0
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123zemtsov321
volumes:
- ./docker/database/mysql01:/var/lib/mysql
adminer:
image: adminer
restart: always
ports:
- 6080:8080
db_1 | mysqld: Table 'mysql.plugin' doesn't exist
db_1 | 2019-09-05T10:16:32.860733Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
db_1 | 2019-09-05T10:16:32.861130Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17) starting as process 1
db_1 | 2019-09-05T10:16:34.628973Z 0 [ERROR] [MY-010735] [Server] Could not open the mysql.plugin table. Please perform the MySQL upgrade procedure.
db_1 | 2019-09-05T10:16:34.737017Z 0 [Warning] [MY-010015] [Repl] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
db_1 | 2019-09-05T10:16:34.788763Z 0 [Warning] [MY-000054] [Server] World-writable config file './auto.cnf' is ignored.
db_1 | 2019-09-05T10:16:34.789050Z 0 [Warning] [MY-010107] [Server] World-writable config file './auto.cnf' has been removed.
db_1 | 2019-09-05T10:16:34.789371Z 0 [Warning] [MY-010075] [Server] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 3d261de8-cfc6-11e9-a284-0242ac120003.
db_1 | 2019-09-05T10:16:34.922344Z 0 [Warning] [MY-010015] [Repl] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
db_1 | 2019-09-05T10:16:34.958036Z 0 [Warning] [MY-010069] [Server] Failed to set up SSL because of the following SSL library error: SSL context is not usable without certificate and private key
db_1 | 2019-09-05T10:16:34.958975Z 0 [Warning] [MY-010284] [Server] RSA public key file not found: /var/lib/mysql//public_key.pem. Some authentication plugins will not work.
db_1 | 2019-09-05T10:16:34.959371Z 0 [Warning] [MY-010284] [Server] RSA public key file not found: /var/lib/mysql//public_key.pem. Some authentication plugins will not work.
db_1 | 2019-09-05T10:16:34.964792Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
db_1 | 2019-09-05T10:16:34.965651Z 0 [Warning] [MY-010441] [Server] Failed to open optimizer cost constant tables
db_1 | 2019-09-05T10:16:34.966395Z 0 [ERROR] [MY-013129] [Server] A message intended for a client cannot be sent there as no client-session is attached. Therefore, we're sending the information to the error-log instead: MY-001146 - Table 'mysql.component' doesn't exist
db_1 | 2019-09-05T10:16:34.966438Z 0 [Warning] [MY-013129] [Server] A message intended for a client cannot be sent there as no client-session is attached. Therefore, we're sending the information to the error-log instead: MY-003543 - The mysql.component table is missing or has an incorrect definition.
db_1 | 2019-09-05T10:16:34.968178Z 0 [ERROR] [MY-010326] [Server] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
db_1 | 2019-09-05T10:16:34.968332Z 0 [ERROR] [MY-010952] [Server] The privilege system failed to initialize correctly. For complete instructions on how to upgrade MySQL to a new version please see the 'Upgrading MySQL' section from the MySQL manual.
db_1 | 2019-09-05T10:16:34.969207Z 0 [ERROR] [MY-010119] [Server] Aborting
db_1 | 2019-09-05T10:16:35.815514Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.17) MySQL Community Server - GPL.
work_db_1 exited with code 1
Why i'm get this is errors?????
It looks like it might be caused by whatever's currently in ./docker/database/mysql01 when you start it up. I'd start troubleshooting by mounting the to a different location on your filesystem (to see if a fresh start resolves the issue), or dig into your existing contents to root out what's tripping it up. This post (while rather old) looks like it solves something similar to the errors you're seeing in your situation:
https://ma.ttias.be/mysql-table-mysql-plugin-doesnt-exist-after-mysql-upgrade/

Error connecting to MariaDB using Docker-Compose, "Connection Lost: The server closed the connection"

I'm trying to connect the official MariaDB docker image to an image of my client-side application with the command docker-compose up; it successfully connects to the server but when trying to connect to the MariaDB database, after about a minute logs "Error: Connection lost: The server closed the connection." I have no problem connecting to the database locally when starting the server, but it throws the error when trying to link docker images of the same code.
My docker-compose.yml:
version: '3.1'
services:
webapp:
image: client
ports:
- "3306:3306"
links:
- db
depends_on:
- db
environment:
DATABASE_URL: "mysql://root:root#db/yelp"
db:
image: mariadb:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: yelp
adminer:
image: adminer
restart: always
ports:
- 3001:3001
Dockerfile:
FROM node:10
WORKDIR /cadenza/documents/SDC/leaveReview
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3306
CMD ["npm", "start"]
mysql/MariaDB index.js connection file:
var mysql = require('mysql')
var connection = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: 'root',
database: 'yelp'
});
connection.connect((err) => {
if (err) {
console.log('error when connecting to db', err);
} else {
console.log('connected to db')
}
});
module.exports = connection;
Logs when running docker-compose up:
docker-compose up
Starting leavereview_db_1 ... done
Starting leavereview_adminer_1 ... done
Starting leavereview_webapp_1 ... done
Attaching to leavereview_adminer_1, leavereview_db_1, leavereview_webapp_1
adminer_1 | PHP 7.2.7 Development Server started at Tue Jul 3 02:34:03 2018
webapp_1 |
webapp_1 | > yelp-reviews#1.0.0 start /cadenza/documents/SDC/leaveReview
webapp_1 | > nodemon server/index.js
webapp_1 |
db_1 | 2018-07-03 2:34:04 0 [Note] mysqld (mysqld 10.3.7-MariaDB-1:10.3.7+maria~jessie) starting as process 1 ...
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Using Linux native AIO
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Uses event mutexes
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Compressed tables use zlib 1.2.8
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Number of pools: 1
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Using SSE2 crc32 instructions
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Completed initialization of buffer pool
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Starting crash recovery from checkpoint LSN=1630896
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Creating shared tablespace for temporary tables
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Waiting for purge to start
webapp_1 | [nodemon] 1.17.5
webapp_1 | [nodemon] to restart at any time, enter `rs`
webapp_1 | [nodemon] watching: *.*
webapp_1 | [nodemon] starting `node server/index.js`
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: 10.3.7 started; log sequence number 1630905; transaction id 21
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
db_1 | 2018-07-03 2:34:04 0 [Note] Plugin 'FEEDBACK' is disabled.
db_1 | 2018-07-03 2:34:04 0 [Note] Recovering after a crash using tc.log
db_1 | 2018-07-03 2:34:04 0 [Note] Starting crash recovery...
db_1 | 2018-07-03 2:34:04 0 [Note] Crash recovery finished.
db_1 | 2018-07-03 2:34:04 0 [Note] Server socket created on IP: '::'.
db_1 | 2018-07-03 2:34:04 0 [Note] InnoDB: Buffer pool(s) load completed at 180703 2:34:04
db_1 | 2018-07-03 2:34:04 0 [Warning] 'proxies_priv' entry '#% root#a1a244ac54cf' ignored in --skip-name-resolve mode.
db_1 | 2018-07-03 2:34:04 0 [Note] Reading of all Master_info entries succeded
db_1 | 2018-07-03 2:34:04 0 [Note] Added new Master_info '' to hash table
db_1 | 2018-07-03 2:34:04 0 [Note] mysqld: ready for connections.
db_1 | Version: '10.3.7-MariaDB-1:10.3.7+maria~jessie' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
webapp_1 | Listening on port 3306
webapp_1 | error when connecting to db { Error: Connection lost: The server closed the connection.
webapp_1 | at Protocol.end (/cadenza/documents/SDC/leaveReview/node_modules/mysql/lib/protocol/Protocol.js:113:13)
webapp_1 | at Socket.<anonymous> (/cadenza/documents/SDC/leaveReview/node_modules/mysql/lib/Connection.js:109:28)
webapp_1 | at Socket.emit (events.js:187:15)
webapp_1 | at endReadableNT (_stream_readable.js:1081:12)
webapp_1 | at process._tickCallback (internal/process/next_tick.js:63:19)
webapp_1 | --------------------
webapp_1 | at Protocol._enqueue (/cadenza/documents/SDC/leaveReview/node_modules/mysql/lib/protocol/Protocol.js:145:48)
webapp_1 | at Protocol.handshake (/cadenza/documents/SDC/leaveReview/node_modules/mysql/lib/protocol/Protocol.js:52:23)
webapp_1 | at Connection.connect (/cadenza/documents/SDC/leaveReview/node_modules/mysql/lib/Connection.js:130:18)
webapp_1 | at Object.<anonymous> (/cadenza/documents/SDC/leaveReview/database/index.js:9:12)
webapp_1 | at Module._compile (internal/modules/cjs/loader.js:702:30)
webapp_1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
webapp_1 | at Module.load (internal/modules/cjs/loader.js:612:32)
webapp_1 | at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
webapp_1 | at Function.Module._load (internal/modules/cjs/loader.js:543:3)
webapp_1 | at Module.require (internal/modules/cjs/loader.js:650:17) fatal: true, code: 'PROTOCOL_CONNECTION_LOST' }
webapp_1 | (node:29) [DEP0096] DeprecationWarning: timers.unenroll() is deprecated. Please use clearTimeout instead.
It should be logging "connected to db". I have also tried solutions that people have suggest for handling server disconnect due to being idle etc etc, but it doesn't even connect to the database in the first place.
So it connected after I changed the host in the database connection index.js file from '127.0.0.1' to:
var connection = mysql.createConnection({
host: 'db',
user: 'root',
password: 'root',
database: 'yelp'
});
where 'db' is referring to the 'db' image in my docker-compose.yml file.
When a developer approaches me saying that his application cannot connect to a container, this is what I usually do. I give them the following setup.
docker-compose.yml:
version: '3.1'
services:
webapp:
build: .
db:
image: mariadb:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: yelp
Dockerfile:
FROM alpine
RUN apk add -U mysql-client
COPY entry.sh .
CMD ["/bin/sh", "entry.sh"]
entry.sh:
#!/bin/sh
echo "Waiting for mysql..."
until mysqladmin ping -h"db" -P"3306" --silent
do
echo "mysql is not ready will retry in 5..."
sleep 5
done
echo -e "\nmysql is ready"
mysql -h"db" -P"3306" -p"root" -u"root" -e "SHOW DATABASES"
And this works, when you run docker-compose up, output being something along these lines:
Starting 51145943_db_1 ... done
Starting 51145943_webapp_1 ... done
Attaching to 51145943_webapp_1, 51145943_db_1
webapp_1 | Waiting for mysql...
webapp_1 | mysql is not ready will retry in 5...
db_1 | 2018-07-03 3:17:10 0 [Note] mysqld (mysqld 10.3.7-MariaDB-1:10.3.7+maria~jessie) starting as process 1 ...
db_1 | 2018-07-03 3:17:10 0 [Note] InnoDB: Using Linux native AIO
db_1 | 2018-07-03 3:17:10 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1 | 2018-07-03 3:17:10 0 [Note] InnoDB: Uses event mutexes
db_1 | 2018-07-03 3:17:10 0 [Note] InnoDB: Compressed tables use zlib 1.2.8
db_1 | 2018-07-03 3:17:10 0 [Note] InnoDB: Number of pools: 1
db_1 | 2018-07-03 3:17:10 0 [Note] InnoDB: Using SSE2 crc32 instructions
db_1 | 2018-07-03 3:17:10 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
db_1 | 2018-07-03 3:17:10 0 [Note] InnoDB: Completed initialization of buffer pool
db_1 | 2018-07-03 3:17:10 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
db_1 | 2018-07-03 3:17:11 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
db_1 | 2018-07-03 3:17:11 0 [Note] InnoDB: Creating shared tablespace for temporary tables
db_1 | 2018-07-03 3:17:11 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
db_1 | 2018-07-03 3:17:11 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
db_1 | 2018-07-03 3:17:11 0 [Note] InnoDB: 10.3.7 started; log sequence number 1630896; transaction id 21
db_1 | 2018-07-03 3:17:11 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
db_1 | 2018-07-03 3:17:11 0 [Note] Plugin 'FEEDBACK' is disabled.
db_1 | 2018-07-03 3:17:11 0 [Note] Server socket created on IP: '::'.
db_1 | 2018-07-03 3:17:11 0 [Warning] 'proxies_priv' entry '#% root#4a91686036a2' ignored in --skip-name-resolve mode.
db_1 | 2018-07-03 3:17:11 0 [Note] InnoDB: Buffer pool(s) load completed at 180703 3:17:11
db_1 | 2018-07-03 3:17:11 0 [Note] Reading of all Master_info entries succeded
db_1 | 2018-07-03 3:17:11 0 [Note] Added new Master_info '' to hash table
db_1 | 2018-07-03 3:17:11 0 [Note] mysqld: ready for connections.
db_1 | Version: '10.3.7-MariaDB-1:10.3.7+maria~jessie' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
db_1 | 2018-07-03 3:17:15 8 [Warning] Access denied for user 'root'#'172.22.0.3' (using password: NO)
webapp_1 |
webapp_1 | mysql is ready
webapp_1 | Database
webapp_1 | information_schema
webapp_1 | mysql
webapp_1 | performance_schema
webapp_1 | yelp
51145943_webapp_1 exited with code 0
This proves that connectivity between containers is working and whatever problem they have is application specific. May be the pass the connection string in a wrong format, or using wrong server name, or trying to connect to early when mysql is not up yet.
Having proven this, they now can "bridge the gap" making small changes to their own or my setup, trying to bring them closer until something breaks. That last step reveals the culprit. For example, if the add mysql command line connection to their application container and it indeed connects, but their application does not, it means that they need to look for the reason in their application code.
From my experience this the quickest way to sort out problems like this, when the reason is not immediately obvious.