Could not connect to local MySQL server with Larav

2020-04-19 03:15发布

When I run this command find / -name my.cnf I find two locations for my.cnf

  • /opt/lampp/etc/my.cnf
  • /etc/mysql/my.cnf

I don't get which one is used by MySQL server?

And, when i run command php artisan migrate , I get this error

 [PDOException]                                                              
  SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket ' 
  /var/run/mysqld/mysqld.sock' (2) 

You can all see very clearly the error path showing /var/run/mysqld/mysqld.sock (Actually, in my system/desktop. There is no folder name "mysqld" inside "run folder")

I have a `mysql.sock` file inside /opt/lampp/var/mysql/mysql.sock

So, what can be the issue. ?? Please help. I asked the question here bind-address not present in my.cnf file - Laravel 3 hours before, but no response until now.

3条回答
再贱就再见
2楼-- · 2020-04-19 03:36

Make sure that MySQL is running first (simple, but good to check!), and make sure that the correct install of MySQL is being run (if you have a LAMP install as well as a standalone MySQL install).

If you are connecting using localhost, try connecting using 127.0.0.1. This may use a TCP/IP connector instead of the socket - just a workaround.

查看更多
家丑人穷心不美
3楼-- · 2020-04-19 03:37

I think you have to uninstall lamp , it's better to use a manual configuration install mysql and apache2 manualy don't use lamp !

查看更多
趁早两清
4楼-- · 2020-04-19 04:01

First of all,

  • Find your php.ini file in your system using $ php -i |grep php\.ini command OR click where can I find the php.ini for php-cli.
  • Open php.ini file.
  • And, make sure these lines are present or not.

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

  • If Yes, remove (;) this before them.
  • 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,
],
查看更多
登录 后发表回答