今天執行mysql操作的時候出現了錯誤:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'問題
1:首先檢查是否安裝了mysql-server了
sudo apt-get install mysql-server to install mysql on ubuntu, 如果mysql-server已經存在了,那么在去嘗試一下是否成功,如果還是這個錯誤的話那么就是文件/var/run/mysqld/mysqld.sock 不存在。
2:如果/var/run/mysqld/mysqld.sock文件不存在應該怎么辦?
執行 vim /etc/mysql/my.cnf
將會看到內容如下:
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
#
# * IMPORTANT: Additional settings that can override those from this file!
# The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
你的mysql的配置在/etc/mysql/mysql.conf.d目錄下的mysqld.cnf文件,打開如下:
可以看到:bind-address = 127.0.0.1 socket = /var/run/mysqld/mysqld.sock 等信息
我的虛擬機是/var/run下面沒有mysqld目錄,執行下面命令,然后目錄和sock文件就都有了
sudo mkdir -p /var/run/mysqld
sudo chown mysql /var/run/mysqld/
sudo service mysql restart
現在就都有了:
然后執行:mysql -u root -p 然后提示你輸入密碼,即可, 如下圖所示:
就可以了
---------------------
作者:mellymengyan
來源:CSDN
原文:https://blog.csdn.net/mellymengyan/article/details/78958486
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!