一.重置root密码
1.找到my.cnf文件vim编辑,在[mysqld]下新增
skip-grant-tables
保存退出
2.重启mysql
3.使用无密码登录
mysql -uroot
4.选择mysql数据库,将root密码置空
use mysql; update user set authentication_string='' where user='root';
退出
5.编辑my.cnf文件,删除[mysqld]下的skip-grant-tables
# 使用''密码进行登录 mysql -uroot -p
6.重置root密码
use mysql; # 设置为自己的密码 alter user 'root'@'localhost' identified by 'pwd123456'; FLUSH PRIVILEGES;
二.设置安全访问账号
1.创建新的用户账号
# 登录MySQL服务器use mysql; mysql -uroot -p use mysql; # 创建新账号并设置自己的密码并设置只有192.168.1.%的ip可访问,%数值为1-254 create user 'account001'@'192.168.1.%' identified by 'pwd654321'; FLUSH PRIVILEGES; # 修改host的命令,修改时使用 update user set host='192.168.1.%' where user ='account001'; FLUSH PRIVILEGES;
2.控制访问权限
# 按照自己的规则设置权限 GRANT ALL PRIVILEGES ON *.* TO 'account001'@'192.168.1.%'WITH GRANT OPTION;