ready
Environmental information in this article:
Software | Version |
---|---|
CentOS | CentOS 7.4 |
MySQL | 8.0.x |
Update all packages of the system before installation
sudo yum update
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: You can find the latest rpm package name on the official website.
**2. Install **MySQL
# installation
sudo yum -y install mysql-community-server
# Start the daemon
sudo systemctl start mysqld
# Check status
sudo systemctl status mysqld
# View version
mysql -V
After installation, MySQL will start automatically when the system starts. If you don't want it to start automatically, you can use systemctl disable mysqld to close it.
3. change Password
During the MySQL installation process, a temporary password will be generated for the root user and stored in /var/log/mysqld.log. View through 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';
The password strength requirement is: no 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 can be used to modify security options.
sudo mysql_secure_installation
After running, set the following items in sequence:
Set according to personal situation.
User rights
1. Grant permissions
# 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'@'%';
# Give users remote access permissions to the specified database
GRANT ALL PRIVILEGES ON test_db.* TO 'user'@'%';
# Grant users remote access to all databases
GRANT ALL PRIVILEGES ON *.* TO 'user'@'%';
# Give users local access to all databases
GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost';
# Refresh permissions
FLUSH PRIVILEGES;
2. Withdraw permission
# Withdraw permission
REVOKE ALL PRIVILEGES ON *.* FROM 'test'@'%';
# Delete local user
DROP USER 'user'@'localhost';
# Delete remote user
DROP USER 'user'@'%';
# Refresh permissions
FLUSH PRIVILEGES;
3. Remote login
View user table information in the mysql database:
use mysql;
select host, user, authentication_string, plugin from user;
The host of the root user in the table defaults to localhost, which only allows local access. Authorize all permissions of the root user and set up remote access:
# Authorization
GRANT ALL ON *.* TO 'root'@'%';
# Refresh
FLUSH PRIVILEGES;
The default password encryption method for root users is: caching_sha2_password; and many graphical client tools may not support this encryption authentication method, and an error will be reported when connecting. Change the password again with the following command:
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'your password';
The password encryption method for root is specified here as mysql_native_password. If you want to change the default password encryption method, you can add a line in the /etc/my.cnf file:
default-authentication-plugin=mysql_native_password
If the server has a firewall enabled, port 3306 needs to be opened.
firewall-cmd --add-port=3306/tcp --permanent
firewall-cmd --reload
Note: If it is [Cloud Server] (https://cloud.tencent.com/product/cvm?from=10680), some service providers (such as Alibaba Cloud) need to go to the console to open the port.
Modify character encoding
The character set is a set of symbols and codes, check the character set configuration:
mysql> show variables like 'charac%';+--------------------------+--------------------------------+| Variable_name | Value |+--------------------------+--------------------------------+| character_set_client | utf8mb4 || character_set_connection | utf8mb4 || character_set_database | utf8mb4 || character_set_filesystem | binary || character_set_results | utf8mb4 || character_set_server | utf8mb4 || character_set_system | utf8 || character_sets_dir |/usr/share/mysql-8.0/charsets/|+--------------------------+--------------------------------+
The effective rule of character set is: Table inherits from Database, and Database inherits from Server. In other words, only character_set_server can be set.
The collation rules are a set of rules for comparing characters in a character set. Check the collation rules:
mysql> show character set like 'utf8%';+---------+---------------+--------------------+--------+| Charset | Description | Default collation | Maxlen |+---------+---------------+--------------------+--------+| utf8 | UTF-8 Unicode | utf8_general_ci |3|| utf8mb4 | UTF-8 Unicode | utf8mb4_0900_ai_ci |4|+---------+---------------+--------------------+--------+
Proofreading rules effective rules: If no proofreading rules are set, the character set adopts the default proofreading rules, for example, the proofreading rule for utf8mb4 is utf8mb4_0900_ai_ci.
The default character set of MySQL 8 has been changed to utf8mb4. If the default character set of the previous MySQL version is not utf8mb4, it is recommended to change to utf8mb4.
mb4 is most bytes 4. Why is utf8mb4 instead of utf8? The maximum character length of utf8 encoding supported by MySQL is 3 bytes. If a 4-byte wide character is encountered, an exception will be inserted.
The following are the steps to modify the character set of the old version of MySQL to utf8mb4, MySQL 8.0+ does not need to be modified.
# View configuration file location
whereis my.cnf
# open a file
vi /etc/my.cnf
Add character encoding configuration items:
[ client]default-character-set=utf8mb4
[ mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
Restart MySQL service
sudo systemctl restart mysqld
Use MySQL commands to check the character set configuration:
show variables like 'charac%';
reference
https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/
https://ken.io/note/centos-mysql8-setup
recommend:
Interested friends can follow the editor’s WeChat public account [Ma Nong's little things], more webpage production special effects source code and learning dry goods! ! !
to sum up
The above is a detailed explanation of the tutorial for installing MySQL 8 in CentOS 7 introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message, and the editor will reply to you in time. Thank you very much for your support to the ZaLou.Cn website!
Recommended Posts