如何在云服務(wù)器 ECS 安裝 MySQL

雙11騰訊云搞優(yōu)惠,新用戶第一年只需花 88 元即可購買最低配的云服務(wù)器 ECS 。近來,我恰好想嘗試在云服務(wù)器上部署完整的項(xiàng)目(Angular + Spring Boot REST + MySQL)。

那就從如何安裝 MySQL 開始吧:

第一步,確定當(dāng)前沒有安裝 MySQL:

[root@VM-0-10-centos ~]# yum list installed | grep mysql
[root@VM-0-10-centos ~]# 
  • yum 是 Linux 的一款安裝軟件包的工具
  • list 是動(dòng)詞
  • installed 是名稱
  • | 相當(dāng)于 SQL 里面的 where
  • grep 相當(dāng)于 SQL 里面的 select like。
  • 整個(gè)命令的所表達(dá)的是,請 Linux 幫忙查詢出包含 mysql 字樣的使用 yum 安裝的軟件包。

確定沒有安裝,那么我們就往下走...

第二步,下載安裝包

MySQL-Repository
  • 切換到 /usr/local 并下載
[root@VM-0-10-centos local]# wget http://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm

第三步,安裝

  • 安裝 rpm
[root@VM-0-10-centos local]# yum install -y mysql80-community-release-el7-3.noarch.rpm
  • 安裝 mysql-server
[root@VM-0-10-centos local]# yum install -y mysql-server

使用當(dāng)下最新版的 mysql (mysql80-community-release-sles12.rpm)不能使用 install mysql-server,我還未深究為何。提示信息如下:

Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
No package mysql-server available.
Error: Nothing to do
  • 查看安裝結(jié)果
[root@VM-0-10-centos local]# yum list installed | grep mysql
mysql-community-client.x86_64          8.0.22-1.el7               @mysql80-community
mysql-community-client-plugins.x86_64  8.0.22-1.el7               @mysql80-community
mysql-community-common.x86_64          8.0.22-1.el7               @mysql80-community
mysql-community-libs.x86_64            8.0.22-1.el7               @mysql80-community
mysql-community-libs-compat.x86_64     8.0.22-1.el7               @mysql80-community
mysql-community-server.x86_64          8.0.22-1.el7               @mysql80-community
mysql80-community-release.noarch       el7-3                      @/mysql80-community-release-el7-3.noarch

第四步,啟動(dòng) mysqld 服務(wù)并查看狀態(tài)

[root@VM-0-10-centos local]# systemctl start mysqld.service
[root@VM-0-10-centos local]# systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-10-29 16:59:56 CST; 3s ago
   ......
  • systemctl start mysqld.service: 啟動(dòng) mysql 服務(wù)
  • systemctl status mysqld.service: 查看 mysql 服務(wù)狀態(tài)
  • systemctl enable mysqld.service: 設(shè)定開機(jī)啟動(dòng) mysql 服務(wù)

第五步,登錄 mysql

  • 查看 root 默認(rèn)密碼
[root@VM-0-10-centos local]# grep ' password' /var/log/mysqld.log
2020-10-29T08:59:50.026442Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: btG*ut3*PTbe
  • 默認(rèn)密碼為:btG*ut3*PTbe
  • 用 root 登錄 mysql
[root@VM-0-10-centos local]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.22

Copyright (c) 2000, 2020, 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 -u root -p 按回車,并輸入 btG*ut3*PTbe 就可正常登錄 mysql。

  • 修改 root 的密碼
mysql> alter user 'root'@'localhost' identified by 'Abcd#12345';
Query OK, 0 rows affected (0.03 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
  • 修改一個(gè)自己能記得住的密碼
  • 修改完密碼,要用 flush privileges 刷新權(quán)限
  • 重新登錄試試看,使用 exit 命令退出 mysql

中場休息...


當(dāng)目前為止,MySQL 已經(jīng)可以在云服務(wù)上使用了,但是,大家都知道不可能讓團(tuán)隊(duì)每個(gè)成員都在服務(wù)器上操作 MySQL 的。

接下來我們繼續(xù)看看怎么使用 Workbench 從本機(jī)連到云服務(wù)器上的 MySQL...

第六步,下載并安裝 Workbench

下載地址:https://dev.mysql.com/downloads/workbench/

Workbench

第七步,新建一個(gè) MySQL 的 DB 賬號

因?yàn)?root 權(quán)限太大,所以新建一個(gè)其他賬號,并開放給大家用:

mysql> create user 'star'@'%' identified by 'A123@a123';
Query OK, 0 rows affected (0.03 sec)

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set plugin='mysql_native_password' where user='star';
Query OK, 1 row affected (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select user, host, plugin from user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| star             | %         | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)
mysql> grant all privileges on *.* to 'star'@'%';
Query OK, 0 rows affected (0.01 sec)
  • 創(chuàng)建賬號 star,host 為 %(host 默認(rèn)是 localhost,表示只能本機(jī)連接,設(shè)定為 % 表示其他所有 IP 都可以連接,假如只允許某個(gè) IP 訪問,那么指定 IP 地址即可)。所以要遠(yuǎn)程連接 MySQL,一定要設(shè)定 host。
  • identified by 后面帶的是賬號 star 的密碼
  • 新建的賬號會(huì)保存到 database=mysql 的 user table 下
  • 之前舊版的 mysql ,密碼是用 mysql_native_password 加密的。而現(xiàn)在的都默認(rèn)用 caching_sha2_password 加密,但遠(yuǎn)程登錄會(huì)失敗,所以要更新為 mysql_native_password。
  • 把所有權(quán)限給 star

第八步,開放 3306 端口

  • 啟動(dòng)防火窗
[root@VM-0-10-centos ~]# systemctl status firewalld
...
   Active: inactive (dead)
 ...
[root@VM-0-10-centos ~]# systemctl start firewalld.service
[root@VM-0-10-centos ~]# systemctl enable firewalld
...
[root@VM-0-10-centos ~]# systemctl status firewalld
...
   Active: active (running) since ...
...
  • 默認(rèn)下,防火墻服務(wù) firewalld 是關(guān)閉的
  • 啟動(dòng)并設(shè)定防火墻開機(jī)自動(dòng)啟動(dòng)
  • 開放端口
[root@VM-0-10-centos ~]# firewall-cmd --zone=public --list-ports

[root@VM-0-10-centos ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@VM-0-10-centos ~]# firewall-cmd --zone=public --list-ports

[root@VM-0-10-centos ~]# firewall-cmd --reload
success
[root@VM-0-10-centos ~]# firewall-cmd --zone=public --list-ports
3306/tcp
  • --list-ports: 列出所有開放的端口
  • --add-port: 添加端口
  • --permanent: 永久添加,否則重啟 Linux 需要重新設(shè)定
  • 新增端口后,要 --reload 才起效

第九步,用 Workbench 連接

  • 配置基本參數(shù)
Workbench-1
  1. Connection Name起一個(gè)自己喜歡的名字就可以
  2. 選擇 Parameters
  3. Hostname 寫公網(wǎng) IP,端口默認(rèn)為 3306,Username 是剛剛建立的 MySQL DB 用戶名
  4. 點(diǎn)擊 4 打開 5,輸入密碼 A123@a123
  • 測試結(jié)果
Test Result
  • 使用 Workbench 創(chuàng)建 Schema
Create Schema
  • 使用 Workbench 創(chuàng)建 Table
Create Table

到目前為止,已經(jīng)達(dá)到目標(biāo)了:

  • 在 ECS 安裝了 MySQL
  • 在本地使用 Workbench 連接 ECS 上的 MySQL
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容