简体   繁体   中英

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (13) Ubuntu

I have checked status of mysql by sudo service mysql status and get the following output:

 mysql.service - LSB: Start and stop the mysql database server daemon
   Loaded: loaded (/etc/init.d/mysql; bad; vendor preset: enabled)
   Active: active (exited) since Fri 2016-07-22 15:06:35 IST; 11s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 4946 ExecStart=/etc/init.d/mysql start (code=exited, status=0/SUCCESS

Jul 22 15:06:35 ubuntu-lenovo systemd[1]: Starting LSB: Start and stop the mysql
Jul 22 15:06:35 ubuntu-lenovo systemd[1]: Started LSB: Start and stop the mysql 

But when I try it to run via terminal it says:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (13)

When I try to run with host ie mysql --host 127.0.0.1 -uroot it says:

ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)

I have tried almost every solution from SO.

Simply check your my.cnf (mysql configuration file) and change from

bind-address = 0.0.0.0

to

bind-address = 127.0.0.1

if you don't have that parameter just add it.

Binding to the 0.0.0.0 let your mysql being available on every IP configured cause you can't bind just on two or three IP on the server, the config can be: localhost or everything.

Then check your /etc/hosts file and be sure that the line

127.0.0.1 localhost

Next, restart your mysql

service mysql restart
  1. Find your php.ini file in your system using $ php -i |grep php\\.ini command OR Find Using this Link
  2. Open php.ini file.
  3. And, make sure these lines are present or not.

    a) extension=mysql.so b) extension=pdo_mysql.so

  4. If Yes, remove (;) this before them.
  5. If not present, run this command sudo apt-get install php5-mysql

Now, type php artisan migrate command. I'm sure you will get error

can't connect to local MYSQL server through socket

Now,

  • Change bind-address from localhost to 127.0.0.1
  • Run /opt/lampp/bin/php .
  • After running this if you get

"unable to load dynamic library"

Then, remove php_mssql.dll extension (for non-windows)

  • If not getting error, come directly to Project-Name-Folder/config/database.php file and add this code 'unix_socket' => '/opt/lampp/var/mysql/mysql.sock' ,

Find the complete path of mysql.sock , and add it

'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'database'  => env('DB_DATABASE', 'danishLara'),
    'username'  => env('DB_USERNAME', 'root'),
    'password'  => env('DB_PASSWORD', ''),
    'unix_socket'   => '/opt/lampp/var/mysql/mysql.sock', //Add this line here
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],

This could happen after the homebrew installation and sometimes occurs due to permission issues. The following commands fixed the same problem on my computer.

sudo chown -R _mysql:mysql /usr/local/var/mysql

sudo mysql.server start

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM