Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in

后端 未结 17 1993
长发绾君心
长发绾君心 2020-11-22 08:27

I\'m trying to connect to my MySQL DB with the Terminal on my Apple (With PHP).

Yesterday it worked fine, and now I suddenly get the error in the title.

The

相关标签:
17条回答
  • 2020-11-22 08:55

    You can do it by simply aliasing the MAMP php on Apple terminal:

    alias phpmamp='/Applications/MAMP/bin/php/php7.0.0/bin/php'
    

    Example: > phpmamp - v

    Now you can run something like: > phpmamp scriptname.php

    Note: This will be applied only for the current terminal session.

    0 讨论(0)
  • 2020-11-22 08:56

    When you install php53-mysql using port it returns the following message which is the solution to this problem:

    To use mysqlnd with a local MySQL server, edit /opt/local/etc/php53/php.ini
    and set mysql.default_socket, mysqli.default_socket and
    pdo_mysql.default_socket to the path to your MySQL server's socket file.
    
    For mysql5, use /opt/local/var/run/mysql5/mysqld.sock
    For mysql51, use /opt/local/var/run/mysql51/mysqld.sock
    For mysql55, use /opt/local/var/run/mysql55/mysqld.sock
    For mariadb, use /opt/local/var/run/mariadb/mysqld.sock
    For percona, use /opt/local/var/run/percona/mysqld.sock
    
    0 讨论(0)
  • 2020-11-22 08:57

    Since your might use MAMP, either change your Port to the default 3306 or use 127.0.0.1 in the database.php

    $db['default'] = array(
        'dsn'   => '',
        'hostname' => 'localhost',// leave it for port 3306
        'username' => 'yourUserhere',
        'password' => 'yourPassword',
        'database' => 'yourDatabase',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => FALSE,
        'db_debug' => (ENVIRONMENT !== 'production'),
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE
    );
    

    Or with the default settings:

    $db['default'] = array(
            'dsn'   => '',
            'hostname' => '127.0.0.1:8889',// leave it for port 8889
            'username' => 'yourUserhere',
            'password' => 'yourPassword',
            'database' => 'yourDatabase',
            'dbdriver' => 'mysqli',
            'dbprefix' => '',
            'pconnect' => FALSE,
            'db_debug' => (ENVIRONMENT !== 'production'),
            'cache_on' => FALSE,
            'cachedir' => '',
            'char_set' => 'utf8',
            'dbcollat' => 'utf8_general_ci',
            'swap_pre' => '',
            'encrypt' => FALSE,
            'compress' => FALSE,
            'stricton' => FALSE,
            'failover' => array(),
            'save_queries' => TRUE
        );
    
    0 讨论(0)
  • 2020-11-22 08:59

    When you face the following issue:

    PHP throwing error "Warning: mysql_connect() http://function.mysql-connect: 2002 No such file or directory (trying to connect via unix:///tmp/mysql.sock)"

    Set "mysql.default_socket" value in your /etc/php.ini to

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

    Then restart web service in server admin

    0 讨论(0)
  • 2020-11-22 09:01

    MySQL socket is located, in general, in /tmp/mysql.sock or /var/mysql/mysql.sock, but probably PHP looks in the wrong place.

    1. Check where is your socket with:

       sudo /usr/libexec/locate.updatedb
      
    2. When the updatedb is terminated:

       locate mysql.sock
      
    3. Then locate your php.ini:

       php -i | grep php.ini
      

      this will output something like:

       Configuration File (php.ini) Path => /opt/local/etc/php54
       Loaded Configuration File => /opt/local/etc/php54/php.ini
      
    4. Edit your php.ini

       sudo vim /opt/local/etc/php54/php.ini
      
    5. Change the lines:

       pdo_mysql.default_socket=/tmp/mysql.sock
       mysql.default_socket=/tmp/mysql.sock
       mysqli.default_socket = /tmp/mysql.sock
      

      where /tmp/mysql.sock is the path to your socket.

    6. Save your modifications and exit ESC + SHIFT: x

    7. Restart Apache

       sudo apachectl stop
       sudo apachectl start
      
    0 讨论(0)
  • 2020-11-22 09:01

    I just had this problem, but it only appeared when loading certain pages (other pages worked fine). It turned out that I was making calls to MySQL after I closed the connection with mysql_close(). So, as @brucenan said: make sure that MySQL is running when you call it.

    0 讨论(0)
提交回复
热议问题