完美解决Can't connect to local MySQL server through socket mysql.sock

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

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 '/tmp/mysql.sock' (2)" 是一个错误提示,它表示无法通过指定的socket连接到本地的MySQL服务器。这个错误通常出现在使用PHP连接MySQL数据库时。我们可以通过以下方法来解决这个问题: 1. 确认MySQL服务器是否正在运行。可以使用以下命令来检查MySQL服务器的运行状态: ``` systemctl status mysql ``` 2. 确认MySQL服务器是否使用了正确的socket路径。通过检查MySQL配置文件中的socket路径来确认,配置文件的位置通常是`/etc/mysql/mysql.conf.d/mysqld.cnf`。 3. 如果socket路径配置正确,但仍然出现错误,可以尝试手动创建socket文件。使用以下命令可以创建socket文件: ``` sudo touch /tmp/mysql.sock ``` 4. 如果手动创建socket文件后仍然无法解决问题,可以尝试修改MySQL配置文件中的socket路径,将其指向正确的位置。确保修改后重新启动MySQL服务。 总结起来,要解决"Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)"的问题,我们需要确认MySQL服务器是否运行,检查配置文件中的socket路径是否正确,并尝试手动创建socket文件。如果问题仍然存在,可以尝试修改配置文件并重新启动MySQL服务。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Can't connect to local MySQL server through socket](https://blog.csdn.net/qq_35462323/article/details/105024596)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Can’t connect to local MySQL server through socket解决方法](https://blog.csdn.net/weixin_34112900/article/details/93854948)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值