Mysql error 2002 after server shutdown - mysql

The server had shut down yesterday because electricity had cut off. After that, I've tried connect to Mysql but only got the 2002 error related with mysql.sock. Anyway, I've fixed the issue with this solution:
Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)
sudo chmod -R 755 /var/lib/mysql/
But my question is: Why? Why a shutdown caused this error? How prevent this from happening again?

Check the '/var/log/mysqld.log' first to see messages on file/directory permission
If the MySQL server terminated unexpectedly, the MySQL socket was not reset after the reboot
Due to power shutdown, the existing mysql process got terminated and socket file is not removed properly as this is not a graceful shutdown. The MySQL server no longer attempts to verify the existence of a MySQL socket when the socket is not being used by any processes as the block checking for socket existence should only matter if there's a PID attached to the other end of the socket. On unclean reboot, this socket will persist and thus cause the failure.
Solution :
I guess the simplest thing to do is remove the socket check. If there's no mysqld pids, then the existence of the socket may not be a useful check

Why a shutdown caused this error?
The error indicates that MySQL was not bound to its socket. It probably wasn't able to bind to its socket because it appeared that another process (namely the MySQL instance that did not shutdown cleanly) was still bound thereto.
How prevent this from happening again?
Install a UPS (uninterruptible power supply), which can provide backup power (from batteries) whilst signalling to the server that primary power has failed. This gives the server an opportunity to cleanly shutdown, so that its files are not left in a corrupt state.

Related

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) when running mysql -uroot

Out of the sudden, I am getting this error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
on mysql when I run mysql -uroot.
I haven't touched web dev for around 1 month and as soon as I wanted to return to my project, I am getting this very annoying error out of nowhere. I have trie all advice and solutions from other answers and none of them work. This includes changing permissions to 777, ownership to root, mysql etc, killing PID all of those and none of them work. I even tried re-installing mysql and still getting the same error. I even re-installed my whole macOS Mojave and still getting the same error. I mean this error doesn't even make sense...
Is mysql completely broken or I am just super dumb?
Has anyone had this issue? If so how did you solve it?
The error says, it's using the socket /tmp/mysql.sock and failed.
Try to troubleshoot with the below things.
check the process status of mysqld and confirm its running or not. If running which socket is using.
ps -ef | grep -i mysqld
Based on the socket file try login to the MySQL through command line.
mysql -uroot -p -S <<file.sock>>
Check the my.cnf to make sure which socket file its mentioned under the [client] group option.
In your case, probably the mysql instance reading the socket from my.cnf files where it placed somewhere. Check the hierarchy of the configuration file usage by mysqld which may be located in either the /etc or /etc/mysql

The file mysqld.sock is removed and recreated constantly

This error appears to me:
ERROR 2002 (HY000): Can't connect to local MySQL server through
socket '/var/run/mysqld/mysqld.sock' (2)
I saw in others threads and no one help me, because it is created but in seconds is removed and recreated again...
Any idea?
That is not the error, it is a symptom.
Your mysql-server is dieing and restarting again probably from some error in configuration, or permissions in your system.
You should start by looking your syslog either /var/log/syslog or /var/log/messages to see what is happening with your mysqlserver when it starts.
If you don't see anything you can try starting mysqld from your root-shell and see what it sends to stdout/err.

After restarting query, Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

There are a number of questions on this topic, but they all appeal to not knowing the cause.
The cause of my situation is I was running a query that was taking a very long time, so I ran service mysqld restart and now, that specific table is giving me this error.
MySQL is running and the other tables load fine.
The error is:
#2002 - Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) The server is not responding (or the
local server's socket is not correctly configured).
Your table may be corrupted.
Check how to solve it here.

Website connection error related with MySQL 4.1.24

