CentOS 下使用官方 MySQL 源安裝 MySQL 5.7.x


各位有做網站開發及管理網站的朋友總會離不開便宜好用的開源平台 LAMP 組合吧。

當中 LAMP 中的 M 字母代表了開源界的資料庫姣姣者 MySQL。

就讓 Jimmy Chan 示範一下教教大家在 CentOS 7.x 下使用官方的 MySQL 源安裝 MySQL 5.7.x 吧!








增加官方 MySQL Repository 源

### CentOS 7 ###
# rpm -ivh https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
之後為了檢查是否成功增加官方的 MySQL源可輸入下面的指令
# yum repolist all | grep mysql | grep enabled
輸出畫面:
mysql-connectors-community/x86_64  MySQL Connectors Community     enabled:    42
mysql-tools-community/x86_64       MySQL Tools Community          enabled:    53
mysql57-community/x86_64           MySQL 5.7 Community Server     enabled:   227

安裝官方社區版 MySQL Server

增加完成官方 MySQL 源之後就只用輸入下面的指令就能安裝社區版的 MySQL Server 了
### CentOS 7 ###
# yum -y install mysql-community-server

啟用 MySQL Server

安裝完成了當然就是要把 MySQL 後台服務啟用起來吧,輸入下面的指令來啟用 MySQL
 
### CentOS 7 ###

# systemctl start mysqld

在每次重新起動伺服器後自動運行 MySQL Server 可輸入下面的指令,這樣就不用每次重新起動伺服器後要手動起動 MySQL Server 服務在背景運行了
 
### CentOS 7 ###

# systemctl enable mysqld

輸入下面的指令可以檢查 MySQL Server 的後台服務是否正確運行中
 
### CentOS 7 ###

# systemctl status mysqld

輸出畫面:

### CentOS 7 ###

 mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2018-05-28 02:02:14 EST; 1min 17s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 1575 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─1575 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

May 28 02:02:07 www.jimmychanserver.com systemd[1]: Starting MySQL Server...
May 28 02:02:14 www.jimmychanserver.com systemd[1]: Started MySQL Server.

加強 MySQL Server 的安全

在初步安裝完成及運行 MySQL Server 後
MySQL 預設會把 MySQL root 的初始臨時密碼放在 /var/log/mysqld.log 的位置

你可以輸入下面的指令來快速查出它的臨時密碼,以便之後更改了為自己記得的密碼及提高安全性之用,你總不希望任何人也能進入這個 LOG 內找出 MySQL root 的密碼而為所卻為吧
 
# cat /var/log/mysqld.log | grep -i 'temporary password'
 
輸出畫面:

2018-05-28T07:02:29.549979Z 1 [Note] A temporary password is generated for root@localhost: Ae*Yn(9HwqRh
Now, you need to run mysql_secure_installation to secure your MySQL installation. This command takes care of setting the root password, removing anonymous users, disallow root login remotely, etc.

之後輸入下面的指令來運行安全性最佳化程序

# mysql_secure_installation
 
輸出畫面:

Securing the MySQL server deployment.

Enter password for user root:  <== Enter Root password taken from previous step

The existing password for the user account root has expired. Please set a new password.

New password: <== Enter New Root Password

Re-enter new password: <== Re-Enter New Root Password 
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : no <== Since the password is already changed in previous step, type no.

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : yes <== Remove Anonymous user
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : yes <== Disable remote root login
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : yes <== Remove test database
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : yes <== Reload Privilege
Success.

All done!

運行 MySQL Server 的方法

設定好新的 MySQL Root 密碼後就可輸入下面的指令開始在 CLI 環境下使用 MySQL 了
mysql -u root -p

輸出畫面:

MySQL 5.7:
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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>




參考資料

MySQL Yum Repository 官方網址
https://dev.mysql.com/downloads/repo/yum/



A Quick Guide to Using the MySQL Yum Repository
https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/








留言

這個網誌中的熱門文章

64bit Windows 如何使用 32bit ODBC 連接器存取資料庫

更改 NetBeans 預設支援 UTF-8 的方法

Windows 10 RTM and KMS Keys