天天看點

Linux Ubuntu 20.04/Mint20.1 安裝mysql 5.7Linux Ubuntu 20.04/Mint20.1 安裝mysql 5.7

Linux Ubuntu 20.04/Mint20.1 安裝mysql 5.7

最近開始使用linux系統,選擇使用Linux mint發行版系統,在搭建環境過程中安裝mysql資料遇到問題,MySQL服務無法成功啟動。一開始使用的是MySQL官方網站提供的二進制.deb包進行安裝,按官方安裝說明進行操作最後服務無法正常啟動,内網中各種查閱資料後也是無法解決問題。最後谷歌采用另一種方式進行安裝。

在Ubuntu下使用apt指令安裝軟體是最快捷、友善的軟體安裝方式,為什麼一開始我不選擇apt指令安裝呢,那是因為自Ubuntu20之後倉庫中MySQL源預設是8.0的版本,也就是如果能解決這個問題就可以使用指令安裝了,之前内網中沒有搜尋到換源的方式就采用了使用MySQL官方提供的二進制deb包安裝了。以下的安裝方式就是解決MySQL的換源問題後使用apt指令安裝的,看到這裡的朋友若認為後面的内容對你沒有什麼幫助,那可以退出本文章另找其他打解決方案了。(由于Linux mint是Ubuntu發行版的衍生版本,是以在很多地方都是通用的)。

本文是對外網網站内容的翻譯,友善沒有條件的朋友解決問題,有條件的朋友可以選擇通路原文。

原文位址:https://computingforgeeks.com/how-to-install-mysql-on-ubuntu-focal/

目錄

Linux Ubuntu 20.x/Mint20.x 安裝mysql 5.7

添加MySQL APT 庫

更新MySQL-Repository

在Ubuntu 20.04 Linux機器上安裝MySQL 5.7

在Ubuntu 20.04上安全地安裝MySQL 5.7

檢查Mysql版本

建立MySQL使用者(可選,僅測試)

啟用MySQL遠端通路(可選)

添加MySQL APT 庫

Ubuntu已經提供了預設的MySQL包倉庫。為了添加或安裝最新的庫,我們将安裝包倉庫。使用以下指令下載下傳倉庫:

sudo apt update
sudo apt install wget -y
wget https://dev.mysql.com/get/mysql-apt-config_0.8.12-1_all.deb
           

下載下傳後,通過運作以下指令安裝存儲庫:

sudo dpkg -i mysql-apt-config_0.8.12-1_all.deb
           

在提示中,選擇Ubuntu Bionic,然後單擊“确定”。

Linux Ubuntu 20.04/Mint20.1 安裝mysql 5.7Linux Ubuntu 20.04/Mint20.1 安裝mysql 5.7

下一個提示顯示預設情況下選擇的MySQL 8.0。選擇第一個選項,然後單擊“确定”或回車。

Linux Ubuntu 20.04/Mint20.1 安裝mysql 5.7Linux Ubuntu 20.04/Mint20.1 安裝mysql 5.7

在下一個提示中,選擇“ MySQL 5.7伺服器”,然後單擊“确定”或回車。

Linux Ubuntu 20.04/Mint20.1 安裝mysql 5.7Linux Ubuntu 20.04/Mint20.1 安裝mysql 5.7

在下一個提示下,預設情況下選擇MySQL5.7。選擇最後一個ot,然後單擊“确定”。

Linux Ubuntu 20.04/Mint20.1 安裝mysql 5.7Linux Ubuntu 20.04/Mint20.1 安裝mysql 5.7

更新MySQL倉庫

運作以下指令以更新系統軟體包

sudo apt-get update
           

現在使用apt-cache搜尋MySQL 5.7(本人在安裝時5.7的版本已經更新到5.7.33,搜尋内容使用了本人環境下打清單内容),如下所示