I get an error on my website (MySQL with TCP/IP connection)
Can't connect to local MySQL server
It sounds easy, perhaps, but not so easy to solve as would seem. At first i verified that mysqld process is running. Then connected to SSH server and started /etc/init.d/mysqld restart, but nothing has changed. Also i checked is the server running, using telnet your-host-name tcp-ip-port-number, just in case, but MySQL does not work on this port and shows another error: telnet: Unable to connect to remote host: Connection refused
The last i have tried is to run mysql with the skip-networking option, and it did not help as well as other attempts above. Any suggestions would be very appreciated.
This error normally means that MySQL is not running on the system or that you are using a wrong TCP/IP port number while trying to connect to the mysqld server.
Try to connect to the mysqld daemon on the local machine and check by mysqladmin variables, which TCP/IP port is configured to use mysqld (variable port).
Perhaps you are running MySQL-server with no corresponding privileges for the directory holding the socket file. In this case, either change the privilege for the directory or restart mysqld
Also this discussion might be interesting for you: http://community.office365.com/en-us/f/172/p/266451/815406.aspx
Unfortunately, i am not a pro and my advices can help not much, but that all what i found about your issue
This sounds like you are being blocked by the firewall on the server. You can disable the firewall for a quick test:
service iptables stop
service iptables start
This article will show you how to apply rules to the firewall to allow mysql access.
http://www.cyberciti.biz/tips/linux-iptables-18-allow-mysql-server-incoming-request.html
If you have SELinux, you have another set of problems which I can't help you with.
Sounds to me permission issue.
Does it run on the local machine?
If yes, then it might me possible it isn't allowed to listen any port(security purposes: mostly on a linux based server). Otherwise, Network user might not have permission granted to access mysql over internet.
As you say, if it's a website, I would never allow user access mysql directly but only the server shall: possible security breach otherwise.(you don't want your database to be dropped, do you?)
Well still, proper answer to you question resides here: http://dev.mysql.com/doc/refman/5.1/en/grant.html
and this might help:
Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server
Firstly, you should verify that MySQL is actually running by checking your processlist. On linux you could do that like this, note that you should see both mysqld_safe and then mysqld as two separate processes.
sudo ps auxwww|grep -i mysqld
If it is not running, I would check the MySQL error log for clues as to why it is not starting.
If you then verify that it is running, we can check to see what ports or unix sockets it is listening on like so. If this doesn't work, get the process ID of mysqld (not mysqld_safe) and try search for that with grep instead of 'mysql'
sudo netstat -anp|grep -i mysql
You'll obviously want to restart without skip-networking to see a TCP socket appear.
Based on the output of that, you should see both a unix socket and a tcp socket. Also check the address in the fourth column for the TCP socket, it will likely say either 127.0.0.1:3306 or 0.0.0.0:3306. The former means that you can only connect via localhost (127.0.0.1) and the latter means the connection will work on any IP address.
If you've gone through all of that and are still not sure why it is working, you could post the processlist and netstat outputs for further review, along with the exact settings you are using to try and connect to MySQL and indicate whether you are connecting from the same server (i.e. locally) or from another server over the network.
See also:
http://dev.mysql.com/doc/refman/5.6/en/can-not-connect-to-server.html

Can't run mysql console via ssh after restarting service

I use Ubuntu 12.04 x64, currently with Mysql 5.5.28 (plus Galera, but I don't think it's related).
I made some changes in my.cnf, but changes was related to InnoDB settings (a lot more memory for the buffer pool, flush, thread concurrency settings etc.).
After that when I hit service mysql restart I cannot enter mysql console - I get following error message:
Lost connection to MySQL server at 'reading initial communication packet',
system error: 104
Sometimes next try results in entering console, sometimes not and I have to restart my SSH connection. What could be the problem and how to fix it?
EDIT:
I noticed that most of the time after the system error: 104 following error occurs:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock' (111)
After few times it usually starts. Unfortunately now it didn't and in syslog there is:
Error in my_thread_global_end(): 2 threads didn't exit
Restarting SSH connection / MySQL server is not working. I can try reboot server but I'd try to resolve this without that step. Its testing environment so I want to find better solution than restart whole server ;)
Please try the answers in this page:- Lost connection to MySQL server at 'reading initial communication packet', system error: 0
Also try this.