1.权限表
安装Mysql时会自动安装一个名为Mysql的数据库。Mysql数据库下存储的都是权限表。
1.1 user表
有39个字段,可分为4类
(1)用户列
包括Host(主机名)、User(用户名)、Password(密码),
(2)权限列
包括Select_priv、Insert_priv等以priv结尾的字段。
(3)安全列
包括ssl_type、ssl_cipher、x509_issuer和x509_subject。
可使用show variables like ‘have_openssl’语句查看是否具有ssl功能
(4)资源控制列
包括max_questions、max_updates、max_connections和max_user_connections.
max_questions :每小时可以允许执行多少次查询
max_updates :每小时可以允许执行多少次更新
max_connections :每小时可以建立多少连接
max_user_connections :单个用户可以同时具有的连接数
1.2 db表和host表
db表存储了某个用户对一个数据库的权限。两个表的结构差不多,可分为两类:
(1)用户列
db表包括:Host(主机名)、Db(数据库名)、User(用户名);
Host表包括:Host(主机名)、Db(数据库名)。
Host表是db表的扩展。
(2)权限列
db表比Host表多了两个字段:Create_routine_priv和Alter_routine_priv。两个字段决定用户是否具有创
建和修改存储过程的权限。
1.3 tables_priv表和columns_priv表
(1)tables_priv表 可以对单个表进行权限设置,包括8个字段:Host(主机名)、Db(数据库名)、User(用
户 名)、Table_name(表名)、Table_priv、Column_priv、Timestamp和Grantor
Table_priv :对表操作的权限(select、insert、update、delete、create、drop、grant、
references、 index和alter)。
Column_priv :对表中的数据列进行操作的权限(select、insert、update和references)。
Timestamp :修改权限的时间。
Grantor :权限是谁设置的。
(2) columns_priv表 可以对单个数据列进行权限设置,包括7个字段:Host(主机名)、Db(数据库名)、
User(用户 名)、Table_name(表名)、Column_name、Column_priv和Timestamp
Column_name :可以对那些数据列进行操作。
!!Mysql中的权限分配顺序:user表 —>db表 —>tables_priv表 —>columns_priv表 。
若user表中值为Y则不需检查后面的表。
1.4 procs_priv表
可对存储过程和存储函数进行权限配置。
包含8个字段:Host(主机名)、Db(数据库名)、User(用户名)、routine_name、routine_type、
proc_priv、Timestamp和Grantor
routine_name :存储过程或函数的名称
routine_type :类型(function或procedure)
proc_priv :拥有的权限(execute、alter routine和grant)
Timestamp :存储更新的时间
2.账户管理
2.1 登陆和退出Mysql服务器
mysql -h hostname|hostIP -P port -u username -p DataBaseName -e "sql语句"
//例:
mysql -h localhost -P 3306 -u root -p mysqldb -e "desc mysqldbTable"
mysql -h 59.65.226.15 -P 3306 -u root -p123 mysqldb -e "desc mysqldbTable"
2.2 新建普通用户
(1)用create user语句新建普通用户
create user user1 [identified by [password] 'password1'] [,user2[identified by
[password] 'password2']]
user1 :由用户名(User)和主机名(Host)构成
identified by :设置用户密码(若是普通字符串,就不需[password]关键字。
//例:
create user 'user1'@'localhost' identified by '123','user2'@'localhost'
identified by '234'
(2)用insert语句新建用户
insert into mysql.user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject)
values ('localhost','user1',password('password'),' ',' ',' ');
mysql数据库下user表中,ssl_cipher,x509_issuer,x509_subject没有默认值,所以必须在此为其设置
初始值。
Password字段一定要使用password()函数将密码加密。
//例:
insert into mysql.user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject)
values('localhost','user1',password('123'),'','','');
flush privileges;
执行完insert命令后要使用flush命令使用户生效(需要reload权限)。
(3)grant语句新建用户
grant priv_type on database.table to user1[identified by [password] 'password1']
[,user1[identified by [password] 'password1']]...
priv_type :新用户的权限
database.table :新用户权限范围(只能在指定的数据库和表上使用自己的权限)
user1 :由用户名(User)和主机名(Host)构成
例:
grant select on mysqldb.table1 to 'user1'@'localhost' identified by '123';
2.3删除普通用户
(1)drop user语句删除普通用户
drop user user1,user2,...
//例:
drop user 'user1'@'localhost','user2'@'localhost';
(2)delete语句删除普通用户
delete from mysql.user where Host='localhost' and User='user1';
flush privileges;
2.4root用户修改自己的密码
//(1)
mysqladmin -u username -p password "new_password"
//(1)例:
mysqladmin -u root -p password "123456"
//(2)
update mysql.user set Password=password("new_password") where User="root" and
Host="localhost";
flush privileges;
//(2)例:
update mysql.user set Password=password("123456") where User="root" and
Host="localhost";
flush privileges;
//(3)
set password=password("new_password");
flush privileges;
//(3)例:
set password=password("123456");
flush privileges;
2.5root用户修改普通用户的密码
//(1)
set password for 'user1'@'localhost'=password("1234");
//(2)
update mysql.user set Password=password("1234") where User="user1" and
Host="localhost";
flush privileges;
//(3)
grant select on *.* to "user1"@"localhost" identified by '1234';
2.6普通用户修改密码
set password=password('1234')
2.7root用户密码丢失的解决方法
(1)使用 ‘–skip-grant-tables’选项启动Mysql服务(使Mysql服务器停止权限判断)
Windows操作系统:
mysqld –skip-grant-tables 或mysqld-nt –skip-grant-tables 或net start mysql –skip-grant-tables;
Linux操作系统:
mysqld_safe –skip-grant-tables user=mysql 或
/etc/init.d/mysql start –mysqld –skip-grant-tables
(2)登陆root用户,设置新密码
update mysql.user set Password=password(‘1root1’) where User=’root’ and Host=’localhost’;
(3)加载权限表
flush privileges;
3.权限管理
3.1各种权限
这些权限都存储在mysql数据库下的权限表中。
p344
3.2授权
grant priv_type [(column_list)] on database.table to user [identified by [password]
‘password’] [, user [identified by [password] ‘password’]]… [with with_option [with_option]…]
priv_type:权限类型
column_list:权限作用于那些列上
user:用户名和主机名构成,‘username’@’hostname’
identified by:为用户名设置密码
with:关键字后面有一个或多个with_option参数。有五个参数选项:
grant option:被授权用户可以将权限赋予给别的用户;
max_queries_per_hour count:每小时可执行count次查询;
max_updates_per_hour count:每小时可执行count次更新;
max_connections_per_hour count:每小时可建立count次连接;
max_user_connections count:单个用户可同时具有count个连接数。
//新建用户'test5'对所有数据库有select和update权限
grant select,update on *.* to 'test5'@'localhost' identified by '12345' with grant
option
3.3收回权限
revoke priv_type [(column_list)]… on database.table from user[,user]…
收回全部权限:revoke all privileges,grant option from user[,user]…
3.4查看权限
select * from mysql.user
或
show grants for ‘username’@’localhost’;