Cannot get mysql_connect to work

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael

    Cannot get mysql_connect to work

    Hi,

    I'm trying to do something which should be very simple - connect to the
    MySQL database. Here is the call, followed by the error msg.

    $conn = mysql_connect(" localhost", "root", "");

    Warning: mysql_connect() : Can't connect to local MySQL server through
    socket '/var/lib/mysql/mysql.sock' (13) in /var/www/html/buildhw.php on
    line 13
    failed

    I've also tried this with the host arg as "", but I get the same
    result. Logging on as root made no difference either.

    Any help would be very much appreciated.

    Thanks,
    M. McDonnell

  • David Haynes

    #2
    Re: Cannot get mysql_connect to work

    Michael wrote:[color=blue]
    > Hi,
    >
    > I'm trying to do something which should be very simple - connect to the
    > MySQL database. Here is the call, followed by the error msg.
    >
    > $conn = mysql_connect(" localhost", "root", "");
    >
    > Warning: mysql_connect() : Can't connect to local MySQL server through
    > socket '/var/lib/mysql/mysql.sock' (13) in /var/www/html/buildhw.php on
    > line 13
    > failed
    >
    > I've also tried this with the host arg as "", but I get the same
    > result. Logging on as root made no difference either.
    >
    > Any help would be very much appreciated.
    >
    > Thanks,
    > M. McDonnell
    >[/color]

    You may want to adjust your php.ini for mysql.
    Look for [MySQL] and then the lines like this:

    ; Default socket name for local MySQL connects. If empty, uses the built-in
    ; MySQL defaults.
    mysql.default_s ocket =

    I suspect your mysql has been compiled not to have the socket file in
    /tmp/mysql.sock. In fact, it appears to be in /var/lib/mysql/mysql.sock.
    A quick check of /etc/my.cnf will tell you where it should be.

    -david-

    Comment

    • Michael

      #3
      Re: Cannot get mysql_connect to work

      David,

      Thank you for your help. The file /etc/my.cnf is shown below. It looks
      like the default location is correct in the sense that the file
      /var/lib/mysql/mysql.sock does exist (whether it contains the correct
      information, I cannot tell).

      Also, please not that I can connect to MySQL through the MySQL monitor
      by typing "mysql".

      Do you, or anyone else have any other suggestions? I think I'm stuck at
      this point...

      mysqld]
      datadir=/var/lib/mysql
      socket=/var/lib/mysql/mysql.sock

      [mysql.server]
      user=mysql
      basedir=/var/lib

      [safe_mysqld]
      err-log=/var/log/mysqld.log
      pid-file=/var/run/mysqld/mysqld.pid

      Comment

      • David Haynes

        #4
        Re: Cannot get mysql_connect to work

        Michael wrote:[color=blue]
        > David,
        >
        > Thank you for your help. The file /etc/my.cnf is shown below. It looks
        > like the default location is correct in the sense that the file
        > /var/lib/mysql/mysql.sock does exist (whether it contains the correct
        > information, I cannot tell).
        >
        > Also, please not that I can connect to MySQL through the MySQL monitor
        > by typing "mysql".
        >
        > Do you, or anyone else have any other suggestions? I think I'm stuck at
        > this point...
        >
        > mysqld]
        > datadir=/var/lib/mysql
        > socket=/var/lib/mysql/mysql.sock
        >
        > [mysql.server]
        > user=mysql
        > basedir=/var/lib
        >
        > [safe_mysqld]
        > err-log=/var/log/mysqld.log
        > pid-file=/var/run/mysqld/mysqld.pid
        >[/color]

        What does the [MySQL] block in your php.ini file look like?

        -david-

        Comment

        • David Haynes

          #5
          Re: Cannot get mysql_connect to work

          Michael wrote:[color=blue]
          > David,
          >
          > Thank you for your help. The file /etc/my.cnf is shown below. It looks
          > like the default location is correct in the sense that the file
          > /var/lib/mysql/mysql.sock does exist (whether it contains the correct
          > information, I cannot tell).
          >
          > Also, please not that I can connect to MySQL through the MySQL monitor
          > by typing "mysql".
          >
          > Do you, or anyone else have any other suggestions? I think I'm stuck at
          > this point...
          >
          > mysqld]
          > datadir=/var/lib/mysql
          > socket=/var/lib/mysql/mysql.sock
          >
          > [mysql.server]
          > user=mysql
          > basedir=/var/lib
          >
          > [safe_mysqld]
          > err-log=/var/log/mysqld.log
          > pid-file=/var/run/mysqld/mysqld.pid
          >[/color]

          Another question:
          Are you running php standalone or via a web server?

          -david-

          Comment

          • Michael

            #6
            Re: Cannot get mysql_connect to work

            David,

            The MySQL block in php.ini is shown below. I do have another piece of
            information, and that is that the following call does work:

            $conn = mysql_connect(" 127.0.0.1", "mike", ""); // This works.

            I still don't understand why "locahost" doesn't work, though (probably
            because I don't fully understand what "locahost" actually refers to).

            Thanks for any help you can give.

            M. McDonnell




            ==============p hp.ini MySQL block follows======== ===========

            [MySQL]
            ; Allow or prevent persistent links.
            mysql.allow_per sistent = On

            ; Maximum number of persistent links. -1 means no limit.
            mysql.max_persi stent = -1

            ; Maximum number of links (persistent + non-persistent). -1 means no
            limit.
            mysql.max_links = -1

            ; Default port number for mysql_connect() . If unset, mysql_connect()
            will use
            ; the $MYSQL_TCP_PORT or the mysql-tcp entry in /etc/services or the
            ; compile-time value defined MYSQL_PORT (in that order). Win32 will
            only look
            ; at MYSQL_PORT.
            mysql.default_p ort =

            ; Default socket name for local MySQL connects. If empty, uses the
            built-in
            ; MySQL defaults.
            mysql.default_s ocket =

            ; Default host for mysql_connect() (doesn't apply in safe mode).
            mysql.default_h ost =

            ; Default user for mysql_connect() (doesn't apply in safe mode).
            mysql.default_u ser =

            ; Default password for mysql_connect() (doesn't apply in safe mode).
            ; Note that this is generally a *bad* idea to store passwords in this
            file.
            ; *Any* user with PHP access can run 'echo
            get_cfg_var("my sql.default_pas sword")
            ; and reveal this password! And of course, any users with read access
            to this
            ; file will be able to reveal the password as well.
            mysql.default_p assword =

            ; Maximum time (in seconds) for connect timeout. -1 means no limit
            mysql.connect_t imeout = 60

            ; Trace mode. When trace_mode is active (=On), warnings for table/index
            scans and
            ; SQL-Errors will be displayed.
            mysql.trace_mod e = Off

            Comment

            • Jerry Stuckle

              #7
              Re: Cannot get mysql_connect to work

              Michael wrote:[color=blue]
              > David,
              >
              > The MySQL block in php.ini is shown below. I do have another piece of
              > information, and that is that the following call does work:
              >
              > $conn = mysql_connect(" 127.0.0.1", "mike", ""); // This works.
              >
              > I still don't understand why "locahost" doesn't work, though (probably
              > because I don't fully understand what "locahost" actually refers to).
              >
              > Thanks for any help you can give.
              >
              > M. McDonnell
              >
              >
              >
              >
              > ==============p hp.ini MySQL block follows======== ===========
              >
              > [MySQL]
              > ; Allow or prevent persistent links.
              > mysql.allow_per sistent = On
              >
              > ; Maximum number of persistent links. -1 means no limit.
              > mysql.max_persi stent = -1
              >
              > ; Maximum number of links (persistent + non-persistent). -1 means no
              > limit.
              > mysql.max_links = -1
              >
              > ; Default port number for mysql_connect() . If unset, mysql_connect()
              > will use
              > ; the $MYSQL_TCP_PORT or the mysql-tcp entry in /etc/services or the
              > ; compile-time value defined MYSQL_PORT (in that order). Win32 will
              > only look
              > ; at MYSQL_PORT.
              > mysql.default_p ort =
              >
              > ; Default socket name for local MySQL connects. If empty, uses the
              > built-in
              > ; MySQL defaults.
              > mysql.default_s ocket =
              >
              > ; Default host for mysql_connect() (doesn't apply in safe mode).
              > mysql.default_h ost =
              >
              > ; Default user for mysql_connect() (doesn't apply in safe mode).
              > mysql.default_u ser =
              >
              > ; Default password for mysql_connect() (doesn't apply in safe mode).
              > ; Note that this is generally a *bad* idea to store passwords in this
              > file.
              > ; *Any* user with PHP access can run 'echo
              > get_cfg_var("my sql.default_pas sword")
              > ; and reveal this password! And of course, any users with read access
              > to this
              > ; file will be able to reveal the password as well.
              > mysql.default_p assword =
              >
              > ; Maximum time (in seconds) for connect timeout. -1 means no limit
              > mysql.connect_t imeout = 60
              >
              > ; Trace mode. When trace_mode is active (=On), warnings for table/index
              > scans and
              > ; SQL-Errors will be displayed.
              > mysql.trace_mod e = Off
              >[/color]

              Micheal,

              localhost refers to the machine itself, via the loopback adapter
              (127.0.0.1). You should see in your /etc/hosts file something similar to:

              127.0.0.1 localhost

              Ensure that's there; if not mysql won't be your only failure.


              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • Michael

                #8
                Re: Cannot get mysql_connect to work

                Jerry,

                Thanks for your help. I guess I'm a little confused about localhost. It
                seems that if I open a file in the browser at
                http:/localhost/myFile.html, it actually refers to the file
                /var/www/html/myFile.html. So it seems there are two meanings to
                localhost? In one context it refers to the local machine via ip
                address 127.0.0.1, and in the context of the browser, (with Apache
                running?), localhost refers to a specifc directory path on the local
                machine. This is why I'm confused. Can you shed any light on this?

                Thanks,
                M. McDonnell
                P.S., my /etc/host file looks ok (see below).
                =============== =/etc/host file =============== =
                Do not remove the following line, or various programs
                # that require network functionality will fail.
                127.0.0.1 McDonnell localhost.local domain localhost
                =============== =============== ============

                Comment

                • Jerry Stuckle

                  #9
                  Re: Cannot get mysql_connect to work

                  Michael wrote:[color=blue]
                  > Jerry,
                  >
                  > Thanks for your help. I guess I'm a little confused about localhost. It
                  > seems that if I open a file in the browser at
                  > http:/localhost/myFile.html, it actually refers to the file
                  > /var/www/html/myFile.html. So it seems there are two meanings to
                  > localhost? In one context it refers to the local machine via ip
                  > address 127.0.0.1, and in the context of the browser, (with Apache
                  > running?), localhost refers to a specifc directory path on the local
                  > machine. This is why I'm confused. Can you shed any light on this?
                  >
                  > Thanks,
                  > M. McDonnell
                  > P.S., my /etc/host file looks ok (see below).
                  > =============== =/etc/host file =============== =
                  > Do not remove the following line, or various programs
                  > # that require network functionality will fail.
                  > 127.0.0.1 McDonnell localhost.local domain localhost
                  > =============== =============== ============
                  >[/color]

                  No, in either case it's referring to the local machine.

                  If your domain is example.com, http://www.example.com/myFile.html would
                  also refer to the file /var/www/html/myFile.html. Here you're accessing
                  your machine through the web browser from an external source; if you
                  call http://localhost/myFile.html you're calling the web browser from
                  the same machine.

                  localhost always refers to the machine itself. It's the web browser
                  which is translating the directory.

                  And if you have the appropriate daemons running, you can

                  telnet localhost
                  ftp localhost
                  ping localhost
                  etc.

                  which does exactly the same things as

                  telnet example.com
                  ftp example.com
                  ping example.com

                  The only difference is the first ones you're calling from the local
                  machine; for the second group you're calling from either the same or a
                  different machine.


                  --
                  =============== ===
                  Remove the "x" from my email address
                  Jerry Stuckle
                  JDS Computer Training Corp.
                  jstucklex@attgl obal.net
                  =============== ===

                  Comment

                  • Gordon Burditt

                    #10
                    Re: Cannot get mysql_connect to work

                    >Thanks for your help. I guess I'm a little confused about localhost. It

                    localhost refers to the local machine. The *whole* machine, not
                    just one directory on it.
                    [color=blue]
                    >seems that if I open a file in the browser at
                    >http:/localhost/myFile.html, it actually refers to the file
                    >/var/www/html/myFile.html.[/color]

                    On the local machine.

                    If I ftp to localhost and log in as 'gordon', I end up in my home
                    directory on the local machine. If I ftp to localhost and log in
                    as 'ralph', I end up in Ralph's home directory on the local machine.
                    If I http to localhost and request /, I get the web server's document
                    root directory (or an index file in it). If I ping localhost, I
                    get *no directory whatever*, I just get ping replies back. If I
                    log in on MySQL to localhost, I get the database I asked for, and
                    who cares what directory it's kept in (sometimes databases are
                    jumbled together in one large file, but if MySQL can figure it out,
                    that's fine with me) if I get the right one? If I connect an IRC
                    client to localhost, I end up talking to the IRC server on my local
                    machine (assuming one exists), and the directory is not relevant.
                    [color=blue]
                    >So it seems there are two meanings to
                    >localhost? In one context it refers to the local machine via ip
                    >address 127.0.0.1, and in the context of the browser, (with Apache
                    >running?), localhost refers to a specifc directory path on the local
                    >machine. This is why I'm confused. Can you shed any light on this?[/color]

                    localhost refers to the local machine. The *whole* local machine.
                    Any directory is implied or specified by parts of the request other
                    than the hostname.

                    Gordon L. Burditt

                    Comment

                    • Michael

                      #11
                      Re: Cannot get mysql_connect to work

                      Jerry and Gordon,

                      Thank you for taking the time to write out these superb explanations; I
                      and I'm sure others appreciate it. Just a few more question, if I
                      may...

                      1. How does the browser know that it should translate
                      http:/localhost/myFile.html into /var/www/html/myFile.html? Is there an
                      ini file somewhere that contains this information?

                      2. This thread got started because the first mysql call (see below)
                      failed, but when I changed localhost to 127.0.0.1, as in the second
                      call, it worked. Given the information I have from you now, I don't see
                      how the first call could ever work, although the documentation says
                      that it should. Any ideas?

                      $conn = mysql_connect(" localhost", "root", ""); // This fails.
                      $conn = mysql_connect(" 127.0.0.1", "mike", ""); // This works.

                      Thanks again for your help,
                      M. McDonnell

                      Comment

                      • Gordon Burditt

                        #12
                        Re: Cannot get mysql_connect to work

                        >Thank you for taking the time to write out these superb explanations; I[color=blue]
                        >and I'm sure others appreciate it. Just a few more question, if I
                        >may...
                        >
                        >1. How does the browser know that it should translate[/color]

                        It doesn't. The WEB SERVER translates. The browser should not
                        have access to this information (and I would consider it a security
                        problem), nor does it need it.
                        [color=blue]
                        >http:/localhost/myFile.html into /var/www/html/myFile.html? Is there an
                        >ini file somewhere that contains this information?[/color]

                        You configure a web server for a document root (that's what Apache
                        calls it. You can run Apache on Windows. On UNIX, this is probably
                        set in httpd.conf or a file included by it.). The document root
                        and its subdirectories contain the web pages to serve. IIS certainly
                        has a similar concept that you can configure somewhere. I don't
                        do Windows so I have no idea where this is.
                        [color=blue]
                        >2. This thread got started because the first mysql call (see below)
                        >failed, but when I changed localhost to 127.0.0.1, as in the second
                        >call, it worked. Given the information I have from you now, I don't see
                        >how the first call could ever work, although the documentation says
                        >that it should. Any ideas?[/color]

                        MySQL (specifically, the client library) uses local UNIX-domain (at
                        least on UNIX) sockets (created by the server) when you connect to
                        "localhost" . (This string is recognized specially, whether or not
                        looking it up in the hosts file yields "127.0.0.1" for the IP
                        address.) This will have problems if your MySQL client (e.g. PHP)
                        and your MySQL server (mysqld) disagree over the location of the
                        socket. The fix here is to get them to agree on the location of
                        the socket (may be fixable in php.ini).

                        MySQL uses Internet-domain sockets when you connect to an IP address.
                        There is no issue of the path name to a socket that can be
                        misconfigured: you don't get to specify one, and there may not be
                        any such socket on the local MySQL client machine (the one running
                        PHP and the web server).
                        [color=blue]
                        >$conn = mysql_connect(" localhost", "root", ""); // This fails.
                        >$conn = mysql_connect(" 127.0.0.1", "mike", ""); // This works.[/color]

                        Gordon L. Burditt

                        Comment

                        • Michael

                          #13
                          Re: Cannot get mysql_connect to work

                          This thread was too long for me to bother reading. Before when I wasn't
                          using XAMPP <http://apachefriends.o rg> I had the same kind of problems
                          so i found all files like mysql.dll or something simular and placed
                          them in every directory i thought might make it work and it did, so if
                          you don't get it working then try that.

                          Comment

                          • David Haynes

                            #14
                            Re: Cannot get mysql_connect to work

                            Michael wrote:[color=blue]
                            > Jerry,
                            >
                            > Thanks for your help. I guess I'm a little confused about localhost. It
                            > seems that if I open a file in the browser at
                            > http:/localhost/myFile.html, it actually refers to the file
                            > /var/www/html/myFile.html. So it seems there are two meanings to
                            > localhost? In one context it refers to the local machine via ip
                            > address 127.0.0.1, and in the context of the browser, (with Apache
                            > running?), localhost refers to a specifc directory path on the local
                            > machine. This is why I'm confused. Can you shed any light on this?
                            >
                            > Thanks,
                            > M. McDonnell
                            > P.S., my /etc/host file looks ok (see below).
                            > =============== =/etc/host file =============== =
                            > Do not remove the following line, or various programs
                            > # that require network functionality will fail.
                            > 127.0.0.1 McDonnell localhost.local domain localhost
                            > =============== =============== ============
                            >[/color]
                            Michael,
                            The URL http://localhost/myFile.html is composed of a number of sections:
                            'http:' indicates the protocol to be used (another example would the svn:)
                            'localhost' (or any fully qualified name) indicates the Internet Address
                            of the server that browser requests should be sent to. Sometimes there
                            is also a port associated with the address (e.g. localhost:8000) . The
                            default ports are 80 for http protocol and 443 for https protocol.

                            The name 'localhost' or 'www.foo.com' is simply an alias for an Internet
                            address in the form aaa.bbb.ccc.ddd (assuming IPv4). So, your browser
                            needs to be able to translate www.foo.com into a meaningful
                            aaa.bbb.ccc.ddd value.

                            Your system has a file (/etc/hosts) that maps unregistered names (e.g.
                            localhost) to IP addresses (e.g. 127.0.0.1)

                            So, by the time your browser is done with it, the URL
                            http://localhost/myFile.html has been translated to 'use http protocol',
                            talking to 127.0.0.1 port 80, and ask for the 'myFile.html' file.

                            Now, your web server has a concept of a document root, that is the
                            directory where all the web pages and supporting files on kept. On your
                            system, this is set to /var/www/html. So when the server sees the
                            browser's request for the file 'myFile.html' is translates this to 'send
                            back the contents of file '/var/www/html/myFile.html'.

                            Another thing about IP aliases. You notice that I said 'unregistered
                            names'. That is because names like 'www.ibm.com' are registered to be
                            universally (sometimes called globally) unique and are defined in a
                            world-wide database know as the 'name service'.

                            If you want to know more about this side of things, check out programs
                            like 'bind' and files like '/etc/resolv.conf'.

                            -david-

                            Comment

                            • Jerry Stuckle

                              #15
                              Re: Cannot get mysql_connect to work

                              Michael wrote:[color=blue]
                              > Jerry and Gordon,
                              >
                              > Thank you for taking the time to write out these superb explanations; I
                              > and I'm sure others appreciate it. Just a few more question, if I
                              > may...
                              >
                              > 1. How does the browser know that it should translate
                              > http:/localhost/myFile.html into /var/www/html/myFile.html? Is there an
                              > ini file somewhere that contains this information?
                              >
                              > 2. This thread got started because the first mysql call (see below)
                              > failed, but when I changed localhost to 127.0.0.1, as in the second
                              > call, it worked. Given the information I have from you now, I don't see
                              > how the first call could ever work, although the documentation says
                              > that it should. Any ideas?
                              >
                              > $conn = mysql_connect(" localhost", "root", ""); // This fails.
                              > $conn = mysql_connect(" 127.0.0.1", "mike", ""); // This works.
                              >
                              > Thanks again for your help,
                              > M. McDonnell
                              >[/color]

                              Michael,

                              The browser doesn't know - and doesn't care. This is all done by the
                              web server (Apache, IIS, Tomcat, etc.), based on settings in the browser.

                              As for your problem - you've changed your signon id, also. In localhost
                              it is "root", but when you connect to 127.0.0.1, you use "mike".

                              In neither case do you have a password listed - so if "root" has no
                              password (very bad idea), it should work also.

                              Remember - the internet works only on ip addresses. It doesn't know
                              anything about "localhost" , "example.co m" or whatever. But your HOSTS
                              file should provide the translation from "localhost" to 127.0.0.1 for you.

                              --
                              =============== ===
                              Remove the "x" from my email address
                              Jerry Stuckle
                              JDS Computer Training Corp.
                              jstucklex@attgl obal.net
                              =============== ===

                              Comment

                              Working...