MySQL系列 | 安裝 MySQL 5.7 on Ubuntu 16.04 | 18.04


How To Install MySQL 5.7 

介紹

MySQL是世界上最受歡迎的開源關系數據庫管理系統。廣泛用於apache2,Nginx,IIS等Web服務器。MySQL具有客戶端/服務器體系結構,支持InnoDB存儲引擎,可以安裝在Ubuntu,Debian,CentOS,Windows等各種操作系統上。

Step 1: Add MySQL APT Repository in Ubuntu

Ubuntu隨附默認軟件包存儲庫。因此,如果我們要添加/安裝最新的存儲庫,則必須添加/安裝軟件包存儲庫。以下是使用命令行添加/下載最新的MySQL APT存儲庫的命令

$ sudo wget http://repo.mysql.com/mysql-apt-config_0.8.9-1_all.deb

 輸出結果

www@iZbp1c61wsgboarqkiz0twZ:~$ sudo wget http://repo.mysql.com/mysql-apt-config_0.8.9-1_all.deb
[sudo] password for www: 
--2020-01-07 18:13:29--  http://repo.mysql.com/mysql-apt-config_0.8.9-1_all.deb
Resolving repo.mysql.com (repo.mysql.com)... 23.1.245.43
Connecting to repo.mysql.com (repo.mysql.com)|23.1.245.43|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 35950 (35K) [application/x-debian-package]
Saving to: ‘mysql-apt-config_0.8.9-1_all.deb’

mysql-apt-config_0.8.9-1_all.deb                             100%[===========================================================================================================================================>]  35.11K   184KB/s    in 0.2s    

2020-01-07 18:13:30 (184 KB/s) - ‘mysql-apt-config_0.8.9-1_all.deb’ saved [35950/35950]

然后,下面的命令是安裝上面下載的apt存儲庫

$ sudo dpkg -i mysql-apt-config_0.8.9-1_all.deb

當我們運行上面的命令時,如下面的提示符將打開,請單擊“確定”。  

Step 2: Update MySQL Repository

更新apt倉庫

$ sudo apt-get update

錯誤消息

Err:4 http://repo.mysql.com/apt/ubuntu bionic InRelease
The following signatures were invalid: EXPKEYSIG 8C718D3B5072E1F5 MySQL Release Engineering <mysql-build@oss.oracle.com>
Reading package lists... Done
W: GPG error: http://repo.mysql.com/apt/ubuntu bionic InRelease: The following signatures were invalid: EXPKEYSIG 8C718D3B5072E1F5 MySQL Release Engineering <mysql-build@oss.oracle.com>
E: The repository 'http://repo.mysql.com/apt/ubuntu bionic InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

W: The repository ‘https://repo.mysql.com/apt/ubuntu xenial InRelease’ is not signed.  

在更新apt軟件包時,如果您收到KEYEXPIRED消息。使用以下命令檢查過期的密鑰列表,要續訂過期的密鑰,  

$ sudo apt-key adv --keyserver keys.gnupg.net --recv-keys 8C718D3B5072E1F5 

 輸出內容

www@iZbp1c61wsgboarqkiz0twZ:~$ sudo apt-key adv --keyserver keys.gnupg.net --recv-keys 8C718D3B5072E1F5
Executing: /tmp/apt-key-gpghome.ro9G1rmzGZ/gpg.1.sh --keyserver keys.gnupg.net --recv-keys 8C718D3B5072E1F5
gpg: key 8C718D3B5072E1F5: 3 duplicate signatures removed
gpg: key 8C718D3B5072E1F5: 102 signatures not checked due to missing keys
gpg: key 8C718D3B5072E1F5: "MySQL Release Engineering <mysql-build@oss.oracle.com>" 29 new signatures
gpg: Total number processed: 1 gpg: new signatures: 29 

更新系統存儲庫。  

