MySQL缺少mysql.sock或mysql.sock路径不正确的解决办法

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

MySQL下mysql.sock丢失丢失的原因一般是因为配置文件不一致的原因,mysqld 错误启动,mysqld_safe 会清除一次mysql.sock 。解决方法是:

判断一般人解决故障时没有切换到mysql用户,造成权限有问题,无法创建mysql授权表,所以也就无法创建/tmp/mysql.sock 和hostname.pid文件。因此,总结解决方法如下:

[root@tencent ~]# su root //切换到root用户或者具有数据库权限的其他用户
[root@tencent ~]# /bin/mysql_install_db //在bin目录下运行mysql_install_db重建授权表
[root@tencent ~]# /bin/mysqld_safe &
测试
[root@tencent bin]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.41 Source distribution

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye
[root@tencent bin]# mysql_install_db 
Installing MariaDB/MySQL system tables in '/var/lib/mysql' ...
190426 18:19:33 [Note] /usr/libexec/mysqld (mysqld 5.5.60-MariaDB) starting as process 31241 ...
OK
Filling help tables...
190426 18:19:34 [Note] /usr/libexec/mysqld (mysqld 5.5.60-MariaDB) starting as process 31250 ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:

'/usr/bin/mysqladmin' -u root password 'new-password'
'/usr/bin/mysqladmin' -u root -h tencent password 'new-password'

Alternatively you can run:
'/usr/bin/mysql_secure_installation'

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.

You can start the MariaDB daemon with:
cd '/usr' ; /usr/bin/mysqld_safe --datadir='/var/lib/mysql'

You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/mysql-test' ; perl mysql-test-run.pl

Please report any problems at http://mariadb.org/jira

The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/

[root@tencent ~]# mysqld_safe &
[1] 3559
[root@tencent ~]# 190426 18:31:34 mysqld_safe Logging to '/usr/local/mysql/data/tencent.err'.
190426 18:31:35 mysqld_safe A mysqld process already exists

文件已经解决,重新生成新的 /tmp/mysql.sock 和 hostname.pid

二、解决mysql.sock文件路径不对

1.在/var/lib/mysql创建一个link:ln -s /var/lib/mysql/mysql.sock /path/to/mysql/mysql.sock

2.在root权限下修改my.cnf文件(/etc/mysql/my.cnf),指定正确的路径

3.1和2任选其一就可以

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章