安装前提: 实例安全组的入方向规则已放行22、80、443、3306端口。
目录
步骤一:安装MySQL
1.通过Xshell远程连接ECS实例。
2.运行以下命令,更新YUM源。
sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
3.(可选)当操作系统为Alibaba Cloud Linux 3时,执行如下命令,安装MySQL所需的库文件。
sudo rpm -Uvh https://mirrors.aliyun.com/alinux/3/updates/x86_64/Packages/compat-openssl10-1.0.2o-4.0.1.al8.x86_64.rpm
4.运行以下命令,安装MySQL。
sudo yum -y install mysql-community-server --enablerepo=mysql80-community --nogpgcheck
5.运行以下命令,查看MySQL版本号。
mysql -V
返回结果如下,则表示MySQL安装成功。
步骤二:配置MySQL
1.运行以下命令,启动MySQL服务。
sudo systemctl start mysqld
2.运行以下命令,设置开机自启动MySQL服务。
sudo systemctl enable mysqld
3.运行以下命令,获取并记录root用户的初始密码。
sudo grep 'temporary password' /var/log/mysqld.log
执行命令结果示例如下。
A temporary password is generated for root@localhost: x_W&g2wyu_vi
示例末尾的x_W&g2wyu_vi
为初始密码,后续在对MySQL进行安全性配置时,需要使用该初始密码。
PS: 如果执行sudo grep 'temporary password' /var/log/mysqld.log 后没有报错,也没有任何结果显示,说明默认密码为空,可以直接进行下一步(后面设置密码时直接填写新密码就行)。
4. 运行以下命令,对MySQL进行安全性配置。
sudo mysql_secure_installation
a.根据提示信息,重置MySQL数据库root用户的密码。如果上一步没有查到初始密码,直接输入 新密码即可。
ps: 在输入密码时,系统为了最大限度地保证数据安全,命令行将不做任何回显。只需要输入 正确的密码信息,然后按Enter键即可。
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确认使用已设置的密码。
b.根据提示信息,删除匿名用户。
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.
c.禁止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.
d.删除test库以及对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.
e. 重新加载授权表。
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官方文档。
步骤三:远程访问MySQL数据库
建议使用非root账号远程登录MySQL数据库。下面示例中,将创建新的MySQL账号,用于远程访问MySQL。
1.运行以下命令后,输入root用户的密码登录MySQL。
sudo mysql -uroot -p
2.依次运行以下命令,创建远程登录MySQL的账号,并允许远程主机使用该账号访问MySQL。
我新建的账号为yuhui、密码为Yuhui123456.
。
#创建数据库用户dmsTest,并授予远程连接权限。
create user 'yuhui'@'%' identified by 'Yuhui123456.';
#为dmsTest用户授权数据库所有权限。
grant all privileges on *.* to 'yuhui'@'%';
#刷新权限。
flush privileges;
PS: 实际创建账号时,需将示例密码Yuhui123456.
更换为符合要求的密码,并妥善保存。
密码要求:长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。
可以使用以下特殊符号:
()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/
因为本人是做学习使用的,密码规则较为简单的话便于测试,故修改MySQL密码规则的命令如下:
SHOW VARIABLES LIKE 'validate_password%'; # 查看MySQL的密码规则 (该语句只适合Mysql8,Mysql5.x的另外百度)
+-------------------------------------------------+-------+
| Variable_name | Value |
+-------------------------------------------------+-------+
| validate_password.changed_characters_percentage | 0 |
| validate_password.check_user_name | ON |
| validate_password.dictionary_file | |
| validate_password.length | 4 |
| validate_password.mixed_case_count | 0 |
| validate_password.number_count | 0 |
| validate_password.policy | LOW |
| validate_password.special_char_count | 0 |
+-------------------------------------------------+-------+
8 rows in set (0.01 sec)
/**
validate_password.dictionary_file:用于验证密码强度的字典文件路径;
validate_password.length:密码最小长度,参数默认为8,它有最小值的限定;
validate_password.mixed_case_count:密码至少要包含的大写字母和小写字母的个数;
validate_password.number_count 密码至少要包含的数字个数;
validate_password.policy:密码强度等级检查;(默认是1,即MEDIUM)
validate_password.special_char_count:密码至少要包含的特殊字符个数;
*/
# 以下是修改MySQL的密码规则
mysql> set global validate_password.policy=0; # 修改密码强度等级为0,即是LOW;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password.mixed_case_count=0; # 修改密码至少要包含的大写字母和小写字母的个数为0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password.number_count=0; # 修改密码至少要包含的数字个数为0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password.special_char_count=0; # 修改密码至少要包含的特殊字符的个数为0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password.length=3; # 修改密码的长度最小个数为3;
Query OK, 0 rows affected (0.00 sec)
使用ALTER修改密码
ALTER USER yuhui@localhost identified by '123456';
或
ALTER USER 'yuhui'@'%' identified by '123456';
因为本人是做学习使用,所以用root账号远程登录MySQL数据库方便。以下是root用户远程登录MySQL的创建命令。
#创建数据库用户dmsTest,并授予远程连接权限。
create user 'root'@'%' identified by '123456';
#为dmsTest用户授权数据库所有权限。
grant all privileges on *.* to 'root'@'%';
#刷新权限。
flush privileges;
用Navicat连接服务器MySQL。