Hi,

I'm getting the error message at the bottom of this posting when trying to connect to MySQL. Line 9 of my program is as follows:

$db = mysql_connect("localhost", "graham", "password");

the error produced is:

Warning: MySQL Connection Failed: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111) in /home/sites/site1/web/test/testpage.php on line 9

Any assistance would be very much appreciated.

regards, Graham

    make sure the socket file exists at '/var/lib/mysql/mysql.sock' and also try to give all permissions to the file. the file is only used to connect with mysql so it shouldn't be a huge security risk as other users would still need to authenticate with mysql.

    brandon

      hi
      the only thing u need to do is just start ur mysql deamon

      if on linux u can
      /etc/rc.d/init.d/mysql start

      or windows
      c:/mysql/bin/mysqld.exe

      this will work
      if any problems let me know

      cheers :-)
      mandar@freeos.com

        Hi,

        There was no file mysql.sock at /var/lib/mysql/mysql.sock. mysql.sock lived at /tmp/mysql.sock where it does on our other (intranet) mysql installation. So, I copied it to /var/lib/mysql/mysql.sock but get the same error message as before. This is very frustrating. Does anyone have any further suggestions please? I've double-checked the mysql is running, by using 'mysqladmin version' this morning. It was still running fine. I just can't connect to it with PHP as yet. Doh!!!

        Graham

          if you can access mysqld using the mysql client application make sure that the user that php is running as has access to the socket file in /var/lib/mysql.

          so if php is compiled as an apache module it'll most likely be running running as user nobody/nobody. try:
          chmod +ox /var/lib/mysql
          to give user nobody access to the mysql folder (and the new mysql socket file).

          make sure that the databases are owned by the mysql user/group, and setup permissions on the databases folders so that only mysqld has rwx permissions to the database folders.

          check your permissions on the mysqld log as it can contain username/passwords.

          rather than copying the socket file you might want to create a /etc/my.cnf file and tell mysqld to create the socket file in /var/lib/mysql/. Or modify the mysqld startup/shutdown script and specify the socket file location using the --socket=/var/lib/mysql/mysql.sock option.

            Hi Folks:

            I have been "lurking" on this thread as I was at exactly the same problem point that you are/were at, Graham.

            Thanks, Brandon, as your point below solved two weeks of frustration (part time working in my server):

            "if php is compiled as an apache module it'll most likely be running running as user nobody/nobody.

            try: chmod +ox /var/lib/mysql to give user nobody access to the mysql folder (and the new mysql socket file). "

            After I ran this command it worked AWESOMELY

            Thanks, again, Brandon. It's people like you who volunteer their expertise that makes the 'net and open source systems great.

            Paul

              I have the same problem. I have read and done all the suggestions offered by Brandon, Paul, Mandar et al. Alas, I am still not up and running. Here is what I got.

              mysql.sock exists and looks like this :
              srwxrwxrwx 1 mysql mysql /var/lib/mysql/mysql.sock

              Running /etc/rc.d/init.d/myslqd start & returns [ OK ] every time without any other messages.

              Running /etc/rc.d/init.d/myslqd status returns
              mysqld dead but subsys locked

              Running /etc/rc.d/init.d/myslqd stop returns [ FAILED ] every time without any other messages.

              Running /usr/bin/safe_mysqld returns
              Starting mysqld daemon with database from /usr/lib/mysql
              010406 13:03:59 mysqld ended

              /usr/bin/mysqladmin status or /usr/bin/mysqladmin shutdown both return
              ERROR: 'Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)'
              Check that mysqld is running and that the socket '/var/lib/myslq/mysql.sock' exists.

              Nothing gives me access to mysql through PHP. Also, there is no 'mysql' or 'mysqld' listed with a "ps -A"

              Any good pointers left out there? Thanks in advance... Gary

                Hi Gary:

                Here are some e-mails from someone who helped us:

                "it was all about permissions...
                
                from the command line can you get into mysql
                
                > mysql -p
                password
                
                mysql>
                ~~~~~~~~~~~~~~~~~~~~~~~~~
                Very Simple...
                
                there is a database in mysql called mysql
                
                select * from user;
                
                sets permissions...
                
                
                telnet into port 3306 on your local machine and tell me what the error is
                
                > telnet 127.0.0.1 3306
                
                This will help you see if it is accessible locally.
                
                ~~~~~~~~~~~~~~~~~~~~~~~~~
                you want to set your permissions within mysql.
                
                from the mysql prompt
                
                mysql>grant all on databasename.* to usename identified by "userpassword"
                
                this line allows that user to have full control over the database from
                anywhere
                
                mysql>grant select,update,delete,insert on databasename.tablename to
                username.localhost identified by "userpassword"
                
                more secure can't drop the database or its tables and only allows the user to connect from the localhost.
                
                The way you did it anyone can connect and have full control over your
                databases, even opening up security holes for any FTP user etc.
                
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                postings courtesy of Kurt Simons  (kurt@kurtsimons.com)
                
                I hope this helps, Gary.  
                
                Cheers,
                
                Paul

                  mysql isn't starting.

                  1) as root clear the lock file /var/lock/subsys/mysql

                  2) restart mysql (/etc/rc.d/init.d/myslqd)

                  3) look at the logs in /var/lib/mysql/*.err

                  4) make sure the directory /var/lib/mysql/ is accessible also ("chmod o+rx /var/lib/mysql/").

                    a month later

                    Where do you type this in? At the regular shell prompt? In the mysql program? I'm new to this. Right now, the mysql.sock file lives in the /home/mysql directory on my server, but the .php file is looking for it in the /tmp directory. What would I need to do and where?

                    Thanks in advance.

                      Hi Kelli:

                      I understand that this must be a pain. I hadn't used a command line prompt for admin stuff since back in the DOS days...it takes a while to get used to. No easy way around it. After a few months of Linux work, it becomes quite easy and powerful.

                      If you are trying to "type this stuff in", and you mean changing the permissions of the directory ("chmod"), then go to the directory right above the one which you want to change the directory of. Then change the permissions as Brandon notes by typing in the "chmod..." command. This allows PHP to work with (read, write, execute programs in) the contents of the directory - in this case the MySQL directory that it needs to access.

                      If you are unsure of how to work with command line prompts in Linux, grab any good Linux or Unix book and get a quick primer on moving around and doing stuff from the command line.

                      Good luck. Once you have the whole thing running, this problem will just be a "blip" in the past...

                      Paul

                        if php is looking for the socket in /tmp then change the php.ini file value for mysql.default_socket to "/home/mysql/mysql.sock".

                        if you have problems updating the file make a symbolic link from /home/mysql/mysql.sock to /tmp/mysql.sock.

                        at the command prompt:
                        ln -s /tmp/mysql.sock /home/mysql/mysql.sock

                          Great! Thanks, it worked.

                            a month later

                            I just ran across this discussion after spending 5 hrs trying to fix the same problem. Its done, I can sleep now.

                            Thanks a ton guys.

                            -Hrishi

                              17 days later

                              Found something that was interesting and might help you in future.

                              I had phpMyAdmin 2.1.0 giving me similar trouble...

                              The fix in the end combined creating a symlink to the correct location of mysql.sock

                              +

                              changing the script hostname to the actual IP...

                              I only worked it out by mistake when I used the IP in the browser instead of the servers name.

                              Hope that helps someone
                              🙂
                              Martin

                                11 days later

                                I am getting the exact error message has Gary Kiser. I have read all the responses in this thread and no one has solved the problem only Brandon Schnell came close to it.

                                I have been on this whole night, I was connecting but coudn't authenticate so I re-installed mysql, now it's not connecting at all. and I am stuck.

                                  Somebody please help me out.. mysqld isn't running and I don't know what to do from here. I am new too to mysql and linux.. It's just giving me the same message.
                                  "mysqld dead but subsys locked" why? why? why? aaaaaaaarggggggggggg!!!

                                    Lot's of mails coming in my inbox from phpbuilder.com why is it?

                                      Well I fixed the proble. Took me the whole night last night. But today what I did was uninstall the mysql that i had 3.23.26 and installed the latest version from www.mysq.com version 3.23.39 I think it was.. much better version and it placed the mysqld in the /usr/sbin/ directory rather than the old /etc/rc.d/init.d/ path.. this is a really cool version you guys need to try this one out.

                                      Oh btw I installed the RPM for RedHat 7.1 and it's cool, no-prob-mon..

                                      Now i'm gonna try to see if it to start/stop on boot/shutdown.

                                      later guys..

                                        7 months later

                                        If anyone is still having problems try this.

                                        assuming mysql.mysql owns all the db files in /var/lib/mysql and all commands are type from / and are logged in as root or have su to root.

                                        if not: su root yourpassword

                                        (so that mysql can create the domain sock file mysql.sock and so that php as a module of apache can access mysqld's mysql.sock.)

                                        You see the thing is, if /var and /var/lib are set to 0700 then only root can read the mysql files, and if the user 'mysql' can't read the the /var or /var/lib directory's it can't find the /var/lib/mysql directory and therefore it can't create the domain socket /var/lib/mysql/mysql.sock

                                        So to make it so, you have to give 'other' (the fourth digit) read access, and ofcourse execute access to 'cd' the directory. Without execute access on the directorys, mysqld run as mysql, cannot go: cd /var/lib/mysql... so to speak.

                                        cd /
                                        chmod 0755 /var
                                        chmod 0755 /var/lib
                                        chmod 0755 /var/lib/mysql
                                        chown -R mysql.mysql /var/lib/mysql

                                        also this helps:
                                        ( so that mysql can create the file mysqld.pid)

                                        chmod 0755 /var/run/mysqld
                                        chown -R mysql.mysql /var/run/mysqld

                                        you can view the directory permission by doing this:
                                        ls -ald /var/lib/mysql
                                        -> drwxr-xr-x ## mysql mysql 4096 Feb 8 07:26 /var/lib/mysql

                                        and: ls -al -> to view the permissions of files in the directory.

                                        common access masks on files and directories.
                                        0700 : drwx------ <-- not good for this purpose
                                        only the user can view the contents of this directory, write or execute
                                        group and other are just screwed.

                                        0755 : drwxr-xr-x <-- what you want
                                        ( this is a directory, user can read, write, execute on to this directory, group and other can read and execute(cd ...), to this directory

                                        0660 : -rw-rw---- common data file setting
                                        ( user and group can read+write to this file, but other is screwed)

                                        I hope that helps! (otherwise I have had some good typing practice)

                                        Cheers,

                                        Jade