# 主机域书写
- 172.16。1.%
- 172.16.%.%
- 172.%.%.%
- % (匹配所有)
- 172.16.1.0/255.255.255.0
- 172.16.1.5%(意思是172.16.1.50-59和172.16.1.5)
- db01
- localhost
(错误写法:不能写成172.16.1.0/24)
# 查询用户
select user,host from mysql.user;
# 添加用户(user后的引号可加可不加)
create user bbb@'%';
create user '用户名'@'主机域';
# 删除用户
drop user ''@'localhost';
drop user '用户名'@'主机域';
# 修改密码
(原来有秘密需要修改密码就加-p,没有就不加)
[root@db02 ~]# mysqladmin -uroot -p password '789'
# 进入表里修改密码
## 修改当前登录的用户
mysql> set password=PASSWORD('111');
## update修改密码(配合刷新授权表)(改表里的password字段)
mysql> update mysql.user set password=PASSWORD('222') where user='root' and host='localhost';
不加where····这段改的是所有用户的密码
###刷新授权表
mysql> flush privileges; //(只有在修改密码时,才需要用到)
## 修改密码(这条命令既可以创建用户用可以修改密码,还可以修改权限)
mysql> grant all on *.* to root@'localhost' identified by '123';
# 进库里修改密码
## alter
mysql> alter user root@'localhost' identified by '123';
## 修改当前登录的用户
mysql> set password=PASSWORD('111');
## update修改密码(配合刷新授权表)
mysql> update mysql.user set authentication_string=PASSWORD('222') where user='root' and host='localhost';
###刷新授权表
mysql> flush privileges; //(只有在修改密码时,才需要用到)
## 修改密码
mysql> grant all on *.* to root@'localhost' identified by '123';
# 库外修改密码
[root@db02 ~]# mysqladmin -uroot -p password '789'
# 停掉数据库
/etc/init.d/mysqld stop
# 跳过授权表和网络启动
mysqld_safe --skip-grant-tables --skip-networking &
# 刷新授权表
flush privileges;
# 用grant去创建(添加用户和密码)
grant all on *.* to root@'localhost' identified by '123' with grant option;
# 修改密码
set password=PASSWORD('111');
update mysql.user set authentication_string=PASSWORD('222') where user='root' and host='localhost';
# 授权
grant all on *.* to user@'%' identified by '123';
所有权限 库.表 用户@主机域 密码
all=all privileges
# 给开发授权(实际环境中记得走流程)
grant select,update,insert on lol.* to dev@'10.0.0.%' identified by '复杂一点';
一般不会给delete权限
grant select,update,insert,delete on lol.* to dev@'10.0.0.%' identified by '复杂一点';
##解析
lol:是某个库名
lol.*:单库级别去授权
lol.user:单表级别去授权
还可以给到更小级别去授权(user表下面还有user字段)(但是user表下面的字节要加到select后面)
如:脱敏:脱离敏感信息
mysql> grant select(user,host),update,insert on mysql.user to dev1@'localhost' identified by '123';
# 权限
INSERT,SELECT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE,
REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES,
EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE
ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE