操作系统环境如下:

[root@c1e5cca51f2e data]# cat /etc/issue
CentOS release 6.7 (Final)
Kernel \r on an \m
[root@c1e5cca51f2e data]# uname -r
3.19.0-25-generic
[root@c1e5cca51f2e data]# getconf LONG_BIT
64

 

mysql数据库由于yum安装方式,所以默认的数据库目录是/var/lib/mysql,也就是my.cnf文件中的datadir指定的,于是乎吧datadir位置更改了,然后重启mysql数据库,结果悲剧了,无人进入mysql数据库,报错提示如下:

[root@c1e5cca51f2e data]# mysql

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

 

解决方法:

1:由于mysql是yum方式安装的,所以默认查找的是'/var/lib/mysql/mysql.sock',即使my.cnf文字sock位置更改后也无法进入数据库,如果把sock文件连接到'/var/lib/mysql/目录下即可解决问题。

 

2:mysql数据库已经启动但是无法进入数据库,登录mysql数据库指定主机,用户名和密码即可进入数据库

root@c1e5cca51f2e data]# mysql -uroot -p -h 127.0.0.1
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, 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>

 

3:直接my.cnf文件中mysql段和client段下添加以下内容,然后重启mysql即可解决

[mysql]

socket=/data/mysql.sock

[client]
socket=/data/mysql.sock
[root@c1e5cca51f2e ~]# /etc/init.d/mysqld restart

Stopping mysqld:                                           [  OK  ]

Starting mysqld:                                           [  OK  ]
[root@c1e5cca51f2e ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, 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>