Can't connect to local MySQL server through socket

chris (2004-07-06 23:45:43)
4020 views
1 replies

I have been asked this mysql socket question a thousand times, so I decided to document the post-installation process you should go through to ensure that all the permissions are as they should be for your mysql installation.

The error generally looks like the following:

Can't connect: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

The usual fix for these errors is to go through the MySQL post installation process as documented below: (note this is also documented on the mysql documentation site)

shell> groupadd mysql
shell> useradd -g mysql mysql
shell> gunzip < mysql-VERSION.tar.gz | tar -xvf -
shell> cd mysql-VERSION
shell> ./configure --prefix=/usr/local/mysql
shell> make
shell> make install
shell> scripts/mysql_install_db
shell> chown -R root /usr/local/mysql
shell> chown -R mysql /usr/local/mysql/var
shell> chgrp -R mysql /usr/local/mysql
shell> cp support-files/my-medium.cnf /etc/my.cnf
shell> /usr/local/mysql/bin/mysqld_safe --user=mysql &

If your version of MySQL is older than 4.0, use safe_mysqld rather than mysqld_safe


If you want more ideas, then read on. Below is how I have documented the MySQL installation process on servers within my own organisation:

Create a new my.cnf in your /etc directory,
containing the following directives:

[mysqld]
datadir=/export/data
log=/var/log/mysql-messages

# move the compressed archive from it's
download location into a convenient build area
cd /usr/src && mv /home/user/mysql-standard-4.0.13-pc-linux-i686.tar.gz .

# extract and create a symlinked mysql directory
tar -zxvf mysql-standard-4.0.13-pc-linux-i686.tar.gz
ln -s mysql-standard-4.0.13-pc-linux-i686 mysql

# create new mysql user
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql


# create a data directory
mkdir /export/data

# build the default mysql databases
cd /usr/src/mysql
scripts/mysql_install_db
chown -R root .
chown -R mysql data
chown -R mysql /export/data
chgrp -R mysql .

# finally, symlink to mysql's usual location
cd /usr/local/
ln -s /usr/src/mysql mysql

# end of mysql installation

# start server
bin/mysqld_safe --user=mysql &

note that this means that any upgrades to the
mysql installation will atomatically fall into place,
so long as they are symlinked to /usr/src/mysql
as was explained at the beginning of this section.


Hope that helps,

Christo
Digg it! Submit to Slashdot Add to Blinklist Del.icio.us Add to Newsvine Add to Technorati Add it to Google Bookmarks
comment
chris
2005-03-16 17:51:07

an additional note - on some systems you will need to run a chown under /var as shown:

$ chown -R mysql.mysql /var/lib/mysql/*



christo





reply icon