查看版本
mysql --version
修改配置文件
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
打开后,按“i”进入insert模式,添加语句 skip-grant-tables
[mysqld]
#
# * Basic Settings
#
user = mysql
# pid-file = /var/run/mysqld/mysqld.pid
# socket = /var/run/mysqld/mysqld.sock
# port = 3306
# datadir = /var/lib/mysql
skip-grant-tables //添加这一行
按“ESC”退出输入模式,并输入“:wq”保存并退出。
重新启动mysql
service mysql restart
mysql -u root -p
直接按enter键进入mysql,无需输入密码
mysql> use mysql;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '自己的密码';
mysql>flush privileges;
退出
mysql> quit;
重新打开配置文件,将之前添加的一行语句删除
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
完成!
如果过程中遇到下面错误:
错误一:
ERROR 1524 (HY000): Plugin 'auth_socket' is not loaded
解决:
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start
错误二:
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
解决:
flush privileges