Friday, July 9, 2010

Troubleshooting MYSQL : Could not connect: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

1. maybe lost mysql.sock file

You need to give the mysql group ownership of the /var/lib/mysql directory.

#> chown -R mysql:mysql /var/lib/mysql

If you lost your mysql.sock file just make a symlink to the file:

# ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

ref: http://answers.yahoo.com/question/index?qid=20080704165734AAG757T


2. When connecting to a MySQL server located on the local system, the mysql client connects thorugh a local file called a socket instead of connecting to the localhost loopback address 127.0.0.1. For the mysql client, the default location of this socket file is /tmp/mysql.sock. However, for a variety of reasons, many MySQL installations place this socket file somewhere else like /var/lib/mysql/mysql.sock.

While it is possible to make this work by specifying the socket file directly in the mysql client command

mysql --socket=/var/lib/mysql/mysql.sock ...

it is painful to type this in every time. If you must do so this way (because you don’t have permissions to the file in the solution below), you could create an alias in your shell to make this work (like alias mysql=”mysql –socket=/var/lib/mysql/mysql.sock” depending on your shell).

To make your life easier, you can make a simple change to the MySQL configuration file /etc/my.cnf that will permanently set the socket file used by the mysql client. After making a backup copy of /etc/my.cnf, open it in your favorite editor. The file is divided into sections such as
[mysqld]
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock

[mysql.server]
user=mysql
basedir=/usr/local/mysql

If there is not currently a section called [client], add one at the bottom of the file and copy the socket= line under the [mysqld] section such as:

[client]
socket=/var/lib/mysql/mysql.sock

If there is already a [client] section in the my.cnf file, add or edit the socket line as appropriate. You won’t need to restart your server or any other processes. Subsequent uses of the mysql client will use the proper socket file.

ref: http://www.tech-recipes.com/rx/762/solve-cant-connect-to-local-mysql-server-through-socket-tmpmysqlsock/


3. Okay, so let's answer to this question, once for all.

First of all, you DO NOT WANT to recreate mysql.sock,
that logically makes NO sense at all.

What you REALLY want to do is restart the mysql daemon (mysqld),
which when it is up and running will create a socket for you,
so you can communicate with it.

- Creating a file by hand won't help
- Creating a socket by hand won't help
- None of these "can talk" to the server which is "not running".

The reason why you "lost mysql.sock" is that:
- the mysqld daemon stop or crashed
- a fatal error occurred
- your configuration is wrong /etc/my.cnf
- your permissions are wrong /var/lib/mysql and /var/lib/mysql/mysql ...
=> make sure the owner is mysql:mysql and 775 or similar.
- your mysql data is corrupted: myisamcheck or mysql_install_db

Now, you may say, hey but I tried:
- /etc/init.d/mysqld restart
- /etc/init.d/mysqld start
- mysqld_safe --user=mysql &

and none of those worked...

That's sort of normal, if the server crashed in the first place.

So, what you REALLY need to do is look at the file located in:
/var/log/mysqld.log

like this:
tail -f /var/log/mysqld.log
or
cat /var/log/mysqld.log

figure out the error message, solve that error message
or try to find a work-around for that error on google.

Once that error message is "solved", then you can safely restart mysqld
[no need to reboot], and once the daemon is up and running,
your magical "mysql.sock" file will appear as it did before. =)

The big problem with this is that the "mysql.sock" error message
is not helping at all, and you must "know" that the real error message
is in the mysql log file that's all.

Hope this help.

ref: http://www.linuxforums.org/forum/servers/1451-what-mysql-sock-file-2.html

4. Only delete de log files ib_logfile0, ib_logfile1 and ibdata1 in the /var/lib/mysql directory and then type:

"mysqld_safe --user=mysql &"

and automatically the mysql.sock file will be create and the news logs files.

Wow!!! Do not this if you are using InnoDB as db. Then you'll loose a lot of information. Deleting those files that is.

ref : http://www.linuxforums.org/forum/servers/1451-what-mysql-sock-file.html

5. For self-installed MySQL

If you have downloaded and installed MySQL yourself but are using the pre-installed version of PHP, note that your custom version of MySQL might be configured to use the old MySQL socket location, /tmp/mysql.sock. The version of PHP in this software update uses the newer location /var/mysql/mysql.sock by default.

Therefore, if your PHP scripts are failing to connect to your custom installation of MySQL, this is a likely cause. To correct this, you can modify the PHP configuration file to use the old MySQL socket location:

1. Create /etc/php.ini if it is not present. (You can do so by copying /etc/php.ini.default to /etc/php.ini).
2. Edit the /etc/phi.ini config file, find the [MySQL] section, and change this line:

mysql.default_socket = /var/mysql/mysql.sock

To:

mysql.default_socket = /tmp/mysql.sock

3. Restart the web server to pick up the new PHP settings. It should not be necessary to restart MySQL.

If you subsequently decide to use the version of MySQL that is pre-installed with Mac OS X Server, which uses /var/mysql/mysql.sock, you'll want to either remove that line from /etc/php.ini, or change it back.

ref: http://support.apple.com/kb/HT3077?viewlocale=en_US


6. Since its a prod DB, I need to do this sometime tonight. I will try >su
> cd /etc/init.d
>./mysql stop
>./mysql start

Apart from:
chmod 755

Have you checked that the files are owned by mysql?

Otherwise you should run:
chown -R mysql /your/mysql/datadir

ref : http://forum.percona.com/index.php/m/4324/

No comments:

Post a Comment