Linux环境介绍
服务器:阿里云ecs.t5-lc1m2.large (性能约束实例)
操作系统:CentOS 7.8 64位
网络:专有网络VPC,并绑定了固定公网IP
MySQL基本信息
- 版本:MySQL 8.0.28(在实际操作时,MySQL的版本可能因软件源的更新而有所不同)
- 配置文件:/etc/my.cnf
- 数据存储:/var/lib/mysql
- 命令文件:/usr/bin和/usr/sbin
安装流程
- 连接服务器,进入Linux环境
具体方式自行百度,此处不做赘述。
- 通过以下命令更新YUM源
sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
- 通过以下命令安装MySQL
sudo yum -y install mysql-community-server --enablerepo=mysql80-community --nogpgcheck
- 查看MySQL版本号
mysql -V
返回以下结果,说明安装成功。
配置流程
- 通过以下命令启动MySQL服务
systemctl start mysqld
- 通过以下命令设置MySQL服务开机自启动
systemctl enable mysqld
- 通过以下命令查看
/var/log/mysqld.log
文件,获取并记录root用户的初始密码
grep 'temporary password' /var/log/mysqld.log
执行命令显示结果如下,其中
root@localhost:
后的内容即为初始密码,后续需要用到,需要记录下来。
- 通过以下命令对MySQL进行安全性配置
mysql_secure_installation
- 重置root密码
Enter password for user root: #输入已获取的root用户初始密码
The existing password for the user account root has expired. Please set a new password.
New password: #输入新的MySQL密码,此处需要输入强密码,否则验证不通过。例如Abc123...
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库以及对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所需的端口号
MySQL默认占用的端口号为3306。需要在ECS实例所使用的安全组入方向添加规则并放行3306端口,详细操作流程见阿里云添加安全组规则 -
在服务器上,创建远程登录MySQL的账号
a. 运行以下命令后,输入root用户的密码登录MySQLmysql -uroot -p
b. 依次运行以下命令创建远程登录MySQL的账号
建议使用非root账号远程登录MySQL数据库,本示例账号为abc、密码为123456。
mysql> create user 'abc'@'%' identified by '123456'; #创建数据库用户dmsTest,并授予远程连接权限。 mysql> grant all privileges on *.* to 'abc'@'%'; #为dmsTest用户授权数据库所有权限。 mysql> flush privileges; #刷新权限。
实际创建账号时,需将示例密码123456更换为符合要求的密码,并妥善保存。密码要求:长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。可以使用以下特殊符号:()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/