sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
# 新增这一行
skip-grant-tables
# 2.重启mysql服务
sudo service mysql restart
# 3.用空密码进入mysql管理命令行,切换到mysql库
mysql
mysql> use mysql
# 4.执行update mysql.user set authentication_string=password('123456') where user='root' and Host ='localhost';把密码重置为123456。退出数据库管理。
mysql> update mysql.user set authentication_string=password('123456') where user='root' and Host ='localhost';
mysql>flush privileges;
mysql>exit
# 5.取消在[mysqld]段下加入一行“skip-grant-tables
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
# 6.重启mysql服务
sudo service mysql restart
2.创建用户
方法1:
# 以下为添加用户的的实例,用户名为guest,密码为guest123,并授权用户可进行 SELECT, INSERT 和 UPDATE操作权限
mysql> use mysql
mysql> INSERT INTO user
(host, user, password,
select_priv, insert_priv, update_priv)
VALUES ('localhost', 'guest',
PASSWORD('guest123'), 'Y', 'Y', 'Y');
mysql> FLUSH PRIVILEGES;
mysql> SELECT host, user, password FROM user WHERE user = 'guest';