I'm trying to get a local LAMP setup on my Ubuntu desktop. I'm successfully got PHP install but I'm having trouble with MySQL

If PHP tries to connet to MySQL I get this error:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /var/www/testing.php on line 3 Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

If I try via command line I get much the same error:
owen@desktop:~$ mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (13)

Weirdly "/var/run/mysqld" does not exist.

Running a whereis command I get the following:
owen@desktop:~$ whereis mysqld.sock
mysqld: /usr/sbin/mysqld /usr/share/man/man8/mysqld.8.gz

So is MySQL even installed? Well acording to dpkg
owen@desktop:~$ dpkg -l | grep mysql
ii libapache2-mod-auth-mysql 4.3.9-13ubuntu1 Apache 2 module for MySQL authentication
ii libdbd-mysql-perl 4.016-1 Perl5 database interface to the MySQL database
ii libmysqlclient15off 5.1.30really5.0.83-0ubuntu3 MySQL database client library
ii libmysqlclient16 5.1.49-1ubuntu8.1 MySQL database client library
ii mysql-admin 5.0r14+openSUSE-2.1 GUI tool for intuitive MySQL administration
ii mysql-client-5.1 5.1.49-1ubuntu8.1 MySQL database client binaries
ii mysql-client-core-5.1 5.1.49-1ubuntu8.1 MySQL database core client binaries
ii mysql-common 5.1.49-1ubuntu8.1 MySQL database common files, e.g. /etc/mysql/my.cnf
ii mysql-gui-tools-common 5.0r14+openSUSE-2.1 Architecture independent files for MySQL GUI Tools
ii mysql-query-browser 5.0r14+openSUSE-2.1 Official GUI tool to query MySQL database
ii mysql-server 5.1.49-1ubuntu8.1 MySQL database server (metapackage depending on the latest version)
ii mysql-server-5.1 5.1.49-1ubuntu8.1 MySQL database server binaries and system database setup
ii mysql-server-core-5.0 5.1.30really5.0.83-0ubuntu3 MySQL database core server files
ii mysql-server-core-5.1 5.1.49-1ubuntu8.1 MySQL database server binaries
ii php5-mysql

Can someone please help I'm really confused as what to do next. I'm not a Linux expert at all most of these commands I've ran I found of diffrent blogs and help forums.

  • Apparently your mysql server is not running. Use /etc/init.d/mysql-server start and check your log tail -f /var/log/syslog for any errors on the startup process. – coredump Feb 2 '11 at 13:27
  • netstat -tupln | grep 3306 – Andrew Taylor Feb 2 '11 at 13:29
  • Run the above comment and it will identify if mysql is listening on it's standard port. – Andrew Taylor Feb 2 '11 at 13:29
  • owen@desktop:~$ /etc/init.d/mysql-server start bash: /etc/init.d/mysql-server: No such file or directory – Owen Feb 2 '11 at 14:00
  • owen@desktop:~$ netstat -tupln | grep 3306 (Not all processes can be identified, no information of ownership of the process not show, to be root to see them all.) – Owen Feb 2 '11 at 14:02

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

Sounds like you're doing something like.....


The mysql client lib, when it sees 'localhost' doesn't bother making a network conection - it uses the local file (unix) socket. In order to find this socket, it should check the php.ini setting, or use the value in my.cnf, or failing these, compiled in default(s).

Check the mysql is running

ps auxwww | grep mysqld

Check its listening on a network socket:

netstats -nap

The above should also show what file socket it is listening on.

Assuming this is a default installation (i.e. listening on the network port and with no root password) You should be able to connect to the network socket using

mysql -u root -h

Then show variables like '%sock%'

will also show you where the filesystem socket is.

Update your php.ini setting, e.g.

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

and restart your webserver.

  • It seems to be running: owen@desktop:~$ ps auxwww | grep mysqld owen 2434 0.0 0.0 11348 868 pts/0 S+ 15:03 0:00 grep --color=auto mysqld – Owen Feb 2 '11 at 14:05
  • I now think that somehow my installation of MySQL has been corrupted or failed, how can I uninstall & reinstall it? – Owen Feb 2 '11 at 14:23

I have the same problem on my box. Try running mysql -h to connect via tcp/ip instead of socket.

  • owen@desktop:~$ mysql -h ERROR 2003 (HY000): Can't connect to MySQL server on '' (111) – Owen Feb 2 '11 at 14:03
  • When PHP tries to connect using I get this diffrent error message: Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 111 in /var/www/testing.php on line 3 Lost connection to MySQL server at 'reading initial communication packet', system error: 111 – Owen Feb 2 '11 at 14:13

Success! I finally got MySQL working by running the commands I found on a similar forum post: http://ubuntuforums.org/showthread.php?t=1373144

sudo mysql_install_db

sudo /etc/init.d/mysql start

sudo dpkg-reconfigure mysql-server-5.1

Thanks for all your help guys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.