$ sudo apt-get update
Hit:1 http://mirrors.cloud.aliyuncs.com/ubuntu bionic InRelease
Hit:2 http://mirrors.cloud.aliyuncs.com/ubuntu bionic-updates InRelease
Hit:3 http://mirrors.cloud.aliyuncs.com/ubuntu bionic-security InRelease                    
Get:4 http://repo.mysql.com/apt/ubuntu xenial InRelease [21.6 kB]                
Get:5 http://repo.mysql.com/apt/ubuntu xenial/mysql-5.7 Sources [944 B]
Get:6 http://repo.mysql.com/apt/ubuntu xenial/mysql-apt-config i386 Packages [566 B]
Get:7 http://repo.mysql.com/apt/ubuntu xenial/mysql-apt-config amd64 Packages [566 B]
Get:8 http://repo.mysql.com/apt/ubuntu xenial/mysql-5.7 i386 Packages [5,650 B]
Get:9 http://repo.mysql.com/apt/ubuntu xenial/mysql-5.7 amd64 Packages [5,656 B]
Get:10 http://repo.mysql.com/apt/ubuntu xenial/mysql-tools amd64 Packages [4,344 B]
Get:11 http://repo.mysql.com/apt/ubuntu xenial/mysql-tools i386 Packages [3,596 B]
Fetched 42.9 kB in 2s (23.5 kB/s)
Reading package lists... Done

Step 3: Install MySQL 5.7 on Ubuntu

使用以下命令安裝MySQL 5.7

$ sudo apt-get install mysql-server

現在,安裝過程將提示root用戶使用默認密碼,然后再次輸入相同的密碼。  

 

Step 4: Secure MySQL Installation

mysql_secure_installation,該腳本可以執行多項與安全性相關的操作。在命令提示符下運行以下腳本,詢問以下選項。

$ sudo mysql_secure_installation

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: no
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : 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) : 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! 

Step 5: Login to MySQL

現在,使用以下命令登錄到MySQL 5.7 Server,並使用密碼登錄在安裝過程中輸入的密碼。  

~$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

Step 6: Create MySQL Remote User

首先,以root用戶使用命令行登錄MySQL Server,下面是創建用戶的命令,這里我創建用戶“www”。

mysql> CREATE USER 'www'@'%' IDENTIFIED BY 'www123456';
Query OK, 0 rows affected (0.00 sec)

接下來,使用以下命令將特權分配給數據庫,在這里,我將所有數據庫特權分配給www用戶, 如果要為特定數據庫分配特權,請使用數據庫名稱替換“.”。  

mysql> GRANT ALL PRIVILEGES ON * . * TO 'www'@'%';
Query OK, 0 rows affected (0.00 sec)

 或者

mysql> GRANT ALL PRIVILEGES ON *.* TO 'www'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

要生效,請使用以下命令重新加載特權  

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

Step 7: How to Enable MySQL Remote Access 

默認情況下,出於安全原因,在MySQL數據庫服務器中禁用遠程訪問。 要啟用MySQL Server的遠程連接,我們必須更改MySQL配置文件中的  

打開 /etc/mysql/mysql.conf.d/mysqld.cnf 文件  

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

在[mysqld]部分下面找到該行  

[mysqld]
bind-address = 127.0.0.1

並替換為  

bind-address = 0.0.0.0

 輸出

[mysqld]
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
datadir		= /var/lib/mysql
log-error	= /var/log/mysql/error.log
# By default we only accept connections from localhost
bind-address	= 0.0.0.0
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

重新啟動MySQL Server生效  

$ sudo systemctl restart mysql.service

 查看MySQL狀態

 

Step 8: Start/Restart/Stop MySQL Server Using Command Line

$ sudo systemctl start mysql.service
$ sudo systemctl restart mysql.service
$ sudo systemctl stop mysql.service

其他

 安裝命令

    1  ls
    2  sudo wget http://repo.mysql.com/mysql-apt-config_0.8.9-1_all.deb
    3  exit
    4  sudo wget http://repo.mysql.com/mysql-apt-config_0.8.9-1_all.deb
    5  ls
    6  sudo dpkg -i mysql-apt-config_0.8.9-1_all.deb
    7  ls
    8  sudo apt-get update
    9  sudo apt-key adv --keyserver keys.gnupg.net --recv-keys 8C718D3B5072E1F5
   10  sudo apt-get update
   11  sudo apt-get install mysql-server 
   12  sudo mysql_secure_installation
   13  mysql -u root -p
   14  sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
   15  sudo systemctl restart mysql.service
   16  sudo systemctl status mysql.service
   17  ifconfig
   18  ps -aux|grep mysql
   19  cat /etc/passwd
   20  history 
   21  history >> install.log

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM