1. 程式人生 > >如何在CentOS 7/6,RHEL 7/6和Fedora 28/27/26上安裝MySQL 8.0

如何在CentOS 7/6,RHEL 7/6和Fedora 28/27/26上安裝MySQL 8.0

MySQL是一個免費的開源資料庫管理系統,常用於Web應用程式中,用於儲存和檢索記錄和資訊。

新增MySQL儲存庫

MySQL不再通過基本作業系統映像分發或在OS儲存庫中可用。 因此,您需要新增MySQL的官方儲存庫來安裝MySQL社群伺服器。

### CentOS 7 / RHEL 7 ###

rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

### CentOS 6 / RHEL 6 ###

rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el6-1.noarch.rpm

### Fedora 28 ###
rpm -ivh https://dev.mysql.com/get/mysql80-community-release-fc28-1.noarch.rpm ### Fedora 27 ### rpm -ivh https://dev.mysql.com/get/mysql80-community-release-fc27-1.noarch.rpm ### Fedora 26 ### rpm -ivh https://dev.mysql.com/get/mysql80-community-release-fc26-1.noarch.rpm

確保使用以下命令新增並啟用了MySQL儲存庫。

yum repolist all | grep mysql | grep enabled

輸出:可能看起來像。

mysql-connectors-community/x86_64  MySQL Connectors Community     enabled:    51
mysql-tools-community/x86_64       MySQL Tools Community          enabled:    63
mysql80-community/x86_64           MySQL 8.0 Community Server     enabled:    17

安裝 MySQL Community Server

Oracle目前提供穩定版本(v8.0和v5.7)。 您可以選擇要在計算機上安裝的那個。

安裝 MySQL 8.0

在CentOS/RHEL中使用yum命令,在Fedora中使用dnf命令安裝最新的穩定版MySQL。

### CentOS 7/6 & RHEL 7/6 ###

yum -y install mysql-community-server

### Fedora 28/27/26 ###

dnf -y install mysql-community-server

安裝 MySQL 5.7

如果您想嘗試舊版本的MySQL,請在您的計算機上安裝MySQL 5.7。

### CentOS 7/6 & RHEL 7/6 ###

yum -y install mysql-community-server  --disablerepo=mysql80-community --enablerepo=mysql57-community

### Fedora 28/27/26 ###

dnf -y install mysql-community-server  --disablerepo=mysql80-community --enablerepo=mysql57-community

啟動 MySQL 服務

安裝MySQL後,可以使用以下命令啟動MySQL伺服器。

### CentOS 7 / RHEL 7 / Fedora 28/27/26 ###

systemctl start mysqld

### CentOS 6 / RHEL 6 ###

service mysqld start

在系統啟動時啟用MySQL伺服器。

### CentOS 7 / RHEL 7 / Fedora 28/27/26 ###

systemctl enable mysqld

### CentOS 6 / RHEL 6 ###

chkconfig mysqld on

使用以下命令驗證是否已啟動MySQL伺服器。

### CentOS 7 / RHEL 7 / Fedora 28/27/26 ###

systemctl status mysqld

### CentOS 6 / RHEL 6 ###

service mysqld status

輸出:

CentOS 7 / RHEL 7 / Fedora:

 mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2018-12-04 19:19:36 EDT; 21s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 8452 (mysqld)
   Status: "SERVER_OPERATING"
   CGroup: /system.slice/mysqld.service
           └─8452 /usr/sbin/mysqld

Dec 04 19:19:23 server.itzgeek.local systemd[1]: Starting MySQL Server...
Dec 04 19:19:36 server.itzgeek.local systemd[1]: Started MySQL Server.

CentOS 6 / RHEL 6 :

mysqld (pid 2258) is running...

MySQL伺服器安全設定

在CentOS/RHEL/Fedora中,可以在/var/log/mysqld.log中找到初始的MySQL root密碼。 您可以使用以下命令從日誌檔案中獲取密碼。

cat /var/log/mysqld.log | grep -i 'temporary password'

輸出:

2017-11-25T07:02:29.549979Z 1 [Note] A temporary password is generated for [email protected]: Ed*Sn(6R9q0h

現在,您需要執行mysql_secure_installation來保護MySQL安裝。 此命令負責設定root密碼,刪除匿名使用者,遠端禁止root登入等。

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 
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: No <== Already high secure enough
Using existing password for root.
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伺服器

使用root使用者及其密碼登入MySQL伺服器。

mysql -u root -p

輸出:
MySQL 8.0:

Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.11 MySQL Community Server - GPL

Copyright (c) 2000, 2018, 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 5.7:

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

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

安裝phpMyAdmin

phpMyAdmin是一個基於Web的開源管理工具,用於管理MySQL和MariaDB資料庫。 按照以下連結,根據您的作業系統安裝和配置phpMyAdmin。