bind-address not present in my.cnf file - Laravel

2019-01-28 14:08发布

问题:

While running this command 'php artisan migrate', i got this error "couldn't connect to local MYSQL server through socket".

I was going through all stackoverflow answers, i got many suggestions saying 'change bind-address:localhost to 127.0.0.1'.

I entered this command 'find / -name my.cnf' and got two locations of 'my.cnf' file.

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

I've confusion.. Which file i've to edit. Which one is correct path ?

And, in my "my.cnf" file, no 'bind-address' is present. If i write manually, so where to write "bind-address" in my.cnf file ? Or, anywhere i can write in that file.?

Here is 'my.cnf' file

# The following options will be passed to all MySQL clients
[client]
#password   = your_password
port        = 3306
socket      = /opt/lampp/var/mysql/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
user = mysql
port=3306
socket      = /opt/lampp/var/mysql/mysql.sock
skip-external-locking
key_buffer = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M

# Where do all the plugins live
plugin_dir = /opt/lampp/lib/mysql/plugin/

Please Help Me.

回答1:

I made changes in "Project-Folder-Name/config/database.php". And, it worked. Added 'unix_socket' => '/opt/lampp/var/mysql/mysql.sock',

'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'DatabaseName'),
            'username'  => env('DB_USERNAME', 'root'),
            'password'  => env('DB_PASSWORD', ''),
        'unix_socket'   => '/opt/lampp/var/mysql/mysql.sock', //Your sock got from above
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],


回答2:

verify your mysql service is running..try to

mysql service start or mysql service restart

Or you can checkout some of the suggestion given here Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)