部署MySQL
简介
MySQL 是一种开源关系型数据库管理系统。与其他关系型数据库一样,MySQL 将数据存储在由行和列组成的表中。用户可以使用结构化查询语言(通常称为 SQL)定义、操作、控制和查询数据。本教程介绍如何在HCE 2.0上安装部署MySQL。
准备工作
- 准备一台ECS实例,并分配公网IP或者弹性公网IP(EIP)。
- 实例安全组的入方向规则已放通22、3306端口。
操作步骤
- 安装MySQL服务端和客户端。
- 执行以下命令安装MySQL服务端和客户端。
dnf install mysql-server mysql-common mysql -y
- 执行以下命令查看MySQL的版本号。
mysql -V
如果返回如下类似信息,表示安装成功。
mysql Ver 8.0.37 for Linux on x86_64 (Source distribution)
- 执行以下命令安装MySQL服务端和客户端。
- 配置MySQL。
- 运行以下命令,启动并将MySql服务设置为开机自启动。
systemctl start mysqld
- 执行以下命令查看服务状态。
systemctl status mysqld
如果显示active (running)则表示启动成功。
如果要将mysqld设置为开机自启动,则需要执行如下命令。
systemctl enable mysqld
- 执行以下命令,对MySQL进行安全配置。
mysql_secure_installation
根据个人需要和提示信息,自定义选项内容。
Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component? Press y|Y for Yes, any other key for No: Y // 是否设置密码组件 There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2 // 选择密码安全策略 Please set the password for root here. New password: Re-enter new password: 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 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 // 是否删除匿名用户 Success. 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 // 是否禁止root账户远程登录 Success. 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 // 是否删除测试数据库 - 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 // 是否重新加载权限表 Success. All done!
- 运行以下命令,启动并将MySql服务设置为开机自启动。
- 远程连接数据库。
- 执行以下命令在MySQL server端连接数据库。
mysql -uroot -p
然后输入上述为root用户创建的密码。
- 输入以下语句创建测试账号和密码。
create user 'test'@'%' identified by 'test****';
其中test为账号,test****为对应密码。
- 输入以下语句为test账号授权数据库所有权限。
grant all privileges on *.* to 'test'@'%';
- 输入以下语句刷新权限。
flush privileges;
- 执行以下语句退出数据库。
exit
- 在另一台服务器上安装MySql client后输入以下命令进行远程连接。
mysql -h <MySql server IP> -utest -p
或者使用Navicat、VSCode插件进行远程连接。
- 执行以下命令在MySQL server端连接数据库。
以上关于密码、权限相关配置仅用于测试,业务环境请谨慎使用