对于任何一种数据库来说,安全问题都是非常重要的。如果数据库出现安全漏洞,轻则数据被窃取,重则数据被破坏,这些后果对于一些重要的数据库都是非常严重的。下面来从操作系统和数据库两个层对MySQL的安全问题进行讨论。
set password=password(‘newpassword‘);
mysql -uroot -p123
mysql -uroot -p
[client] user=username password=password
chomod +600 my.cnf
[root@iZ28dr6w0qvZ ~]# mysql ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)
[root@iZ28dr6w0qvZ ~]# vim /etc/my.cnf ... [client] #password = your_password user=cqh password=123
[root@iZ28dr6w0qvZ ~]# service mysqld restart Shutting down MySQL... SUCCESS! Starting MySQL.. SUCCESS! [root@iZ28dr6w0qvZ ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> select current_user(); +----------------+ | current_user() | +----------------+ | cqh@localhost | +----------------+ 1 row in set (0.02 sec)
grant select,insert,update,delete on tablename to ‘username‘@‘hostname‘;
mysql> select * from db where user=‘cqh‘\G *************************** 1. row *************************** Host: localhost Db: test User: cqh Select_priv: Y Insert_priv: Y Update_priv: Y Delete_priv: Y Create_priv: Y Drop_priv: Y Grant_priv: N References_priv: Y Index_priv: Y Alter_priv: Y Create_tmp_table_priv: Y Lock_tables_priv: Y Create_view_priv: Y Show_view_priv: Y Create_routine_priv: Y Alter_routine_priv: Y Execute_priv: Y Event_priv: Y Trigger_priv: Y 1 row in set (0.00 sec)
[root@iZ28dr6w0qvZ ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 103 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> grant select,update,insert,delete on mysql.user to chenqionghe@localhost; Query OK, 0 rows affected (0.00 sec)
[root@iZ28dr6w0qvZ ~]# mysql -uchenqionghe Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 106 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> use mysql; Database changed mysql> mysql> update user set password=password(‘abcd‘) where user=‘root‘ and host=‘localhost‘; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
[root@iZ28dr6w0qvZ ~]# mysql -uroot -pabcd Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
[root@iZ28dr6w0qvZ ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> use test; Database changed mysql> create table t (name varchar(500)); Query OK, 0 rows affected (0.02 sec)
mysql> load data infile ‘/etc/passwd‘ into table t; Query OK, 23 rows affected (0.01 sec) Records: 23 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from t; +----------------------------------------------------------------------+ | name | +----------------------------------------------------------------------+ | root:x:0:0:root:/root:/bin/bash | | bin:x:1:1:bin:/bin:/sbin/nologin | | daemon:x:2:2:daemon:/sbin:/sbin/nologin | | adm:x:3:4:adm:/var/adm:/sbin/nologin | | lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin | | sync:x:5:0:sync:/sbin:/bin/sync | | shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown | | halt:x:7:0:halt:/sbin:/sbin/halt | | mail:x:8:12:mail:/var/spool/mail:/sbin/nologin | | uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin | | operator:x:11:0:operator:/root:/sbin/nologin | | games:x:12:100:games:/usr/games:/sbin/nologin | | gopher:x:13:30:gopher:/var/gopher:/sbin/nologin | | ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin | | nobody:x:99:99:Nobody:/:/sbin/nologin | | vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin | | ntp:x:38:38::/etc/ntp:/sbin/nologin | | saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin | | postfix:x:89:89::/var/spool/postfix:/sbin/nologin | | sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin | | nscd:x:28:28:NSCD Daemon:/:/sbin/nologin | | www:x:500:500::/alidata/www:/sbin/nologin | | mysql:x:501:501::/home/mysql:/sbin/nologin
[root@iZ28dr6w0qvZ ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 26 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> show processlist; +----+------+-----------+------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+------+---------+------+-------+------------------+ | 2 | root | localhost | NULL | Sleep | 53 | | NULL | | 26 | root | localhost | NULL | Query | 0 | NULL | show processlist | +----+------+-----------+------+---------+------+-------+------------------+ 2 rows in set (0.00 sec) mysql> grant process on *.* to ‘cqh‘@‘localhost‘; Query OK, 0 rows affected (0.00 sec)
mysql> lock table user read; Query OK, 0 rows affected (0.00 sec)
[root@iZ28dr6w0qvZ ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 27 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> set password=password(‘123‘);
[root@iZ28dr6w0qvZ ~]# mysql -ucqh Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 31 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> show processlist; +----+------+-----------+-------+---------+------+------------------------------+------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+-------+---------+------+------------------------------+------------------------------+ | 26 | root | localhost | mysql | Sleep | 20 | | NULL | | 27 | root | localhost | NULL | Query | 15 | Waiting for table level lock | set password=password(‘123‘) | | 31 | cqh | localhost | NULL | Query | 0 | NULL | show processlist | +----+------+-----------+-------+---------+------+------------------------------+------------------------------+ 3 rows in set (0.00 sec)
mysql> show processlist; +----+------+-----------+-------+---------+------+------------------------------+------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+-------+---------+------+------------------------------+------------------------------+ | 26 | root | localhost | mysql | Sleep | 20 | | NULL | | 27 | root | localhost | NULL | Query | 15 | Waiting for table level lock | set password=password(‘123‘) | | 31 | cqh | localhost | NULL | Query | 0 | NULL | show processlist | +----+------+-----------+-------+---------+------+------------------------------+------------------------------+ 3 rows in set (0.00 sec) mysql> kill 27; ERROR 1095 (HY000): You are not owner of thread 27
mysql> grant super on *.* to cqh@localhost; Query OK, 0 rows affected (0.00 sec) mysql> show grants for cqh@localhost; +--------------------------------------------------+ | Grants for cqh@localhost | +--------------------------------------------------+ | GRANT PROCESS, SUPER ON *.* TO ‘cqh‘@‘localhost‘ | +--------------------------------------------------+ 1 row in set (0.00 sec)
[root@iZ28dr6w0qvZ ~]# mysql -ucqh Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 40 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> show processlist; +----+------+-----------+-------+---------+------+------------------------------+------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+-------+---------+------+------------------------------+------------------------------+ | 26 | root | localhost | mysql | Sleep | 20 | | NULL | | 27 | root | localhost | NULL | Query | 15 | Waiting for table level lock | set password=password(‘123‘) | | 31 | cqh | localhost | NULL | Query | 0 | NULL | show processlist | +----+------+-----------+-------+---------+------+------------------------------+------------------------------+ 3 rows in set (0.00 sec) mysql> kill 27; Query OK, 0 rows affected (0.00 sec)
mysql> grant select on test.* to cqh@localhost; Query OK, 0 rows affected (0.00 sec) mysql> show grants for cqh@localhost; +-----------------------------------------------+ | Grants for cqh@localhost | +-----------------------------------------------+ | GRANT USAGE ON *.* TO ‘cqh‘@‘localhost‘ | | GRANT SELECT ON `test`.* TO ‘cqh‘@‘localhost‘ | +-----------------------------------------------+ 2 rows in set (0.00 sec)
[root@iZ28dr6w0qvZ ~]# mysql -ucqh Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 287 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> use test; Database changed mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | menu | | salary | | t | | t1 | | t12 | | t2 | +----------------+ 6 rows in set (0.00 sec)
[root@iZ28dr6w0qvZ ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 288 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> use test; Database changed mysql> drop table t12; Query OK, 0 rows affected (0.00 sec)
[root@iZ28dr6w0qvZ ~]# mysql -ucqh Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 290 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> use test; Database changed mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | menu | | salary | | t | | t1 | | t2 | +----------------+ 5 rows in set (0.00 sec)
mysql> show grants for cqh@localhost; +-----------------------------------------------+ | Grants for cqh@localhost | +-----------------------------------------------+ | GRANT USAGE ON *.* TO ‘cqh‘@‘localhost‘ | | GRANT SELECT ON `test`.* TO ‘cqh‘@‘localhost‘ | +-----------------------------------------------+ 2 rows in set (0.00 sec)
[root@iZ28dr6w0qvZ ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 292 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> use test; Database changed mysql> create table t12(id int); Query OK, 0 rows affected (0.03 sec)
[root@iZ28dr6w0qvZ ~]# mysql -ucqh Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 293 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> use test; Database changed mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | menu | | salary | | t | | t1 | | t12 | | t2 | +----------------+ 6 rows in set (0.00 sec)
mysql> grant select on *.* to cqh identified by ‘123‘ REQUIRE ssl; Query OK, 0 rows affected (0.00 sec)
mysql> grant select,insert on test.* to cqh@localhost; Query OK, 0 rows affected (0.00 sec) mysql> grant all privileges on *.* to cqh@localhost; Query OK, 0 rows affected (0.00 sec) mysql> show grants for cqh@localhost; +-------------------------------------------------------+ | Grants for cqh@localhost | +-------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO ‘cqh‘@‘localhost‘ | | GRANT SELECT, INSERT ON `test`.* TO ‘cqh‘@‘localhost‘ | +-------------------------------------------------------+ 2 rows in set (0.00 sec)
mysql> revoke all privileges on *.* from cqh@localhost; Query OK, 0 rows affected (0.00 sec)
mysql> show grants for cqh@localhost; +-------------------------------------------------------+ | Grants for cqh@localhost | +-------------------------------------------------------+ | GRANT USAGE ON *.* TO ‘cqh‘@‘localhost‘ | | GRANT SELECT, INSERT ON `test`.* TO ‘cqh‘@‘localhost‘ | +-------------------------------------------------------+ 2 rows in set (0.00 sec)
[root@iZ28dr6w0qvZ ~]# mysql -ucqh Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 395 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> use test; Database changed mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | menu | | salary | | t | | t1 | | t12 | | t2 | +----------------+ 6 rows in set (0.00 sec) mysql> insert into t1 values (1); Query OK, 1 row affected (0.01 sec)
原文:http://www.cnblogs.com/chenqionghe/p/4873665.html