mysql帐户安全配置

2017/01/29 mysql

#初始化帐户安全配置 Securing the Initial MySQL Accounts

MySQL的安装过程包括初始化数据目录,其中包含定义MySQL账户的授权表

The MySQL installation process involves initializing the data directory, including the mysql database containing the grant tables that define MySQL accounts

如果使用mysqld –initialize初始化,应该是已经设置了一个密码给初始化帐户

mysql.user授权表定义mysql帐户和其访问权限,mysql的5.7版本只创建一个名称为’root’@’localhost’的帐户,

但是在之前较早的的版本中可能创建多个帐户:

一些帐户的名称是root。这些都是超级用户帐户拥有所有权限,可以做任何事。如果这些root账户密码为空,任何人都不要密码可以连接到MySQL服务器被授予所有权限。这是很危险的

比如之前的root帐户允许从本地连接,可以指定是localhost,127.0.0.1或ipv6地址::1,或其他的IP地址

通常’root’@’localhost’ ,在mysql.proxies_priv 表中还包含了一条授权@,表示任意的用户和任意的主机

如果帐户是匿名的,谁都可以进行连接

##检查系统存在哪些帐户 Checking Which Accounts Exist

mysql -u root -p

mysql 5.7.6之前的版本

mysql> SELECT User, Host, Password FROM mysql.user;

显示结果如下 +——+——————–+———-+ | User | Host | Password | +——+——————–+———-+ | root | localhost | | | root | myhost.example.com | | | root | 127.0.0.1 | | | root | ::1 | | | | localhost | | | | myhost.example.com | | +——+——————–+———-+

mysql5.7.6之后的版本

使用HEX函数,是因为authentication_string密码的存储可能包含二进制文件,可能不能很好的显示。

SELECT User, Host, HEX(authentication_string) FROM mysql.user;

显示如下 mysql> select User,HEX(authentication_string) FROM mysql.user; +———–+————————————————————————————+ | User | HEX(authentication_string) | +———–+————————————————————————————+ | root | 2A36424234383337454237343332393130354545343536384444413744433637454432434132414439 | | mysql.sys | 2A5448495349534E4F544156414C494450415353574F52445448415443414E42455553454448455245 | +———–+————————————————————————————+ 2 rows in set (0.01 sec)

##给root帐户设置密码 Assigning root Account Passwords

使用mysql客户端程序,登录mysqld服务器 mysql -u root -p

Before 5.7.6, use SET PASSWORD:

SET PASSWORD FOR user = PASSWORD('new_password');

As of MySQL 5.7.6, use ALTER USER:

ALTER USER user IDENTIFIED BY 'new_password';

给host为localhost的root帐户设置密码 ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘new_password’;

##给匿名帐户设置密码 Assigning Anonymous Account Passwords

mysql -u root -p Enter password: (enter root password here)

给host为localhost的匿名帐户设置密码 SET PASSWORD FOR ‘’@’localhost’ = PASSWORD(‘new_password’);

##移动匿名帐户 Removing Anonymous Accounts

shell> mysql -u root -p Enter password: (enter root password here)

mysql> DROP USER ''@'localhost';

##测试数据库安全设置

shell> mysql -u root -p
Enter password: (enter root password here)

mysql> DELETE FROM mysql.db WHERE Db LIKE 'test%';
mysql> FLUSH PRIVILEGES; #立即生效
DROP DATABASE test;

参考自 http://dev.mysql.com/doc/refman/5.7/en/default-privileges.html


Search

    Table of Contents