A detailed tutorial on installing MySQL 8 in CentOS 7

  • 2020-12-05 17:32:21
  • OfStack

To prepare

Environmental Information:

软件 版本
CentOS CentOS 7.4
MySQL 8.0.x

Update all system packages before installation

sudo yum update

The installation

1. Add Yum package


wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
#  or  wget http://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm
sudo yum update
sudo rpm -ivh mysql80-community-release-el7-1.noarch.rpm

Note: The latest rpm package names can be found on the official website.

2. Install MySQL


#  The installation 
sudo yum -y install mysql-community-server
#  Start the daemon 
sudo systemctl start mysqld
#  Check the status 
sudo systemctl status mysqld
#  Check the version 
mysql -V

After installation, MySQL will start automatically when the system starts. If you do not want it to start automatically, you can use systemctl disable mysqld to turn it off.

Change your password

MySQL installation process for root user generated a temporary password stored in the/var log/mysqld log. See the following command:
sudo grep 'temporary password' /var/log/mysqld.log

Enter MySQL client to modify:


mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'your passowrd';
# ALTER USER 'root'@ IDENTIFIED BY 'your passowrd';

Password strength requirements are: not less than 12 characters, must contain uppercase letters, lowercase letters, numbers and special characters.

3. MySQL security configuration

MySQL includes a security Settings wizard script that you can use to modify security options.

sudo mysql_secure_installation

After running, the following items are set successively:

1. Change the password of root account
2. Password strength verification plug-in (recommended)
3. Remove anonymous users (removal is recommended)
4. Disable remote login for root account
5. Remove the test database (test)

Set up according to personal situation.

User permissions

1. Give permission


#  Create a local user 
CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
#  New remote user 
CREATE USER 'user'@'%' IDENTIFIED BY 'password';
#  New database 
CREATE DATABASE test_db;
#  View user permissions 
SHOW GRANTS FOR 'user'@'%';
#  Gives the user specified remote access to the database 
GRANT ALL PRIVILEGES ON test_db.* TO 'user'@'%';
#  Give the user remote access to all databases 
GRANT ALL PRIVILEGES ON *.* TO 'user'@'%';
#  Give the user local access to all databases 
GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost';
#  Refresh the permissions 
FLUSH PRIVILEGES;

2. Withdraw permission


#  Recover the permissions 
REVOKE ALL PRIVILEGES ON *.* FROM 'test'@'%';
#  Delete local users 
DROP USER 'user'@'localhost';
#  Delete remote user 
DROP USER 'user'@'%';
#  Refresh the permissions 
FLUSH PRIVILEGES;

3. Remote login

View the user table information in the mysql database:


use mysql;
select host, user, authentication_string, plugin from user;

host for root user in the form is localhost by default and only local access is allowed. Authorize all root users and set up remote access:


#  authorization 
GRANT ALL ON *.* TO 'root'@'%';
#  The refresh 
FLUSH PRIVILEGES;

root user's default password encryption method is: caching_sha2_password; While many graphics client tools may not support this method of encryption authentication, the connection will report an error. Change the password again with the following command:


ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'your password';

The root password encryption method is specified as mysql_native_password. If you want to change the default password encryption method, you can add 1 line in the /etc/ my.cnf file:


default-authentication-plugin=mysql_native_password

If the server has a firewall on, port 3306 needs to be opened.


firewall-cmd --add-port=3306/tcp --permanent
firewall-cmd --reload

Note: if it is a cloud server, some service providers (such as Aliyun) need to go to the console to open the port.

Modify character encoding

The character set is a set of symbols and encodings. Check the character set configuration:


#  The installation 
sudo yum -y install mysql-community-server
#  Start the daemon 
sudo systemctl start mysqld
#  Check the status 
sudo systemctl status mysqld
#  Check the version 
mysql -V
0

The character set valid rule is: Table inherits from Database, and Database inherits from Server. That is, you can set character_set_server only.

Collation rules are a set of rules used to compare characters in a character set. Check the collation rules:


#  The installation 
sudo yum -y install mysql-community-server
#  Start the daemon 
sudo systemctl start mysqld
#  Check the status 
sudo systemctl status mysqld
#  Check the version 
mysql -V
1

Valid collation rule: if no collation rule is set, the character set should be the default collation rule. For example, the collation rule of utf8mb4 is utf8mb4_0900_ai_ci.

The MySQL 8 default character set was changed to utf8mb4. If the default character set of MySQL is not utf8mb4, it is recommended to change to utf8mb4.

mb4 is most bytes 4. Why utf8mb4 and not utf8? The utf8 encoding supported by MySQL has a maximum character length of 3 bytes, and an exception is inserted if a wide character of 4 bytes is encountered.

Below are the steps for the old MySQL to modify the character set to utf8mb4. MySQL 8.0+ does not require modification.


#  The installation 
sudo yum -y install mysql-community-server
#  Start the daemon 
sudo systemctl start mysqld
#  Check the status 
sudo systemctl status mysqld
#  Check the version 
mysql -V
2

Add character encoding configuration items:


[client]
default-character-set=utf8mb4

[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci

Restart the MySQL service


#  The installation 
sudo yum -y install mysql-community-server
#  Start the daemon 
sudo systemctl start mysqld
#  Check the status 
sudo systemctl status mysqld
#  Check the version 
mysql -V
4

Use the MySQL command to check the character set configuration:


#  The installation 
sudo yum -y install mysql-community-server
#  Start the daemon 
sudo systemctl start mysqld
#  Check the status 
sudo systemctl status mysqld
#  Check the version 
mysql -V
5

reference

https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/

https://ken.io/note/centos-mysql8-setup

Recommendation:

Interested friends can pay attention to this site WeChat public number [code farmers that thing], more web production effects source code and learn dry goods oh!!

conclusion


Related articles: