完美解决Can't connect to local MySQL server through socket mysql.sock
2017-08-28 23:40
344 查看
We’ll be doing this in the following order.
Stopping the MySQL server
Create a new data directory and move the content from the old data directory
Correct the MySQL configuration file
Adjust SELinux parameters to accept our new change
Starting the MySQL server
Edit my.cnf file for your distribution. In my example, it’s located in the /etc/mysql/ directory. RHEL/CentOS/Scientific Linux put my.cnf file directly in /etc by default.
Change
to
and
to
and save the file.
Should the following command output “Permissive” or “Disabled” then you may skip the details for SELinux.
Run the semanage command to add a context mapping for /srv/mysql.
Now use the restorecon command to apply this context mapping to the running system.
If this is working, you’re up and running. Should you get a message that says
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’
then add the following to your /etc/my.cnf
Optionally you can just use
to avoid connecting via the socket.
http://crashmag.net/change-the-default-mysql-data-directory-with-selinux-enabled
Stopping the MySQL server
Create a new data directory and move the content from the old data directory
Correct the MySQL configuration file
Adjust SELinux parameters to accept our new change
Starting the MySQL server
Stopping the MySQL server
# service mysqld stop
Create a new data directory and move the content from the old one
Creating a new data directory
# mkdir /srv/mysql/
# chown mysql:mysql /srv/mysql
Moving the original data files
# mv /var/lib/mysql/* /srv/mysql/
Correct the MySQL configuration file
Edit my.cnf file for your distribution. In my example, it’s located in the /etc/mysql/ directory. RHEL/CentOS/Scientific Linux put my.cnf file directly in /etc by default.# nano /etc/mysql/my.cnf
Change
datadir=/var/lib/mysql
to
datadir=/srv/mysql
and
socket=/var/lib/mysql/mysql.sock
to
socket=/srv/mysql/mysql.sock
and save the file.
Adjust SELinux parameters to accept our new change
Should the following command output “Permissive” or “Disabled” then you may skip the details for SELinux.# getenforce
Run the semanage command to add a context mapping for /srv/mysql.
# semanage fcontext -a -t mysqld_db_t "/srv/mysql(/.*)?"
Now use the restorecon command to apply this context mapping to the running system.
# restorecon -Rv /srv/mysql
Starting the MySQL server
# service mysqld start
Verifying access and connectivity
$ mysql -u root -p
mysql> show databases;
If this is working, you’re up and running. Should you get a message that says
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’
then add the following to your /etc/my.cnf
[client] socket = /srv/mysql/mysql.sock
Optionally you can just use
$ mysql -u root -p --protocol tcp
to avoid connecting via the socket.
http://crashmag.net/change-the-default-mysql-data-directory-with-selinux-enabled
相关文章推荐
- Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’解决方法
- 解决Can 't connect to local MySQL server through socket '/tmp/mysql.sock '(2) ";
- 解决mysql启动问题: Can't connect to local MySQL server through socket '/tmp/mysql.sock'
- 解决*unix系统下Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
- 解决Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’错误
- 更换mysql数据目录后出现ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 的解决办法
- 解决Can 't connect to local MySQL server through socket '/tmp/mysql.sock '(2) "
- 解决“ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111)”
- 解决ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
- Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)解决办法
- 原来如此简单:解决Mac上的Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
- 解决Can 't connect to local MySQL server through socket '/tmp/mysql.sock '(2) ";
- Can't connect to local MySQL server through socket '/tmp/mysql.sock'解决
- Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ 解决方法
- mysql中异常解决Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) .
- ERROR 2002: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)解决
- mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' 解决方法
- Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)的解决问题
- 解决Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)的问题
- mysql报错 Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 解决办法