当前位置:网站首页>Detailed tutorial on installing MySQL database in Linux environment (including uninstallation and password reset process)

Detailed tutorial on installing MySQL database in Linux environment (including uninstallation and password reset process)

2022-06-11 19:43:00 Kexiaonan

This tutorial is for centos7/8,mysql 5.x

1、 uninstall mysql( important )

In the installation mysql Make sure you don't have... In your system before mysql, Even if you are a newly installed system or a newly purchased cloud service , You also need to check whether there is mysql Related documents , This is important !( Some images of Tencent cloud include by default mysql The file of ), If it already exists before installation mysql Related documents , Will lead to subsequent mysql Installation failed .
Completely delete mysql step :
(1) Use rpm Command to view installed packages
Enter the following command to check if mysql Software :

rpm -qa | grep mysql

If you find the relevant documents , for example
 Insert picture description here
Use rpm -e file name To delete the file , Take the above file as an example , Execute the following command :

rpm -e mysql57-community-release-el7-10.noarch

Reuse rpm -e Command to delete files , Until all files are completely deleted
(2) Use yum Uninstall the installed mysql

yum remove mysql mysql-server mysql-libs mysql-server

(3) Search for mysql file
The global search name contains mysql All files for

find / -name '*mysql*'

Delete all the search results mysql file , If not installed mysql, Usually search out /var/lib/* and /usr/share/* These two directories contain mysql file . Delete file command :

#  With  /var/lib/mysql  For example 
rm -rf /var/lib/mysql

Use rm -rf Command to delete all searched mysql file .

Linux Copy shortcut keys in :ctrl + insert, Paste shortcut :shift + insert Or use the middle mouse button

Finally, check again :

#  Check 1
rpm -qa|grep mysql

#  Check 2
find / -name '*mysql*'

If any, continue to delete , Until it is deleted !

2、 download mysql The installation files

Linux It is recommended to use RPM Package to install mysql.
download :

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

rpm install :

rpm -ivh mysql-community-release-el7-5.noarch.rpm

yum install mysql-server:

yum install -y mysql-server

If you don't have permission , You can execute the following command to set permissions (root The user does not need to execute ):

chown -R mysql:mysql /var/lib/mysql/

Start the service :

systemctl start mysqld.service

see mysql Running state :

service mysqld status

 Insert picture description here
Check the initial password :

grep 'password' /var/log/mysqld.log

 Insert picture description here
The initial password is marked in the red box , However, in some cases, the initial password cannot be queried , How to change the password will be said later .
Copy the initial password , Log in :

mysql -u root -p

After typing the command , Click enter , Will let you enter the password , Then input the initial password just now ( It is not visible when inputting the password ), Of course, paste is supported , You can copy the initial password after , Then paste . If there is no password , Just go back .
 Insert picture description here
After logging in successfully, it will display :
 Insert picture description here
View all databases :
 Insert picture description here
Here we are ,mysql The database is installed .

3、 modify mysql password

In many cases , Need to change the initial password , But if you can't see the initial password in the previous steps , There is no way to log in to the database , Then you need to change the password . Let's take the inability to view the initial password as an example ( Or forget your login password ), Briefly introduce the password modification process .
(1) Set skip login password
open mysql Configuration file for :

vi /etc/my.cnf

Press... After opening the file a Key to enter edit mode , stay [mysqld] Add... Below the configuration skip-grant-tables, As shown in the figure below :
 Insert picture description here
Then press esc key , Input :wq Save the file .

(2) restart mysql service

service mysqld restart

(3) Get into mysql

mysql -u root -p

Press enter after entering the above command , Will skip password verification , Direct access to mysql in .

(4) Change user login password
Get into mysql library :

use mysql

 Insert picture description here
Change Password :

update user set password=password(' The password you set ') where User='root' and Host='localhost';

Be careful : Set up Host=‘localhost’ May cause the remote connection to fail !

Update cache :

flush privileges;

sign out mysql:

exit

(4) Restore profile
Re enter my.cnf Comment out the document skip-grant-tables, The effect is shown below
 Insert picture description here
Press esc Key to exit edit mode , Input :wq Save the file .

Restart again mysql service :

service mysqld restart

Then connect to the database , Enter your modified password to see if you can enter , If you can enter, it means that the password has been modified successfully , Otherwise, follow the steps again .

原网站

版权声明
本文为[Kexiaonan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111933247585.html