一: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)
四、Mysql备份和恢复
Mysql使用最广泛的备份恢复工具是mysqldump
备份一个指定数据库:
mysqldump -u root -p 数据库名称 > 备份文件.sql
例:mysqldump -u root -p linuxcast > linuxcast_db.sql
mysqldump备份出来的是纯文本的SQL文件,可以修改后作为其它数据库数据使用。
从备份的SQL文件恢复一个指定数据库:
mysql -u root -p 数据库名称 < 备份文件.sql
五、设置数据库编码
数据库使用一个特定编码保存数据,如latin(英文)、Big5(繁体中文)、GB2312(中文)、UTF8(国际通用编码)等,不同语言一般使用不同编码保存。
编码主要影响以下两个方面:
1、数据库保存相同内容所占的空间大小
2、数据库与客户端通信
Mysql默认编码是:character set:latin1
collation:latin1_swedish_ci
可以通过以下命令查看Mysql支持的编码:SHOW CHARACTER SET;
查看Mysql当前使用的编码:SHOW VARIABLES LIKE'character_set%';
SHOW VARIABLES LIKE'collation';
创建数据库的时候可以使用以下命令指定编码:
mysql> create database baiyudb -> default character set utf8 -> default collate utf8_general_ci;
也可以通过以下命令修改一个数据库的编码:
ALTER DATABASE 数据库名 CHARACTER SET utf8 COLLATE utf8_general_ci; #数据库中已经存在数据改变编码可能会导致数据不正常
修改Mysql默认编码:
通过修改Mysql配置文件my.cnf是指Mysql默认编码:
[root@localhost ~]# cat /etc/my.cnf [client] default-character-set=utf8 [mysql] default-character-set=utf8 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 default-character-set=utf8 collation-server=utf8_unicode_ci init-connect='SET NAMES utf8' character-set-server=utf8 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
修改后重启Mysql服务,通过SHOW VARIABLES LIKE'CHARACTER_SET%;
SHOW VARIABLES LIKE'COLLATION%';查看默认编码。