该操作需要先具备一台ECS云服务器,有多种方式可以获得,第一种也即是最适合新人的方式,就是通过产品试用,获得一个三月的试用实例,可以用来实操各种场景及部署体验各种服务。新人朋友可以点击该链接直达产品试用。当然你可以根据实际需求购买对应配置的云服务器。
申请试用完成后,等待实例完成创建并运行,可以通过多种方式连接服务器进行操作了,这里我通过第三方工具MobaXterm来连接。当然你完全可以通过自带的workbench或者其他SSH工具。完成连接后的界面如下:
接下来正式开始本地实操,部署一个mysql数据库,开整。
在开始之前,需要先配置好ECS的安全组策略,实例安全组的入方向规则要放行22、80、443、3306端口,在网络和安全组中点击配置规则,在入方向点击快速添加即可。(实际生产环境中不建议在授权对象这配置0.0.0.0,这表明不限制,也就是所有都可访问,为避免波路面风险,应针对特定访问来源配置。)如下:
接下来可以正式开始了,第一步,安装mysql。命令如下:
##更新yum源
rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
##在线安装mysql社区版
yum -y install mysql-community-server --enablerepo=mysql80-community --nogpgcheck
##查看mysql版本号
mysql -V
##查看mysql进程
ps -ef |grep mysql
##配置mysql开机自启
systemctl enable mysqld
##获取mysql初始密码
grep 'temporary password' /var/log/mysqld.log
##mysql安全性配置
mysql_secure_installation
##安全配置如下
Enter password for user root: #输入已获取的root用户初始密码
The existing password for the user account root has expired. Please set a new password.
New password: #输入新的MySQL密码
Re-enter new password: #重复输入新的MySQL密码
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) :Y #输入Y选择更新MySQL密码。您也可以输入N不再更新MySQL密码。
New password: #输入新的MySQL密码
Re-enter new password: #重复输入新的MySQL密码
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :Y #输入Y确认使用已设置的密码。
##删除匿名用户
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 #输入Y删除MySQL默认的匿名用户。
Success.
##禁止root远程登录
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 #输入Y禁止root远程登录。
Success.
##删除test库及其权限
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 #输入Y删除test库以及对test库的访问权限。
- 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 #输入Y重新加载授权表。
Success.
All done!
运行命令mysql -uroot -p 后,输入root用户的密码登录MySQL,如下:
创建远程登录MySQL的账号,并允许远程主机使用该账号访问MySQL。
#创建数据库用户dmsTest,并授予远程连接权限。
create user 'dmsTest'@'%' identified by 'Ecs@123****';
#为dmsTest用户授权数据库所有权限。
grant all privileges on *.* to 'dmsTest'@'%';
#刷新权限。
flush privileges;
到此你便可以通过MySQL客户端远程登录MySQL进行测试。例如:MySQL Workbench、Navicat。这里我使用Navicat工具连接,如下: