CentOS7 uninstall and install mysql, open remote access permissions

I took a task, installed mysql on the company's server, and allowed remote login. Make a record here for subsequent use.

First check whether mysql is installed on the current machine, enter: mysql --version in the command line, if it is displayed, Insert picture description here
it means that the version of mysql5.6.44 is currently installed.

Note that if it is version 8.0 or later, the remote permission settings are slightly different

One, uninstall

1. Uninstall the installed part

First execute the view command to see what has been installed:

[root@localhost ~]# rpm -qa |grep -i mysql

Insert picture description here

2. Execute uninstall command

[root@localhost ~]# yum remove mysql-community mysql-community-server mysql-community-libs mysql-community-common mysql-community-client mysql-community-release

Check again: rpm -qa |grep -i mysql, make sure the installation part has been completely uninstalled.

3. Clean up remaining files

3.1 View the mysql file directory:
[root@localhost ~]# find / -name mysql

Insert picture description here
Delete all these files.

3.2 Delete the installation log file:
[root@localhost ~]# rm -f /var/log/mysqld.log

Otherwise, you will not find the installation password next time.

3.3 Delete history:
[root@localhost ~]# rm -rf /var/lib/mysql 

This one is extremely important, if you find that there is no password in /var/log/mysqld.log after installation, just execute this sentence! Then restart mysql (systemctl restart mysqld)

Two, installation

Since the latest version of the linux system starts, the default is Mariadb, not mysql, so you need to download the repo source of mysql in the official mysql repository, and then perform the installation operation.

1. Download the repo source of mysql and install it

Download the repo source of mysql
[root@localhost ~]# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

Then execute the installation command
[root@localhost ~]# rpm -ivh mysql-community-release-el7-5.noarch.rpm

2. Install mysql

[root@localhost ~]# yum install mysql-server
can be installed according to the steps, but after the installation is complete, there is no password, you need to reset the password. Check mysql again after installation

3. Log in

[root@localhost ~]# mysql -u root
will report an error when logging in for the first time because of the access permission of /var/lib/mysql. The following command changes the owner of /var/lib/mysql to the root user:
[root@localhost ~]# chown root /var/lib/mysql
Execute the login database command again: mysql -u root to log in to the database.

Tips:
MySQL5.7 strengthens the security of the root user, so a random password will be initialized after the first installation. The following is the way to view the initial random password
Command: grep'temporary password' /var/log/mysqld.log


If this step does not work, please refer to https://www.cnblogs.com/kynewu/p/8995749.html and use skip-grant-tables to change the password

4. Modify the default random password during installation

On the premise of logging in to the database in step 3

mysql> use mysql; 
mysql> update user set password=password('yourpassword') where user='root' and host='localhost'; 
mysql> flush privileges; 

You can change the password to yourpassword

You can also use the mysqladmin
format: mysqladmin -u username -p old password password new password
Example: mysqladmin -uroot -p123456 password 123

5. Set mysql to allow remote connections

Based on security considerations, the root account generally only has local access and does not have remote access permissions. Therefore, when the program and the database are not on the same server, we need to enable mysql remote access permissions. Here is the 5.0 version of the basic steps if the 8.0 version of the look is 5.3 :

5.1. Log in to mysql to authorize remote access for root and execute the following command:
 mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root";

If you need to authorize an ip, you only need to replace the% in the command with the corresponding ip address, such as:
GRANT ALL PRIVILEGES ON . TO root@“172.168.2.135” IDENTIFIED BY “root”; the
last “ "root" represents the remote login password you assigned to these addresses.

5.2, reload permissions

Because MySQL stores all permissions in the cache, it needs to be reloaded after making changes.

mysql> flush privileges;
5.3 After mysql 8.0 version
CREATE USER 'root'@'%' IDENTIFIED BY 'yourpassword';  -- 创建用户
GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION;  -- 授权
flush privileges; -- 刷新权限

Create a root user, the password is yourpassword

the above.

Guess you like

Origin blog.csdn.net/u012140251/article/details/91491445