1. 程式人生 > >資料庫安全mariadb(mysql)

資料庫安全mariadb(mysql)

安裝mariadb

[[email protected] ~]# yum -y install mariadb mariadb-server
[[email protected] ~]# systemctl restart mariadb.service 
[[email protected] ~]# netstat -pntul | grep mysql
tcp      0   0 0.0.0.0:3306         0.0.0.0:*             LISTEN      15787/mysqld        
[[email protected] ~]# mysqladmin -uroot -hlocalhost -p password "123456"   //預設無密碼,設定新密碼
Enter password: 
[
[email protected]
~]# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) [[email protected] ~]# mysql -uroot -p123456 MariaDB [(none)]>

初始化安全指令碼

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> select user,host,password from mysql.user;    //可以看到空使用者,空密碼都可以登入!
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | proxy     |                                           |
| root | 127.0.0.1 |                                           |
| root | ::1       |                                           |
|      | localhost |                                           |
|      | proxy     |                                           |
+------+-----------+-------------------------------------------+
6 rows in set (0.00 sec)


[
[email protected]
~]# mysql_secure_installation ... Enter current password for root (enter for none): OK, successfully used password, moving on... You already have a root password set, so you can safely answer 'n'. Change the root password? [Y/n] n ... skipping. Remove anonymous users? [Y/n] Y //是否移除匿名使用者 ... Success! ... Disallow root login remotely? [Y/n] Y //是否允許root遠端登入 ... Success! ... Remove test database and access to it? [Y/n] Y //是否移除test庫 - Dropping test database... ... Success! - Removing privileges on test database... ... Success! ... Reload privilege tables now? [Y/n] Y //是否重新整理許可權 ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB! [
[email protected]
~]# mysql -uroot -p123456 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec) MariaDB [(none)]> select user,host,password from mysql.user; +------+-----------+-------------------------------------------+ | user | host | password | +------+-----------+-------------------------------------------+ | root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | root | 127.0.0.1 | | | root | ::1 | | +------+-----------+-------------------------------------------+ 3 rows in set (0.00 sec)

刪除記錄歷史命令的檔案內容

[[email protected] ~]# >  .bash_history
[[email protected] ~]# >  .mysql_history 

資料庫內設定密碼

MariaDB [(none)]> set password for [email protected]"localhost"=password('123456');
Query OK, 0 rows affected (0.00 sec)

資料安全

[[email protected] ~]# mysql -uroot -p123456 
MariaDB [(none)]> grant all on *.* to [email protected]"%" identified by "123456";
Query OK, 0 rows affected (0.00 sec)

[[email protected] ~]# tcpdump -w log -i eth0 src or dst port 3306
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes

[[email protected] ~]# mysql -utcpuser -p123456 -h 192.168.4.51    客戶端連結資料庫
MariaDB [(none)]> select * from mysql.user\G;


[[email protected] ~]# tcpdump -A -r log
...
14:39:29.860487 IP 192.168.4.254.39290 > mariadb.mysql: Flags [P.], seq 125:154, ack 183, win 229, options [nop,nop,TS val 22095518 ecr 20353473], length 29
[email protected]@..........3.z....#zbi.[...........
.Q&..6.......select * from mysql.user
...

這些資料明文傳輸,容易被抓取到,所以可以通過ssl或ssh加密資料進行傳輸