云主机安mysql_如何在云服务器 ECS 安装 MySQL

双11腾讯云搞优惠,新用户第一年只需花 88 元即可购买最低配的云服务器 ECS 。近来,我恰好想尝试在云服务器上部署完整的项目(Angular + Spring Boot REST + MySQL)。

那就从如何安装 MySQL 开始吧:

第一步,确定当前没有安装 MySQL:

[root@VM-0-10-centos ~]# yum list installed | grep mysql

[root@VM-0-10-centos ~]#

yum 是 Linux 的一款安装软件包的工具

list 是动词

installed 是名称

| 相当于 SQL 里面的 where

grep 相当于 SQL 里面的 select like。

整个命令的所表达的是,请 Linux 帮忙查询出包含 mysql 字样的使用 yum 安装的软件包。

确定没有安装,那么我们就往下走...

第二步,下载安装包

858474a9c829

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

使用当下最新版的 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

查看安装结果

[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

第四步,启动 mysqld 服务并查看状态

[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: 启动 mysql 服务

systemctl status mysqld.service: 查看 mysql 服务状态

systemctl enable mysqld.service: 设定开机启动 mysql 服务

第五步,登录 mysql

查看 root 默认密码

[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

默认密码为: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)

修改一个自己能记得住的密码

修改完密码,要用 flush privileges 刷新权限

重新登录试试看,使用 exit 命令退出 mysql

中场休息...

当目前为止,MySQL 已经可以在云服务上使用了,但是,大家都知道不可能让团队每个成员都在服务器上操作 MySQL 的。

接下来我们继续看看怎么使用 Workbench 从本机连到云服务器上的 MySQL...

第六步,下载并安装 Workbench

858474a9c829

Workbench

第七步,新建一个 MySQL 的 DB 账号

因为 root 权限太大,所以新建一个其他账号,并开放给大家用:

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)

创建账号 star,host 为 %(host 默认是 localhost,表示只能本机连接,设定为 % 表示其他所有 IP 都可以连接,假如只允许某个 IP 访问,那么指定 IP 地址即可)。所以要远程连接 MySQL,一定要设定 host。

identified by 后面带的是账号 star 的密码

新建的账号会保存到 database=mysql 的 user table 下

之前旧版的 mysql ,密码是用 mysql_native_password 加密的。而现在的都默认用 caching_sha2_password 加密,但远程登录会失败,所以要更新为 mysql_native_password。

把所有权限给 star

第八步,开放 3306 端口

启动防火窗

[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 ...

...

默认下,防火墙服务 firewalld 是关闭的

启动并设定防火墙开机自动启动

开放端口

[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 需要重新设定

新增端口后,要 --reload 才起效

第九步,用 Workbench 连接

配置基本参数

858474a9c829

Workbench-1

Connection Name起一个自己喜欢的名字就可以

选择 Parameters

Hostname 写公网 IP,端口默认为 3306,Username 是刚刚建立的 MySQL DB 用户名

点击 4 打开 5,输入密码 A123@a123

测试结果

858474a9c829

Test Result

使用 Workbench 创建 Schema

858474a9c829

Create Schema

使用 Workbench 创建 Table

858474a9c829

Create Table

到目前为止,已经达到目标了:

在 ECS 安装了 MySQL

在本地使用 Workbench 连接 ECS 上的 MySQL

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值