mysql-server:
  已安裝:(無)
  候選: 8.0.26-0ubuntu0.20.04.2
  版本清單:
     8.0.26-0ubuntu0.20.04.2 500
        500 http://mirrors.aliyun.com/ubuntu focal-updates/main amd64 Packages
        500 http://mirrors.aliyun.com/ubuntu focal-updates/main i386 Packages
        500 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages
        500 http://security.ubuntu.com/ubuntu focal-security/main i386 Packages
     8.0.19-0ubuntu5 500
        500 http://mirrors.aliyun.com/ubuntu focal/main amd64 Packages
        500 http://mirrors.aliyun.com/ubuntu focal/main i386 Packages
     5.7.35-1ubuntu18.04 500
        500 http://repo.mysql.com/apt/ubuntu bionic/mysql-5.7 amd64 Packages
           

如您所見,MySQl 5.7.33-1ubuntu18.04出現在清單中。

在Linux機器上安裝MySQL 5.7

在我們上一步清單中發現了MySQL5.7,我們将使用以下指令安裝MySQL5.7用戶端和MySQL5.7伺服器:

sudo apt install -f mysql-client=5.7.35-1ubuntu18.04 mysql-community-server=5.7.35-1ubuntu18.04 mysql-server=5.7.35-1ubuntu18.04
           

按y鍵開始在linux上安裝MySQL 5.7

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libgdbm-compat4 libmecab2 libperl5.30 libtinfo5 mysql-common mysql-community-client perl perl-modules-5.30
Suggested packages:
  perl-doc libterm-readline-gnu-perl | libterm-readline-perl-perl make libb-debug-perl liblocale-codes-perl
The following NEW packages will be installed:
  libgdbm-compat4 libmecab2 libperl5.30 libtinfo5 mysql-client mysql-common mysql-community-client mysql-community-server mysql-server perl perl-modules-5.30
0 upgraded, 11 newly installed, 0 to remove and 35 not upgraded.
Need to get 58.1 MB of archives.
After this operation, 361 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
           

在出現提示時輸入并重新輸入root密碼,這裡打密碼就是人root賬戶打預設密碼

Linux Ubuntu 20.04/Mint20.1 安裝mysql 5.7Linux Ubuntu 20.04/Mint20.1 安裝mysql 5.7

在Linux上安裝MySQL 5.7安全配置

運作以下指令安裝MySQL安全配置

sudo mysql_secure_installation
           

按Enter鍵。當提示您輸入密碼時,請提供上面設定的root密碼

Securing the MySQL server deployment.

Enter password for user root: 

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: Y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
Using existing password for root.

Estimated strength of the password: 25 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : d

 ... 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) : Y
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) : Y
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) : Y
 - 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) : Y
Success.

All done! 
           

檢查Mysql版本

要确認安裝的版本,首先,使用設定的root密碼連接配接到MySQL。

mysql -u root -p 
           

運作以下指令以顯示版本

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.33    |
+-----------+
1 row in set (0.00 sec)
           

建立MySQL使用者(可選,僅測試)

在仍連接配接到MySQL的同時,運作以下指令來建立使用者:

CREATE USER 'user'@'%' IDENTIFIED BY 'MyStrongPass.';
GRANT ALL PRIVILEGES ON * . * TO 'user'@'%'; 
FLUSH PRIVILEGES;
exit
           

啟用MySQL遠端通路(可選)

(該方案可用來限制遠端通路打服務位址,若沒有此需求可采用其他方式進行配置)

預設情況下,禁用MySQL遠端通路。要啟用它,我們需要編輯mysqld.cnf檔案,如下所示:

$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
           

查找“ bind_address”行,并進行如下更改:

# By default we only accept connections from localhost 
#bind-address   = 127.0.0.1 
bind-address   = 0.0.0.0
           

儲存檔案并重新開機mysql

$ sudo systemctl restart mysql
           

允許通過防火牆的遠端連接配接

$ sudo ufw allow from <remote_IP_address> to any port 3306
$ sudo ufw enable
           

要從遠端計算機通路資料庫,請運作以下指令:

mysql -u user -h database_server_ip -p
           

繼續閱讀