MySQL ‘startup has port listening, but the service is not fully started.

background

The database is initialized for the first time when MySQL is started. After MySQL is fully started, the database is initialized for the second time.

The –skip-networking option is used to initialize the MySQL database for the first time. After initialization, the –skip-networking option is disabled to restart MySQL.

The second initialization of the database sets the root password.

judgeMySQLThe mode has been fully started

Try to connect to MySQL. If the connection is successful, the service has been started

mysql -hlocalhost -P3306 -uroot

Copy the code

The accident

Due to the above judgment methods is to use the socket to connect to the database, but for the first time only prohibit MySQL way of TCP/IP connection, so don’t wait for the first time to initialize the database to complete may have a second to initialize the database, During the second database initialization, the root password is set earlier than the first database initialization. As a result, the database fails to be connected during the first initialization. In the end, the TCP/IP connection is not enabled, and the application fails to connect to the database.

First Modification

Use -h127.0.0.1 to connect to the database

Mysql - h127.0.01 - P3306 - urootCopy the code

However, the initial change failed because the root user’s host is set to localhost and 127.0.0.1 is not allowed to connect to the host.

Second Modification

Wait-for-it-sh 127.0.0.1:3306 --timeout=300 mysql -hlocalhost -p3306-urootCopy the code

Wait-for-it. sh is used to check whether 127.0.0.1:3306 is listening. If yes, the database is initialized for the first time, and then localhost is used to try to connect to the database.

Original link: k8scat.com/posts/wait-…