第八章
数据备份与还原:
数据的备份:
备份单个数据库:
mysqldump–uusername –ppassword dbname [tbname1 [tbname2…]]>filename.sql
mysqldump–uroot –pxxxx chapter8>d:/backup/chapter8_20161125.sql
备份多个数据库:
mysqldump–uusername –ppassword -–database dbname1 [dbname2 dbname3…]>filename.sql【--database后面至少指定一个数据库名称】
备份所有数据库:
mysqldump–uusername –ppassword –all –database>filename.sql
数据的还原:
数据库的库是不能被还原的,所以还得先建库,用库
createdatabase chapter8;
usechapter8;
1、mysql –uroot–pxxxx chapter8>d:/backup/chapter8_20161125.sql
2、sourced:/backup/chapter8_20161125.sql
用户管理:
创建普通用户:
使用grant语句创建用户:
grantprivileges on database.table
to‘username’@’hostname’ [ identified by [password] ‘password’]
[,‘username’@’hostname’[ identified by [password] ‘password’]]…
privileges:表示该用户具有的权限信息
database.table:表示新用户的权限范围表
username:新用户的名称
hostname:主机名
password:新用户的密码
grantprivileges on chapter8 to ‘user1’@’localhost’ identified by ‘123’;【使用grant语句创建一个新用户,用户名为user1、密码为123,并授予该用户对chapter8表具有查询权限】
使用create user语句创建用户:
createuser ‘username’@’hostname’ [identified by [password] ‘password’]
[,‘username’@’hostname’[identified by [password] ‘password’]]…
username:表示新创建的用户名
hostname:表示主机名
identifiedby:用于设置用户的密码
password:用户的密码
[password]:表示使用哈希值设置密码,可选
create user ‘user2’@’localhost’identified by ‘123’;【使用create user语句创建一个新用户,用户名为user2、密码为123】
使用insert语句创建用户:
Insertinto mysql.user (host,user,password,ssl_cipher、x509_issuer、x509_subject)
values(‘hostname’,’username’,password(‘password’),’’,’’,’’);
password()是一个加密函数,用于给密码加密
insertinto mysql.user(host,user,password,ssl_cipher,x509_issuer,x509_subject)
values(‘localhost’,’user3’,password(‘123’),’’,’’,’’);
insert没有刷新的权限,需手动刷新
flushprivileges;
删除普通用户:
dropuser ‘user1’@’localhost’;
deletefrom mysql.user where host=’localhost’ and user=’user2’;
修改用户密码:
修改root用户的密码:
mysqladmin–u username [-h hostname] –p password new_password【-h指定对应的主机,可以省略不写,默认为localhost】
mysqladmin–u root –p xxxx yyyy;【执行成功后还需输入旧密码,下次登录生效】
updatemysql.user set password=password(‘new_password’) where user=’username’ andhost=’hostname’;
updatemysql.user set password=password(‘yyyy’) where user=’root’ and host=’localhost’;【update没有刷新权限,只能flushprivileges】
setpassword=password(‘new_password’);
setpassword=password(‘yyyy’);
root用户修改普通用户的密码:
grantusafe on *.* to ‘username’@’localhost’ identified by [password] ‘new_password’;
updatemysql.user set password=password(‘new_password’)where user=’username’ and host=’hostname’;【不能忘了刷新】
setpassword for ‘username’@’localhost’=password(‘new_password’);
普通用户修改密码:
setpassword=password(‘new_password’);
root用户密码丢失解决方案:
1、 停止mysql服务:
net stop mysql;
2、 使用—skip-grant-tables
Mysqld –skip-grant-tables;
3、 登录mysql服务器,重启一个对话框
mysql –u root;
4、 使用update语句设置root用户密码
update mysql.user set password=password(‘yyyy’) where user=’root’and host=’localhost’;
5、 加载权限表
flush privileges;
权限管理:
授予权限:
grant privileges[(column)] [,privileges [(column)]] on database.table to ‘username’@’hostname’[identified by [password] ‘password’]
[,‘username’@’hostname’[identified by [password] ‘password’]]…
[withwith_option [with_option]…]
privileges:表示权限类型
column:表示权限作用于某一刻
identified by:为用户设置密码
with:后面可以带有多个参数,有五个取值
grant option:将自己的权限授予其他用户
max_queries_per_hour count:设置每小时最多可以执行多少次(count)查询
max_updates_per_hourcount:设置每小时最多可以执行多少次更新
max_connections_per_hourcount:设置每小时最大的连接数量
max_user_connections:设置每个用户最多可以同时建立连接的数量
grant insert,selecton *.* to ‘user4’@’localhost’ identified by ‘123’ with grant option;【使用grant语句创建一个新的用户,用户名为user4,密码为123,user4用户对所有数据库有insert,select权限,并使用with grant option子句】
查看权限:
showgrant for ‘user4’@’localhost’ \G
收回权限:
revokeprivileges [columns] [,privileges [(column)]] on database.table from ‘username’@’hostname’ [,’username’@’hostname’]…
privileges:表示收回的权限
column:表示权限作用于哪列上,如果不指定该参数表示作用于整个表
revokeinsert on *.* from ‘user4’@’localhost’;【收回insert权限】
selecthost,user,password,insert_priv from mysql.user where user=’user4’ \G
revokeall privileges,grant option from ‘username’@’hostname’ [,’username’@’hostname’]…
revokeall privileges,grant option from ‘root’@’localhost’;【收回user4的所有权限】
查询一下:
selecthost,user,password,insrt_priv,select_priv,grant_priv from mysql.user whereuser=’user4’ \G