Life is full of knowledge, so is work! Installing MySQL on Linux more than once in the past is a breeze, but there are a lot of details worth learning!

Installation package Selection

Why RPM?

Software installation on Linux series generally includes source code compilation and installation, RPM (or DPKG), yum, etc. The advantages and disadvantages are mainly reflected in whether the network environment is required, whether it can automatically detect dependencies, and whether it is convenient to upgrade and uninstall. As non-operation and maintenance personnel, they also need to have a basic understanding.

The source code installation is supported offline, but even if you can automatically create makefiles and other files to simplify the installation, the whole process is complicated, making upgrading and uninstalling more troublesome. For some of the official release of the software, can only choose this way.

Yum can automatically monitor dependencies, and the installation information can be written into the system’s software management program during the installation process, upgrade and uninstall are more convenient. The disadvantage is that most cases need a good network environment, especially some software without domestic mirror, speed touching. (In fact, it is also possible to use yum offline in the same directory as the RPM package, provided the dependencies are locally available.)

RPM is equivalent to an official compiled installation package. It supports offline detection and dependency detection. However, some software does not have RPM packages. Therefore, TO a certain extent, RPM has the advantages of the above two methods.

In practice, it depends on your environment, and generally RPM takes precedence over source code.

Installation environment:

Centos7.8 2003 (minimum installation, root permission)

Mysql Community Edition 8.0.16

Select upload the downloaded official bundle file.

Mysql – 8.0.16-2. El7. X86_64. RPM – bundle. The tar

You can also download unpacked files from the official website.

Or download it online (you can also copy the address after wget directly to the browser address bar locally and the download will start automatically)

Wget HTTP: / / https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.16-2.el7.x86_64.rpm-bundle.tarCopy the code

Unpack the

Note that the decompression command is -xvf without z

Tar XVF mysql - 8.0.16-2. El7. X86_64. RPM - bundle. The tarCopy the code

Contains 8 files when unzipped,

Check the environment before installation

check

After MySQL was acquired, the original authors of MySQL were concerned about the risk of MySQL being a closed source and developed Mariadb on a branch of MySQL. Some Linux distributions have since installed Mariadb as the default database system, and mariadb is theoretically compatible with MySQL, and in some ways better. (Of course, I haven’t tested it.)

rpm -qa | grep -i mariadb
Copy the code

Possible results show

Mariadb - libs - 5.5.65-1. El7. X86_64Copy the code

Remove the dependence on

RPM - ev -- nodeps mariadb - libs - 5.5.65-1. El7. X86_64Copy the code

The installation

The installation sequence

The installation should follow the order common – > LiBS – > Client – > Server, otherwise the installation process will remind you of the dependency order. Other installation packages are optional.

RPM RPM -ivh mysql-community-common-8.0.16-2.el7.x86_64. RPM RPM -ivh mysql-community-libs-8.0.16-2.el7.x86_64. RPM Mysql - community - the client - 8.0.16-2. El7. X86_64. RPM RPM - the ivh mysql - community - server - 8.0.16-2. El7. X86_64. RPMCopy the code

As mentioned at the beginning of this article, you can also use the yum command in the unzipped installation file instead of RPM (optional)

Yum install mysql - community - common - 8.0.16-2. El7. X86_64. RPM...Copy the code

Start and change password

Start the

 systemctl start mysqld
Copy the code

Check the status

systemctl status mysqld
Copy the code

View the initial random password

 cat /var/log/mysqld.log | grep password
Copy the code

If the following information is displayed, the initial password is tC0. +kB? BqCg

2020-09-02T05:30:06.739311z 5 [Note] [my-010454] [Server] A temporary password is generated for root@localhost: tC0; +kB? BqCgCopy the code

Log in as the root user

mysql -u root -p
Copy the code

Paste or enter the initial password, note that Linux for security, MySQL command line may not respond or, but the password has been entered

Change the password

You must change the randomly generated password after logging in to the system for the first time. Otherwise, you cannot perform other operations.

Password is required to

According to the default password component of MySQL8.0, the password must contain digits, uppercase and lowercase letters, and special characters. The length of the password must be at least 8 characters. Otherwise, a message will be displayed indicating that the password does not comply with the rules. This is recommended. If you want to set your password to a simple 123456 (not recommended, as a good habit), see the last section of this article, Password questions.

