欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Can’t connect to MySQL Server情况分析和解决

程序员文章站 2022-03-10 18:25:26
查看mysql 日志:cat /var/log/mysqld.log查看 mysql 状态:netstat -anp | grep mysqld 确定ip, port可工作: telnet *.*....

查看mysql 日志:cat /var/log/mysqld.log查看 mysql 状态:netstat -anp | grep mysqld

确定ip, port可工作: telnet *.*.*.* port (telnet 192.168.233.142 3306)

远程访问mysql:mysql -u root -h *.*.*.* -p

打开https 端口

# http, https
  $iptables -a input -p tcp -m state --state new --dport 80 -j accept
  $iptables -a output -p tcp -m state --state new --dport 80 -j accept
  $iptables -a output -p tcp -m state --state new --dport 443 -j accept

there are a lot of different reasons behind this error. sometime, you may not see the exact issue on the error message. you have to dig deeper to identify the exact cause. it may take a lot of time and energy to debug this error. i have encountered this error many times, so based on my experience and research on this issue; i have gathered and given the following possible reasons and workarounds to fix this error.

error message:

can't connect to local mysql server

reason 1:mysql server is not running on the server.

workaround 1:check mysql server is running on the server.

mysql process name : mysqldmysql default port : 3306

on linux:to check the service status : systemctl status mysqlto check the process list : ps ax | grep mysqlto check the listening port : netstat –anp | grep mysql

on windows:to check the service status : ctrl + r -> mysqlto check the process list : task manager -> mysqld

if you are trying to connect to mysql service running on remote server, use telnet.telnet 127.0.0.1 3306 (replace the ip and port)

if not running, start the mysql service and try to connect to mysql server. if mysql is not starting, see the error log to find out the reason and fix the error. if this is the reason, the error could have resolved now.

reason 2:mysql socket file is missing / deleted / different location.

workaround:mysql server can be connected through either socket file or tcp/ip protocol. when you are trying to connect, mysql service running on localhost, it will be connected through the socket file. if socket file missing, you will get error message with location of the file.

can’t connect to local mysql server through socket ‘/var/run/mysqld/mysqld.sock’ (2) when trying to connect.

socket file is a special purpose file, it is used as interface to connect mysql server from clients. this file is created automatically by the server. if the file is not exists, it is because either of the following issues:

mysql server was not started.mysql is created socket file in different location.socket file unexpectedly removed / deleted /missing.mysql service is started with socket variable but client trying to connect to default socket.

example: mysqld –socket=/var/lib/mysqld.sock

if mysql server is not started:

use workaround 1 to verify mysql is running or not. if mysql service is not running, start the mysql service by using the following method:

on linux:

sudo systemctl start mysql

on windows:

go to services.msc and start mysql service.

note: you can also start the mysql service by other methods too.

if mysql is created socket file but it is in different location / mysql service is started with socket variable but client trying to connect to default socket.

you can use mysqladmin utility to check the current location of the mysql socket file, as below:

mysqladmin -u root -h 127.0.0.1 -p version

when you connect it from client, you can specify the client explicitly using - -socket variable or change the socket file location in configuration file (this required service restart).

[mysqld]socket=/datadir/mysql.sock

if socket file is missing / removed /deleted:

i have seen some cases; socket file is missing for no reason. in this case, connect with tcp / ip protocol to restart the service. socket, file cannot be created as plain empty file manually. the file should be created by server.

the worst part is you cannot shutdown the server. while stopping the server, it will look for socket file and if it not exists, it will through the socket file missing error. so the alternative method should be as follows:

to shutdown mysql server : mysqladmin –h 127.0.0.1 –protocol=tcp –u root –p shutdownto login to mysql server : mysql –h 127.0.0.1 –protocol=tcp –u root –p

reason 3:security enhanced linux is protecting mysqld process.

workaround 3:security enhanced linux (selinux) is a linux feature that provides mechanism for supporting access control security policies.

in simple words, it does not allow mysql to access other directories and ports apart from pre-configured access objects. it means, by default, mysql is configured to access/var/lib/mysql,but when you configured data directory and socket files to different folders, it does not permit access.

you can disable the selinux protection for mysqld process. this you can done by:

to set selinux into permissive mode : setenforce 0 (it will allow but show warnings!)

this settings is applicable still the next server restart. in order to permanently disable the selinux (private networks), edit the configuration as follows:

to edit the selinux configuration file : vi /etc/selinux/configto disable the selinux protection : selinux=disabled (restart required!)

if you wish to configure selinux protection for mysqld process with proper privileges (production environment), you can referthis pagefor complete details.

reason 4:

mysql server is bound to single address (bind_address)

workaround 4:

to edit bind_address variable : vi /etc/my.cnfto change bind_address : bind_address=0.0.0.0.0

here, the problem is, sometimes, by default mysql bind_address variable is set to localhost or some ip address. when you try to connect mysql server from other hosts, mysql does not allow, and provides the error message, since mysql service is bound to only that bind_address value.

by changing bind_address value into 0.0.0.0 will accept connections from all server hosts.

reason 5:firewall blocking mysql port:

workaround 5:if the firewall is blocking mysql port 3306, you can open the port on host machine to fix this issue.

sudo iptables -i input -p tcp --dport 3306 -j accept

sudo service iptables save

note: the firewall rules for windows and latest version of centos may vary, use accordingly.

reason 6:access restricted to the server hosts.

workaround 6:there is a possibility that access restricted to server. you can allow access to server by editing the host.allow (linux) / hosts (windows file)

to change in linux : vi /etc/host.allowto change in windows : %windows%\system32\drivers\etcto allow access from all hosts : mysqld: all: allow (this is not best practice to allow all – allow only the web server / client hosts allow)

note: see the tcp wrapper for more details (if you are interested!!!).

other possible reasons:

incorrect host name / ip address – verify you are giving correct ip address / hostname.the user may not access to database - give access in mysql using grant command.the mysql variable old_passwords = 1, old_password() is deprecated in mysql 5.7. it is hashing method to store mysql password. setting this variable to 0 will allow you connect mysql. (see more about this here)the mysqld service unable to create pid file – this issue due to mysql user does not have access to create the file. once, you given access this issue will be resolved.

i hope this article helps you in fixing this error. still, if you are unable to fix this issue, please mention in comment section, i will get back to you to fix this error.

do you know, other workarounds to solve this issue please mention in comment section, so that i will update the answer, it will be helpful to others.