一:mysql用户管理
MYSQL数据库默认只有一个root用户
mysql将用户信息保存在mysql数据库user表中
创建一个新用户:CREATE USER 用户名 IDENTIFIED BY ‘密码‘; #新用户创建后不能登录,因为没有设置权限
mysql> create user xj identified by ‘654321‘; Query OK, 0 rows affected (0.00 sec) mysql> select user,host,password from user; +------+-----------------------+-------------------------------------------+ | user | host | password | +------+-----------------------+-------------------------------------------+ | root | localhost | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 | | root | localhost.localdomain | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 | | root | 127.0.0.1 | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 | | | localhost | | | | localhost.localdomain | | | xxj | % | | | XXJ | % | | | xj | % | *2A032F7C5BA932872F0F045E0CF6B53CF702F2C5 | +------+-----------------------+-------------------------------------------+ 8 rows in set (0.00 sec)
删除一个用户:DROP USER 用户名;
重命名一个用户:RENAME USER 原用户名 TO 新用户名;
mysql> DROP USER XXJ; Query OK, 0 rows affected (0.00 sec) mysql> DROP USER xj; Query OK, 0 rows affected (0.00 sec) mysql> SELECT user,host,password FROM user; +------+-----------------------+-------------------------------------------+ | user | host | password | +------+-----------------------+-------------------------------------------+ | root | localhost | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 | | root | localhost.localdomain | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 | | root | 127.0.0.1 | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 | | | localhost | | | | localhost.localdomain | | | xxj | % | | +------+-----------------------+-------------------------------------------+ 6 rows in set (0.00 sec) mysql> RENAME USER xxj TO xiexiaojun; Query OK, 0 rows affected (0.00 sec) mysql> SELECT user,host,password FROM user; +------------+-----------------------+-------------------------------------------+ | user | host | password | +------------+-----------------------+-------------------------------------------+ | root | localhost | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 | | root | localhost.localdomain | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 | | root | 127.0.0.1 | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 | | | localhost | | | | localhost.localdomain | | | xiexiaojun | % | | +------------+-----------------------+-------------------------------------------+ 6 rows in set (0.00 sec)
修改当前用户密码:SET PASSWORD=PASSWORD(‘新密码‘);
修改当前用户密码: SET PASSWORD FOR 用户名=PASSWORD(‘新密码‘);
mysql> set password for xiejun=password(‘6543210‘); Query OK, 0 rows affected (0.00 sec) mysql> select user,host,password from user; +------------+-----------------------+-------------------------------------------+ | user | host | password | +------------+-----------------------+-------------------------------------------+ | root | localhost | *05EB937E16F9C8885963B8F1F2487AB5C94FED2B | | root | localhost.localdomain | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 | | root | 127.0.0.1 | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 | | | localhost | | | | localhost.localdomain | | | xiexiaojun | % | | | xiejun | % | *2437A328FF22FC8D4C8AC9CB393FAA760BF10B0D | +------------+-----------------------+-------------------------------------------+ 7 rows in set (0.00 sec) mysql> set password for xiexiaojun=password(‘6543210‘); Query OK, 0 rows affected (0.00 sec) mysql> select user,host,password from user; +------------+-----------------------+-------------------------------------------+ | user | host | password | +------------+-----------------------+-------------------------------------------+ | root | localhost | *05EB937E16F9C8885963B8F1F2487AB5C94FED2B | | root | localhost.localdomain | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 | | root | 127.0.0.1 | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 | | | localhost | | | | localhost.localdomain | | | xiexiaojun | % | *2437A328FF22FC8D4C8AC9CB393FAA760BF10B0D | | xiejun | % | *2437A328FF22FC8D4C8AC9CB393FAA760BF10B0D | +------------+-----------------------+-------------------------------------------+ 7 rows in set (0.00 sec) mysql>
二、mysql权限管理
mysql权限系统控制一个用户是否能进行连接,以及连接后能够针对哪些对象进行什么什么操作。
Mysql权限控制包括2个阶段:
1 检查用户是否能够连接
2 检查用户是否具有所执行动作的权限
Mysql授予权限可分为以下几个层级:
全局层级
数据库层级
表层级
列层级
子程序层级
Mysql通过GRANT授予权限,REVOKE撤销权限。
授予一个用户权限:
GRANT ALL PRIVILEGES ON 层级 TO 用户名@主机 IDENTIFIED BY 密码;
撤销一个用户权限:REVOKE ALL PRIVILEGES FROM 用户名;
三、Mysql连接认证
当连接Mysql服务时,Mysql通过用户名密码和主机信息验证是否允许连接。
GRANT ALL PRIVILEGES ON *.* TO ‘用户名‘@‘主机‘ IDENTIFIED BY 密码‘;
这里的主机是指允许从哪些主机进行连接,可以使用如下形式:
1 所有主机‘%‘
2 精确的主机名或IP地址:www.linuxcast.net或192.168.1.1
3 使用“*”通配符:*.linuxcast.net
4 指定一个网段:192.168.1.0/255.255.255.0
mysql> GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘123456789‘; # *.* 全局权限 所有数据库下所有表 Query OK, 0 rows affected (0.00 sec)
4、Mysql备份和恢复
Mysql使用最广泛的备份恢复工具是mysqldump
备份一个指定数据库:
mysqldump -u root -p 数据库名称 > 备份文件.sql
例:mysqldump -u root -p linuxcast > linuxcast_db.sql
mysqldump备份出来的是纯文本的SQL文件,可以修改后作为其它数据库数据使用。
从备份的SQL文件恢复一个指定数据库:
mysql -u root -p 数据库名称 < 备份文件.sql