在CentOS上使用yum安裝MySQL+安全優化

技術小阿哥發表於2017-11-13

   使用yum安裝的好處是,你不用自己去解決軟體之間的依賴問題,基本上yum執行完成,也就把軟體安裝好了,下面介紹使用yum的方法來安裝MySQL,同時也會介紹安裝完成後的安全優化。

        注意:下面的操作都是以新安裝的CentOS  6.5來作為演示的。




1.使用yum安裝MySQL

        

  • 檢視是否已經安裝MySQL:

1
[root@leaf]# yum list installed | grep mysql

        如果你在安裝CentOS的時候指定了安裝MySQL資料庫,就要有顯示,這裡我並沒有安裝MySQL。

  • 安裝MySQL資料庫:

1
[root@leaf ~]# yum -y install mysql-server mysql mysql-devel

        注意在你的系統上檢視安裝成功後的提示資訊。

  • 檢視安裝的MySQL資訊:

1
2
3
4
5
[root@leaf ~]# yum list installed | grep mysql
mysql.x86_64            5.1.73-5.el6_6  @base                                   
mysql-devel.x86_64      5.1.73-5.el6_6  @base                                   
mysql-libs.x86_64       5.1.73-5.el6_6  @base                                   
mysql-server.x86_64     5.1.73-5.el6_6  @base
  • 開啟MySQL服務:

1
2
[root@leaf ~]# service mysqld start
Starting mysqld:                                           [  OK  ]
  • 登陸到MySQL資料庫中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@leaf ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.1.73 Source distribution
 
Copyright (c) 2000, 2013, 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>


        其實如果是非生產環境要求的話,像上面這樣使用yum簡單安裝就可以了,當然如果是用於生產環境的,那麼建議還是使用原始碼安裝。




2.MySQL安全優化


        至於為什麼要安全優化,可以看博主寫的用原始碼安裝MySQL的博文:《在CentOS上原始碼安裝MySQL+安裝問題解決+安全優化》

        下面就直接給出操作的步驟。(注意:要確保MySQL服務已經開啟,上面已經給出方法)


(1)為root使用者建立密碼

1
2
3
4
5
6
7
[root@leaf ~]# mysql -u root
mysql> update mysql.user set password = password(`123456`) where User = `root`;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

        上面就為root使用者建立了密碼`123456`。


(2)刪除匿名使用者

  • 先使用剛剛建立的密碼登陸到MySQL中:

1
2
3
4
[root@leaf ~]# mysql -u root -p
Enter password:
 
mysql>
  • 刪除匿名使用者:

1
2
3
4
5
6
7
mysql> DROP USER ``@`localhost`;
Query OK, 0 rows affected (0.00 sec)
 
mysql> DROP USER ``@`leaf`;     
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

        注意第二條刪除語句的`leaf`,在這裡,`leaf`為我的主機名,按照你的實際情況進行操作即可。


        執行完上面(1)(2)步之後再檢視一下當前的賬戶資訊:

1
2
3
4
5
6
7
8
9
mysql> select User, Host, Password from mysql.user; 
+------+-----------+-------------------------------------------+
| User | Host      | Password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | leaf      | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | 127.0.0.1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)


(3)刪除test資料庫或名字以test開頭的資料庫

        操作如下:

1
2
3
4
5
6
7
8
mysql> delete from mysql.db where db like `test%`;
Query OK, 2 rows affected (0.00 sec)
 
mysql> drop database test;
Query OK, 0 rows affected (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)


        OK,到這裡的話,使用yum安裝MySQL以及MySQL的基本安全優化就完成了,應該不會有太大的問題。

本文轉自 xpleaf 51CTO部落格,原文連結:http://blog.51cto.com/xpleaf/1748625,如需轉載請自行聯絡原作者


相關文章