Tag Archives: MYSQL error 2002

[Solved] SQLSTATE[HY000] [2002] Connection refused to report an error when PHP connects to mysql in the docker container

Laradock is a complete PHP local development environment provided by Docker

Error when connecting to MySQL in the framework

SQLSTATE[HY000] [2002] Connection refused

The main reason is that there is not enough understanding of the isolation mechanism of Docker containers. Each container is isolated. If there are interdependent services, it is necessary to perform display associations, such as using options --link.
In the same way, docker-composewhen using , the association between containers is similar to the following method:

# docker-compose.xml basic
version: '2'
services:
	
	...
	
    php:
        build: ./php
        
        ...
        
        links:
            - "mysql"
           
    mysql:
        build: ./mysql
        ports:
            - "3306:3306"
        environment:
            MYSQL_PASSWORD: root

Note that the key point is here: the code to test the connection to MySQL is actually running in the container corresponding to PHP, and the MySQL service is in its own container. When our host fills in 127.0.0.1, it actually corresponds to the PHP container. Inside, so it is impossible to find the corresponding service, which causes the above connection refused error.

So, how do you connect?

In fact, after the containers are associated, they can be connected by the container name.

In the above docker-compose.xmldocument, the container corresponds to the service name MySQL mysql, PHP container name is associated with it mysql, so the 127.0.0.1change mysqland then connect to.

# thinkphp project modify database.php file
'hostname'        => 'mysql',

# Laravel project modify .env file
DB_HOST=mysql

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’

The binary installation mysql reports the following error

[root@tzPC sdb1]# mysql -v
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

It is found that /tmp/mysql.sock does not exist. The reason for this is generally due to inconsistent configuration files. If mysqld is started by mistake, mysqld_safe will clear mysql.sock once.

Solution

First check whether the current process has a surviving mysql, kill it, and then restart the database

$ ps -ef | grep mysql
$ kill -9 xxxx
$ /etc/init.d/mysqld start

If it still cannot be resolved, check whether the configuration file is correct

$ cat /etc/my.cnf
[mysqld]
user=mysql
basedir=/mnt/sdb1/mysql
datadir=/mnt/sdb1/mysql_data
socket=/tmp/mysql.sock
server_id=6
port=3306
[mysql]
socket=/tmp/mysql.sock

Use the command to find the mysql.sock file

$ find / -name mysql.sock

If not found, re-execute mysql_install_db to rebuild the authorization table

$ mysql/bin/mysql_install_db

Then execute the following command, the mysql.sock file will be there

$ mysql/bin/mysqld_safe &

$ find / -name mysql.sock
/tmp/mysql.sock

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’

ERROR 2002 (HY000) : Can ‘t connect to local MySQL server through socket’/var/run/mysqld/mysqld. (2) the sock ‘
The server encountered a familiar problem today
Input:

#mysql -u root -p
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

Then he searched the Internet for answers
I’m going to try each of the methods that you’ve given me
Plan 1

#ps -A|grep mysql

Display similar to:

1829 ?       00:00:00 mysqld_safe
1876 ?       00:00:31 mysqld
#kill -9 1829
  #kill -9 1876
#/etc/init.d/mysqld restart
#mysql -u root -p

His problem is solved, mine is not solved!
keep looking for
Scheme 2
Check the/etc/rc. D/init. D/mysqld status to see if the mysql has already started.
see if other permissions problems.
— — — — — — — — — — — — — —

[root@localhost beinan]#chown -R mysql:mysql /var/lib/mysql
[root@localhost beinan]# /etc/init.d/mysqld start

Start MySQL: [OK]

[root@localhost lib]# mysqladmin -uroot password ‘123456’
[root@localhost lib]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 3 to server version: 4.1.11

Type ‘help;’ or ‘/h’ for help. Type ‘/c’ to clear the buffe

His was solved, and my troubles continued, and my search continued
Scheme 3
problem solved, but max_connections=1000. He said too much, then changed it to 500, but the problem was solved.
Still won’t do
Plan 4

	/var/lib/mysql All file permissions change to mysql.mysql

No, no
Scheme 5
abstract: Solve by mysql. The problem of the sock to connect mysql main hint is that the problem can’t ‘/ TMP/mysql. The sock’ connected to the server, and PHP standard configuration is used ‘/ TMP/mysql. The sock, but some mysql installation method will mysql. The sock in/var/lib/mysql. Where the sock or other, you can modify the/etc/my CNF file to correct it, open the file, you can see the following things:

 	[mysqld] 
	socket=/var/lib/mysql .sock 

A change would be fine, but it could also cause other problems, such as the mysql program can’t be connected. Add one more point:

	[mysql ] 
  socket=/tmp/mysql .sock 

Or you can modify the configuration in php.ini to make PHP use another mysql.sock, and you can find this for yourself
or you can do it this way:

ln -s /var/lib/mysql /mysql .sock /tmp/mysql .sock

It worked. That was ln-s /var/lib/mysql. sock/TMP/mysql. sock
OK!
Solution to MySQL. Sock loss problem under MySQL 6
[ubuntu, Linux, mysqld.sock]
MySQL. Sock loss is generally caused by inconsistent configuration files. The solution:
It is judged that ordinary people did not switch to mysql user when solving the failure, which caused some permission problems. Therefore, they could not create mysql authorization table, so they could not create/TMP/mysqlm. sock and Hostname. pid files. Therefore, the summary solution is as follows (note: using root is also acceptable) :

#su mysql
$/usr/local/bin/mysql_install_db //Reconstruction authorization table
$/usr/local/bin/mysqld_safe &
$/usr/local/bin/mysql //Test
mysq>bye;
$

The file has been solved, and the new/TMP /mysql.sock and hostname.pid
have been generated.
Share source address: https://blog.csdn.net/thanklife/article/details/69225724

The fifth plan, which I borrowed from the Internet, was not successful for me. Only the sixth one succeeded!