如果您需要在运行Linux系统的ECS实例上部署特定版本的MySQL数据库,可以参考本文提供的步骤手动完成MySQL数据库的部署。
前提条件
部署MySQL
Alibaba Cloud Linux 3
-
远程连接ECS实例。具体操作,请参见使用Workbench登录Linux实例。
-
安装MySQL数据库。
#需要安装compat-openssl10与旧版本OpenSSL库兼容。 sudo yum install -y compat-openssl10 #添加MySQL官方源 sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el8-1.noarch.rpm #安装MySQL服务 sudo dnf install -y mysql-server #启动MySQL服务并设置开机自启 sudo systemctl start mysqld sudo systemctl enable mysqld -
查看root用户默认初始密码。
echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD) -
运行命令,启动MySQL安全安装向导。
sudo mysql_secure_installation-
输入
root用户初始密码。说明输入密码时界面不会显示密码的输入过程,请您确保输入的密码正确。
Securing the MySQL server deployment. Enter password for user root: -
提示root密码已过期,您需要设置新的
root用户密码,需要输入两次新密码。重要密码策略要求密码至少包含一个大写字母、一个小写字母、一个数字和一个特殊字符,并且密码总长度至少为8个字符。
The existing password for the user account root has expired. Please set a new password. New password: Re-enter new password: -
修改
root密码后,输入n,拒绝再次修改密码。Change the password for root ? (Press y|Y for Yes, any other key for No) : -
输入y,移除匿名用户。
说明移除匿名用户可以防止未经授权的用户在没有提供有效凭证的情况下访问您的 MySQL 服务器。
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,禁止MySQL的
root用户从远程登录。说明如果有特定需求允许
root用户远程访问,可以按Y以外任意键以允许远程 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,移除MySQL自带的
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,重新加载权限表,使之前所有更改生效。
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) :
-
Alibaba Cloud Linux 2
-
远程连接ECS实例。具体操作,请参见使用Workbench登录Linux实例。
-
安装MySQL数据库。
#添加MySQL官方源 sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el7-1.noarch.rpm #安装MySQL服务 sudo yum install -y mysql-server #启动MySQL服务并设置开机自启 sudo systemctl start mysqld sudo systemctl enable mysqld -
查看root用户默认初始密码。
echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD) -
运行命令,启动MySQL安全安装向导。
sudo mysql_secure_installation-
输入
root用户初始密码。说明输入密码时界面不会显示密码的输入过程,请您确保输入的密码正确。
Securing the MySQL server deployment. Enter password for user root: -
提示root密码已过期,您需要设置新的
root用户密码,需要输入两次新密码。重要密码策略要求密码至少包含一个大写字母、一个小写字母、一个数字和一个特殊字符,并且密码总长度至少为8个字符。
The existing password for the user account root has expired. Please set a new password. New password: Re-enter new password: -
修改
root密码后,输入n,拒绝再次修改密码。Change the password for root ? (Press y|Y for Yes, any other key for No) : -
输入y,移除匿名用户。
说明移除匿名用户可以防止未经授权的用户在没有提供有效凭证的情况下访问您的 MySQL 服务器。
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,禁止MySQL的
root用户从远程登录。说明如果有特定需求允许
root用户远程访问,可以按Y以外任意键以允许远程 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,移除MySQL自带的
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,重新加载权限表,使之前所有更改生效。
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) :
-
CentOS 8
-
远程连接ECS实例。具体操作,请参见使用Workbench登录Linux实例。
-
安装MySQL数据库。
#添加MySQL官方源 sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el8-1.noarch.rpm #安装MySQL服务 sudo dnf install -y mysql-server #启动MySQL服务并设置开机自启 sudo systemctl start mysqld sudo systemctl enable mysqld -
运行命令,启动MySQL安全安装向导。
sudo mysql_secure_installation-
输入y,启用
VALIDATE PASSWORD组件。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: -
输入2,设置密码策略要求密码至少包含一个大写字母、一个小写字母、一个数字和一个特殊字符,并且密码总长度至少为8个字符。
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: -
您需要设置新的
root用户密码,需要输入两次新密码。New password: Re-enter new password: Estimated strength of the password: 100重要密码策略要求密码至少包含一个大写字母、一个小写字母、一个数字和一个特殊字符,并且密码总长度至少为8个字符。
-
输入y,继续使用提供的密码。
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : -
输入y,移除匿名用户。
说明移除匿名用户可以防止未经授权的用户在没有提供有效凭证的情况下访问您的 MySQL 服务器。
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,禁止MySQL的
root用户从远程登录。说明如果有特定需求允许
root用户远程访问,可以按Y以外任意键以允许远程 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,移除MySQL自带的
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,重新加载权限表,使之前所有更改生效。
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) :
-
CentOS 7
-
远程连接ECS实例。具体操作,请参见使用Workbench登录Linux实例。
-
安装MySQL数据库。
#添加MySQL官方源 sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el7-1.noarch.rpm #安装MySQL服务 sudo yum install -y mysql-server #启动MySQL服务并设置开机自启 sudo systemctl start mysqld sudo systemctl enable mysqld -
查看root用户默认初始密码。
echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD) -
运行命令,启动MySQL安全安装向导。
sudo mysql_secure_installation-
输入
root用户初始密码。说明输入密码时界面不会显示密码的输入过程,请您确保输入的密码正确。
Securing the MySQL server deployment. Enter password for user root: -
提示root密码已过期,您需要设置新的
root用户密码,需要输入两次新密码。重要密码策略要求密码至少包含一个大写字母、一个小写字母、一个数字和一个特殊字符,并且密码总长度至少为8个字符。
The existing password for the user account root has expired. Please set a new password. New password: Re-enter new password: -
修改
root密码后,输入n,拒绝再次修改密码。Change the password for root ? (Press y|Y for Yes, any other key for No) : -
输入y,移除匿名用户。
说明移除匿名用户可以防止未经授权的用户在没有提供有效凭证的情况下访问您的 MySQL 服务器。
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,禁止MySQL的
root用户从远程登录。说明如果有特定需求允许
root用户远程访问,可以按Y以外任意键以允许远程 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,移除MySQL自带的
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,重新加载权限表,使之前所有更改生效。
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) :
-
Ubuntu/Debian
-
远程连接ECS实例。具体操作,请参见使用Workbench登录Linux实例。
-
运行以下命令,更新系统apt仓库,确保您的系统包列表为最新。
sudo apt-get update -
安装MySQL APT存储库。
#下载APT配置包 sudo wget https://dev.mysql.com/get/mysql-apt-config_0.8.33-1_all.deb #使用 dpkg 命令安装下载的 MySQL APT 配置包 sudo dpkg -i mysql-apt-config_0.8.33-1_all.deb-
(可选)若mysql-apt-config脚本检测到您的操作系统版本不是其正式支持的版本,它将显示一个界面,让您选择最接近您系统版本的支持选项以继续安装。
说明获取系统别名请参见部署MySQL常见问题。
Configuring mysql-apt-config The detected system (ubuntu bionic) is not supported by MySQL. If you believe the platform is compatible with one of the supported systems, one of the corresponding repositories may be selected. Add repository to unsupported system? ubuntu lunar ubuntu focal ubuntu jammy ubuntu noble ubuntu oracular debian bookworm abort <Ok> -
配置页面会列出待安装的全部组件,其中包括MySQL Server 、Client、Tool等一系列组件,确认版本及待安装组件无误,按方向键切换到
OK并回车。Configuring mysql-apt-config The MySQL APT Repository features MySQL Server along with a variety of components. You may select the desired products to install and update from the official MySQL Repository, and also select the associated version series. Select "Ok" to save the configuration, and then execute "apt update" to load the selected package list. This configuration can be updated later, depending on your needs. Which MySQL product do you wish to configure? MySQL Server & Cluster (Currently selected: mysql-8.4-lts) MySQL Connectors (Currently selected: Enabled) Ok <Ok>
-
-
安装MySQL服务器。
重要在安装过程中,为MySQL的
root用户设置密码时,需遵循以下密码策略:密码必须包含至少一个大写字母、一个小写字母、一个数字和一个特殊字符,且总长度不少于8个字符。输入您的密码按回车键确认,输入两次后完成设置。在安装过程中弹出 Configuring mysql-community-server 配置对话框,提示为 MySQL root 账户设置强密码;若留空则启用 UNIX socket 认证。在 Enter root password 输入框中输入密码,然后选择 Ok。在 Configuring mysql-community-server 对话框中,在 Re-enter root password 输入框中再次输入 root 密码进行确认,然后选择 Ok。请勿与任何人共享该密码。#更新包列表 sudo apt update #安装 MySQL 服务器 sudo apt install -y mysql-server #启动 MySQL 服务 sudo systemctl start mysql #检查启动状态 sudo systemctl status mysql #在系统启动时自动启动。 sudo systemctl enable mysql -
运行命令,启动MySQL安全安装向导。
sudo mysql_secure_installation-
输入
root用户密码。 -
输入y,启用
VALIDATE PASSWORD组件。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: -
输入2,设置密码策略要求密码至少包含一个大写字母、一个小写字母、一个数字和一个特殊字符,并且密码总长度至少为8个字符。
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: -
输入n,拒绝修改root用户密码。
说明如果您需要更改
root用户密码,可以输入y以进行修改。 -
输入y,移除匿名用户。
说明移除匿名用户可以防止未经授权的用户在没有提供有效凭证的情况下访问您的 MySQL 服务器。
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,禁止MySQL的
root用户从远程登录。说明如果有特定需求允许
root用户远程访问,可以按Y以外任意键以允许远程 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,移除MySQL自带的
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,重新加载权限表,使之前所有更改生效。
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) :
-
添加远程访问MySQL用户
-
远程连接ECS实例。具体操作,请参见使用Workbench登录Linux实例。
-
配置安全组时,MySQL 默认使用3306端口。确保实例安全组的入站规则开放3306端口。如果选择了不同的端口,请根据实际情况调整安全组设置。具体步骤请参考添加安全组规则。
-
执行命令创建用户并设置其可以远程访问MySQL所有数据库。
重要-
请将代码中的
<username>替换为创建MySQL用户时所用的用户名。 -
请将代码中的
<password>替换为创建MySQL用户时所用的密码。需遵循以下密码策略:密码必须包含至少一个大写字母、一个小写字母、一个数字和一个特殊字符,且总长度不少于8个字符。
#执行后需要输入root用户密码 sudo mysql -uroot -p \ -e "CREATE USER '<username>'@'%' IDENTIFIED BY '<password>';" \ -e "GRANT ALL PRIVILEGES ON *.* TO '<username>'@'%' WITH GRANT OPTION;" \ -e "FLUSH PRIVILEGES;" -
-
测试所配置的用户名及密码是否生效。
#<username>是您新创建用户的用户名,执行后需要输入您的新用户密码 sudo mysql -u<username> -p