MySQL is a relational database management system and is often used to build the LAMP or LNMP environment. This topic describes how to install, configure, and connect to a MySQL database on an Elastic Compute Service (ECS) instance that runs CentOS 8.
Prerequisites
An ECS instance is created. For more information, see Create an instance by using the wizard.
- Instance type: ecs.c6.large
- Operating system: CentOS 8.2 64-bit public image
- Network type: Virtual Private Cloud (VPC). A public IP address is assigned to the instance.
Background information
- Configuration file: /etc/my.cnf
- Data storage: /var/lib/mysql
- Command files: /usr/bin and /usr/sbin
Step 1: Install MySQL
- Connect to the ECS instance that runs CentOS 8. For more information,see Connect to a Linux instance by using a password.
- Change the CentOS 8 repository address.
CentOS 8 has reached its end of life (EOL). In accordance with Linux community rules, all content has been removed from the following CentOS 8 repository address: http://mirror.centos.org/centos/8/. If you continue to use the default CentOS 8 repository configurations on Alibaba Cloud, an error is reported. To use specific installation packages of CentOS 8, change the CentOS 8 repository address. For more information, see Change CentOS 8 repository addresses.
- Run the following command to install MySQL:
sudo dnf -y install @mysql
- Run the following command to view the MySQL version:
mysql -V
A command output similar to the following one indicates the MySQL version.
Step 2: Configure MySQL
- Run the following command to start MySQL and configure MySQL to start on instance startup:
sudo systemctl enable --now mysqld
- Run the following command to check whether MySQL is started:
sudo systemctl status mysqld
If the command output containsActive: active (running)
, MySQL is started. - Run the following command to configure security settings for MySQL and set the password:
mysql_secure_installation
After you run the command, perform the following operations based on the command prompts:- Enter Y and press the Enter key to make the configurations.
- Enter 2 as the password strength and press the Enter key.
0 indicates a low password strength, 1 indicates a medium password strength, and 2 indicates a high password strength. We recommend that you use a high password strength.
- Enter a new password for MySQL and confirm it.
In this example, the password is set to
PASSword123!
. - Enter Y and press the Enter key to use the password.
- Enter Y and press the Enter key to delete anonymous users.
- Enter Y and press the Enter key to disallow the root account to connect to MySQL.
- Enter Y and press the Enter key to delete the
test
database and the access permissions on thetest
database. - Enter Y and press the Enter key to reload privilege tables.
Step 3: Connect to the MySQL database
We recommend that you use a non-root account to connect to the MySQL database. In this example, an account is created to connect to MySQL.
- Add an inbound rule to a security group of the ECS instance to allow traffic on the port required by MySQL. The default port number of the MySQL database is 3306. You must add inbound rules to a security group of the ECS instance and allow traffic on port 3306. For more information, see Add a security group rule.
- Create and configure an account to connect to MySQL on the instance.
- Connect to MySQL by using the
dms
account.- We recommend that you use Data Management Service (DMS) provided by Alibaba Cloud to connect to the MySQL database. For more information, see Register an ApsaraDB instance.
- You can use connection tools such as MySQL Workbench and Navicat on your computer to connect to the MySQL database.