Test with 123456 as the password

ALTER user 'root'@'localhost' IDENTIFIED BY '123456';
Copy the code

Here we set the password to root.123456 note that R is capitalized and t is followed by an English period.

ALTER user 'root'@'localhost' IDENTIFIED BY 'Root.123456';
Copy the code

The remote login permission is enabled

In this case, you can log in to the MySQL database only as the root user. For security purposes, you can log in to the MySQL database only as the root user. The normal practice is to assign root permission to other roles for remote login.

However, the remote login permission of root is forcibly enabled. Otherwise, this article will be too long to write about role assignment.

The essence of the open permission is to modify the host attribute of the user table in the mysql database, so that it can be connected by IP outside the local

use mysql;
​select host,user from user;
​update user set host='%' where user ='root';
Copy the code

Refresh permissions are executed after executing the above statement:

flush privileges;
Copy the code

Opening firewall Ports

After the remote login permission of the root user is enabled, the root user cannot be directly connected to external users because the MySQL port number 3306 is disabled by default in Linux.

Note: The use of cloud servers such as Ali Cloud also requires opening ports in the security group policy in the console, which is omitted here.

Example Check whether port 3306 is enabled

 firewall-cmd --query-port=3306/tcp
Copy the code

Open 3306

firewall-cmd --zone=public --add-port=3306/tcp --permanent
Copy the code

Reload the firewall

firewall-cmd --reload
Copy the code

At this point our program can connect directly to MySQL.

Configuration file changes (optional)

To change the location of mysql data and logs, run the /etc/my. CNF configuration file.

vim /etc/my.cnf
Copy the code

The following is not required.

Navicat remote connection problem (optional)

When using the Navicat or Sqlyog tools, newer versions of the tool already connect directly, so you can use Navicat to test if the connection is successful before starting this step.

However, the old version of Navicat may fail to match the encryption rules of mysql when connecting to mysql8.0. The encryption rules here refer to the algorithm used to encrypt and save your password.

Change the encryption rule of mysql

Viewing Encryption Rules

show variables like "default_authentication%";
Copy the code

The result could be

Modify the rule

Alter user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your password ';Copy the code

The Navicat connection is successful.

Password questions (optional)

Changing password Rules

Note: This step can be omitted if you follow the previous rules for complex passwords.

Again, it is recommended to set your password to a high level of complexity (as long as you don’t forget). If you still want to set the password to simple, look below.

Log on to the MySQL

Viewing password rules (Normally, password related components have been installed by default)

SHOW VARIABLES LIKE 'validate_password.%';
Copy the code

The results showed

+--------------------------------------+--------+ | Variable_name | Value | +--------------------------------------+--------+ |validate_password.check_user_name | ON | | validate_password.dictionary_file | | | validate_password.length | 8 | | validate_password.mixed_case_count | 1 | | validate_password.number_count | 1 | | validate_password.policy | MEDIUM | | validate_password.special_char_count | 1 | + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - + -- -- -- -- -- -- -- -- + 7 rows in the set (0.00 SEC)Copy the code

Validate_password. The value of the policy

You can specify the validate_password.policy value using the numeric values 0, 1, 2 or the corresponding symbol values LOW, MEDIUM, and STRONG

Policy Tests Performed
`0` or `LOW` Length
`1` or `MEDIUM` Length; numeric, lowercase/uppercase, and special characters
`2` or `STRONG` Length; numeric, lowercase/uppercase, and special characters; dictionary file

If you want to use a simple password similar to 123456, you need to change the password rules and password length

Mysql8.0 and later

set global validate_password.policy=0;​
set global validate_password.length=1;
Copy the code

In fact, even if the password length is set to 1, the minimum length is no less than the value of the following function, that is, the default length is always no less than 4

validate_password.number_count+ validate_password.special_char_count+ (2 * validate_password.mixed_case_count)
Copy the code

Version 5.7 and earlier

set global validate_password_policy=0;​
set global validate_password_length=1;
Copy the code

Change password

Change the password to 123456

ALTER user 'root'@'localhost' IDENTIFIED BY '123456';
Copy the code

The last

Compared to the entire installation process, in fact, it is worth pondering, just a password setting and saving, MySQL provides different components, set up different rules and policies, to maximize the balance of convenience and security, in a sense, is also worth learning ingenuity!