根据目录/etc/mysql打开文件debain.cnf

此时文本里的内容为

# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = C6V6q9klPIBtAzTq
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = C6V6q9klPIBtAzTq
socket = /var/run/mysqld/mysqld.sock

再打开my.cnf,此时该本文的内容为

[client]
port=3306
socket=/tmp/mysql/mysql.sock

[mysqld]
port=3306
user=mysql
socket=/tmp/mysql/mysql.sock
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
log-error=error.log

然后将my.cnf里两行代码

socket=/tmp/mysql/mysql.sock

改为

socket = /var/run/mysqld/mysqld.sock

即可

最后重新启动数据库

在终端输入

service mysql restart

然后

mysql -uroot -p

最后将debain.cnf

里的密码输入即可