一、用户管理

默认:用户root 创建用户: use mysql; create user 'alex'@'192.168.193.200' identified by '123456'; 创建了alex用户,密码为123456,只能在ip192.168.193.200上连接 create user 'egon'@'192.168.193.%' identified by '123456'; 创建了egon用户,密码为123456,只能在ip192.168.193.上连接 create user 'xuxu'@'%' identified by '123456'; 创建了xuxu用户,密码为123456,能在任何ip上连接 删除用户: drop user 'alex'@'%'; 修改密码: set password for ‘alex’@’%’=password(‘新密码’) 对用户进行相关操作后记得执行命令 flush privileges; 查看用户: select user,host from user;
二、权限管理

授权: 权限 人 #针对所有库的授权:*.* grant select on *.* to 'egon1'@'localhost' identified by '123'; #只在user表中可以查到egon1用户的select权限被设置为Y #针对某一数据库:db1.* grant select on db1.* to 'egon2'@'%' identified by '123'; #只在db表中可以查到egon2用户的select权限被设置为Y #针对某一个表:db1.t1 grant select on db1.t1 to 'egon3'@'%' identified by '123'; #只在tables_priv表中可以查到egon3用户的select权限 #针对某个字段 grant select (id,name),update (age) on db1.t3 to 'egon4'@'localhost' identified by '123'; #可以在tables_priv和columns_priv中看到相应的权限 grant select,insert,update on s1.* to 'alex'@'192.168.193.200'; grant all privileges on db1.t1 to 'alex'@'%'; 去掉权限 revoke all privileges on db1.t1 from 'alex'@'%'; 记得使用命令后每次刷新授权 flush privileges; 查看用户权限 show grants for 用户@'10.0.0.%'; show grants for root@'192.168.1.2';
三、密码管理
1.mysql修改用户密码
方法1: 用SET PASSWORD命令
首先登录MySQL。
格式:mysql> set password for 用户名@localhost = password('新密码');
例子:mysql> set password for root@localhost = password('123');
方法2:用mysqladmin
格式:mysqladmin -u用户名 -p旧密码 password 新密码
例子:mysqladmin -uroot -p123456 password 123
注意:这里是在bash中输入命令,而不是mysql中输入命令
方法3:用UPDATE直接编辑user表
首先登录MySQL。
mysql> use mysql;
mysql> update user set password=password('123') where user='root' and host='localhost';
mysql> flush privileges;
2.忘记root密码时的处理
1)停数据库
/etc/init.d/mysqld stop
windows下则输入(需要设置环境变量,或者在mysqld所处的文件夹下输入命令)
mysqld stop
2)启动数据库为无密码验证模式
linux下输入命令
mysqld_safe --skip-grant-tables --skip-networking &
windows下则输入
mysqld --skip-grant-tables
mysql -u root
update mysql.user set authentication_string=PASSWORD('456') where user='root' and host='localhost';
刷新权限(必须步骤):flush privileges;
退出mysql,然后重启mysql
/etc/init.d/mysqld restart
#注意,mysql各个版本的安全策略不一样,版本不一样修改密码的方式可能不一样。