安裝 MySQL


# 安裝 MySQL
---
###### tags: `MySQL`
### 1. Add MySQL APT repository in Ubuntu
```linux= 
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
```
>In the prompt, choose Ubuntu Bionic and click Ok
>在提示中，選擇 Ubuntu Bionic 並單擊確定

>The next prompt shows MySQL 8.0 chosen by default. Choose the first option and click OK
>下一個提示顯示默認選擇的 MySQL 8.0。 選擇第一個選項，然後單擊確定

>In the next prompt, select MySQL 5.7 server and click OK.
>在下一個提示中，選擇 MySQL 5.7 服務器並單擊確定。

>The next prompt selects MySQL5.7 by default. Choose the last otpion Ok and click OK
>下一個提示默認選擇 MySQL 5.7。 選擇最後一個選項 On 並單擊 OK

---

### 2. Update MySQL Repository on Ubuntu

>Run the below command to update your system packages
>運行以下命令來更新您的系統包

```linux=
sudo apt-get update
```
---

### 3. Install MySQL 5.7 on Ubuntu 20.04 Linux machine

```linux= 
sudo apt install -f mysql-client=5.7* mysql-community-server=5.7* mysql-server=5.7*
```
---

>Hit the y key to start installation of MySQL 5.7 on Ubuntu 20.04 Linux.

```linux=
Reading package lists... Done
Building dependency tree
Reading state information... Done
Selected version '5.7.35-1ubuntu18.04' (MySQL:repo.mysql.com [amd64]) for 'mysql-client'
Selected version '5.7.35-1ubuntu18.04' (MySQL:repo.mysql.com [amd64]) for 'mysql-community-server'
Selected version '5.7.35-1ubuntu18.04' (MySQL:repo.mysql.com [amd64]) for 'mysql-server'
The following additional packages will be installed:
  libmecab2 libtinfo5 mysql-common mysql-community-client
The following NEW packages will be installed:
  libmecab2 libtinfo5 mysql-client mysql-common mysql-community-client mysql-community-server mysql-server
0 upgraded, 7 newly installed, 0 to remove and 90 not upgraded.
Need to get 51.6 MB of archives.
After this operation, 315 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
```

>Enter and re-enter root password when prompted
出現提示時輸入並重新輸入 root 密碼

---

### 4.Secure MySQL 5.7 Installation on Ubuntu 20.04
``` sql=
sudo mysql_secure_installation
```

>Press Enter. When prompted for password, provide the root password set above.
按 Enter。 當提示輸入密碼時，請提供上面設置的 root 密碼。

>Answer the prompts as below:


```linux=
Enter current password for root (enter for none): <Enter 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: n 

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                 

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
更改 root 的密碼？
Remove anonymous users? [Y/n] Y 
刪除匿名用戶？
Disallow root login remotely? [Y/n] n
禁止遠程root登錄？
Remove test database and access to it? [Y/n] Y 
刪除測試數據庫並訪問它？
Reload privilege tables now? [Y/n] Y 
現在重新加載權限表？
Thanks for using MariaDB!
```
----
### 5. 啟用 MySQL 遠程訪問

```sql=
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
```
* 查找“bind_address ”行並更改如下：
```sql= 
# By default we only accept connections from localhost 
#bind-address   = 127.0.0.1 
bind-address   = 0.0.0.0
```
* 保存文件並重啟mysql
```sql=
sudo systemctl restart mysql
```

### 6. 重啟MySQL

```sql=
service mysql restart
```
---

### 7. 修改帳號權限、遠端連線
```sql=
grant all ON *.* TO 'root'@'%' IDENTIFIED BY '12345678';
flush privileges;
```

[參考連結](https://computingforgeeks.com/how-to-install-mysql-on-ubuntu-focal/)

# 刪除MySQL
---
## 1. 首先先移除 mysql


```sql=
apt-get purge --auto-remove mysql-common mysql-server mariadb-server
apt-get autoremove
apt-get autoclean
```

## 2. 然後把設定檔刪掉
```sql=   
rm -rf /etc/mysql rm -rf /var/lib/mysql
```

## 3. 砍掉 mysql 使用者
```sql=
killall -9 mysql (或 killall -9 mysqld) userdel mysql
```

[參考連結](https://noob.tw/remove-mysql-completely/)