1. Preparations before installation
1. Confirm whether MySQL currently exists on the server
rpm -qa | grep mysql
If there is no rpm command, we need to install rpm, command: yum install rpm
Note: If empty information is returned, it means that MySQL is not installed in the current environment.
2. View the location of MySQL-related configuration files, source code and help documents
whereis mysql
ps: If there is, the mysql related path will be returned
3. Find the corresponding directory through the above and delete the corresponding mysql related files.
rpm -e --nodeps mysql-xxxx
-- nodeps parameter, skip dependency checking
Note: If mysql is not installed in your current environment, please ignore steps 2 and 3.
2. Download MySQL
Download address: MySQL :: Download MySQL Community Server
1. Select the corresponding Linux version and x86/x64 to download
2. Download the necessary MySQL components Server, Client, Common, and Libs
3. Upload to a location on the server
It can be transferred through remote file transfer tools (xftp), and other file transfer tools are not limited to xftp.
3. Install MySQL components
premise:
Installation sequence: common→libs→client→server
Use rpm -ivh package name to install components
Note: In the parameter ivh, i-install installation; v-verbose progress bar; h-hash hash verification
1. Install common components
rpm -ivh mysql-community-common-5.7.32-1.el7.x86_64.rpm
2. Install libs components
rpm -ivh mysql-community-libs-5.7.32-1.el7.x86_64.rpm
3. Install client component
rpm -ivh mysql-community-client-5.7.32-1.el7.x86_64.rpm
4. Install server components
rpm -ivh mysql-community-server-5.7.32-1.el7.x86_64.rpm
5. View MySQL installation results
rpm -qa | grep mysql
6. Check the status of MySQL using
systemctl status mysqld. It is not started by default.
7. Start MySQL
systemctl start mysqld
4. MySQL configuration
Now we can't log in to MySQL because the password is encrypted, check the command
cat /var/log/mysqld.log | grep password
1. Add skip-grant-tables to the /etc/my.cnf file
vim /etc/my.cnf
Description: skip-grant-tables is an option in the MySQL configuration file (my.cnf), which is used to skip permission verification when the MySQL service starts, so that all users have full access rights. This option is usually only used for specific maintenance tasks and is not suitable for long-term use in a production environment. (So after we log in and change the password, we turn it off)
Save and exit: wq
Restart MySQL
systemctl restart mysqld
As shown in the figure below: Login successful.
1. Change password
(1) Switch to the mysql library
use mysql;
(2) View current user information: Execute the following SQL query statement to view the user name and related permissions of the current user
select user, host, authentication_string from user;
Note: In previous versions, the field name of the password field was password. In version 5.7, it was changed to authentication_string.
(3) Use sql command to modify the root account password
update user set authentication_string=password('<new_password>') where user='用户';
(4) Flush privileges;
3. Exit the database
exit
4. Delete skip-grant-tables in the /etc/my.cnf file, save and exit, and restart MySQL.
vim /etc/my.cnf
:wq
systemctl restart mysqld
5. Log in again to test
mysql -u root -p
Enter password: Login successful
Encountered after logging in and need to reset password
ALTER USER 'root'@'localhost' IDENTIFIED BY 'H+oAVQ2Bq1';
Flush privileges;
5. Reset the database data storage location
1. View the default database path storage location
show variables like "%datadir%";
2. Create a directory to store mysql data
mkdir /data/mysql/data
3. Stop mysql
systemctl stop mysqld
4. Copy the mysql storage in the default location to the location we want
cp -arp /var/lib/mysql /data/mysql/data
The -arp parameter is to copy the original file directory and file permissions directly to the target path.
5. Set permissions
chown -R msyql:mysql /data/mysql/data/mysql
6. Modify configuration file
vim /etc/my.cnf
7. Restart mysql to see if it takes effect
systemctl start mysqld
8. Connect to mysql
mysql -u root -p
The error is reported as follows:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
Reason: The sock file cannot be found
Solution:
(1) First find the .sock file
find / -name mysql.sock
(2) Add the following to /etc/my.cnf
[client]
default-character-set=utf8
socket=/data/mysql/data/mysql/mysql.sock
[mysql]
default-character-set=utf8
socket=/data/mysql/data/mysql/mysql.sock
9. Restart the database
10. Log in again
11. Check whether the database storage location is successful
6. Enable mysql remote access permissions and allow remote connections
1. Log in to mysql
mysql -u root -p
2. Select mysql database
use mysql
3. Modify configuration
update user set host='%' where user='root';
4. Refresh the permissions to make them effective
flush privileges;
5. Exit
exit
reference: