[Linux] Uninstall and install MySQL (Ubuntu version)

Recently, I came to Linux to study again, because I wanted to gain exposure to cloud server-related knowledge.

So the blogger compiled some knowledge about Linux,

Friends are welcome to like and collect, be happy every day and write code happily!

 For Linux series articles, please click here  Linux Tutorial Column

 e490630c9adb4b3f870d0d7013700eaa.gif

Table of contents

1. Uninstall MySQL

1.1 View MySQL dependencies

1.2 Uninstall mysql server and common 

1.3 Check dependencies again to ensure they are deleted cleanly 

2. Install MySQL

2.1 Update software library

2.2 Install MySQL server and client

2.3 Configure MySQL

2.4 View MySQL service

 3. Basic use of MySQL database under Linux

3.1 Start the MySQL database service

3.2 Restart the MySQL database service

3.3 Stop the MySQL database service

3.4 View MySQL running status

3.5 Set the MySQL service to start automatically at boot

3.6 Stop the MySQL service from starting at boot

3.7 Modify the MySQL configuration file

4. Change password and access permissions

4.1 Change password

4.2 Open permissions

5. Use Navicat connection test


This issue brings you the Ubuntu version of Linux installation of the MySQL database. Without further ado, let’s get into the tutorial!

The whole article can be divided into four parts, each part is full of details. If this tutorial is helpful to you, please like, comment and collect it!

1. Uninstall MySQL

Some people may have questions after seeing this.

Why should you uninstall MySQL? Haven't I installed MySQL yet? Note, this is for those who have tried to install MySQL but failed to install it successfully!

If you have never installed MySQL before, please jump directly to step two!

In order to prevent various complications from occurring in the second, third, and fourth steps below, the operation here is very necessary.

The following takes MySQL5.7 version as an example:

1.1 View MySQL dependencies

First execute the su command to enter the super user,

Here is a little knowledge, the super user can switch back to the ordinary user and execute the exit command.

After entering the password, execute the following command to view mysql dependencies: 

dpkg --list | grep mysql

 Afterwards, code similar to this will appear:

ii  mysql-client                               5.7.33-0ubuntu0.16.04.1                         all          MySQL database client (metapackage depending on the latest version)
ii  mysql-client-5.7                           5.7.33-0ubuntu0.16.04.1                         amd64        MySQL database client binaries
ii  mysql-client-core-5.7                      5.7.33-0ubuntu0.16.04.1                         amd64        MySQL database core client binaries
ii  mysql-common                               5.7.33-0ubuntu0.16.04.1                         all          MySQL database common files, e.g. /etc/mysql/my.cnf
ii  mysql-server                               5.7.33-0ubuntu0.16.04.1                         all          MySQL database server (metapackage depending on the latest version)
ii  mysql-server-5.7                           5.7.33-0ubuntu0.16.04.1                         amd64        MySQL database server binaries and system database setup
ii  mysql-server-core-5.7                      5.7.33-0ubuntu0.16.04.1                         amd64        MySQL database server binaries

1.2 Uninstall mysql server and common 

 Then uninstall mysql-common and execute the following code:

sudo apt remove mysql-common

Next we uninstall and clear mysql5.7 and enter the following code:

sudo apt autoremove --purge mysql-server-5.7

When uninstalling, the following graphical interface prompt will appear:

9ee2900b9a0145708a9bdfdce3b7f864.png Just select ‘Yes’ via the keyboard.​ 

Next, clear the remaining data and enter the following code:

dpkg -l | grep ^rc| awk '{print$2}'| sudo xargs dpkg -P

After execution, the following prompts will appear:

sh: 0: getcwd() failed: No such file or directory

 Note that this is not an error, don’t worry, continue to the next step.

1.3 Check dependencies again to ensure they are deleted cleanly 

Finally, check the dependencies again and enter the following code:

dpkg --list | grep mysql

If the output is empty, it means that mysql has been completely uninstalled. If it is not empty, then we will continue to delete and uninstall.

Continue to enter the following code:

 sudo apt autoremove --purge mysql-apt-config

So far, mysql on Ubuntu has been completely deleted and uninstalled!

2. Install MySQL

2.1 Update software library

Execute the following command to update the software library:

sudo apt-get update

2.2 Install MySQL server and client

Install MySQL server:

sudo apt-get install mysql-server

During the installation process, you will be prompted to create a root password,

Use a simple password and make sure you remember it as you will need it later.

ee0b4744d8804947945aaa8f88f7be44.png

 Install MySQL client:

sudo apt-get install mysql-client

The difference between mysql-server and mysql-client

mysql-server is a MySQL core program that will install the MySQL database server and is used to generate and manage multiple database instances, persist data and provide a query interface (SQL) for different clients to call.

mysql-client is a tool for operating database instances, allowing you to connect to the MySQL server and use the query interface. It will provide you with the MySQL command line program.

If you just need to connect to a remote server and run queries, just install mysql-client.

If the server only provides connection services, you only need to install mysql-server.

The blogger’s suggestion is to install them all, because you can use whatever connection method you want in the future!

2.3 Configure MySQL

Execute this command below:

sudo mysql_secure_installation

A prompt similar to this will appear. Enter the MySQL password, which is the password set in step 2.2:

Securing the MySQL server deployment.

Enter password for user root: 

The things set by the mysql_secure_installation script are

1. If you need to install the password verification plug-in, enter Y:

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

 2. To set the password strength level, just enter 0, because now is the learning stage, so there is no need to make it so complicated:

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 

 3. Do you need to change the password of the root user? According to your needs. If you think the password in step 2.2 is not good,

Enter Y to modify. If no modification is required, enter other keys and press Enter:

Change the password for root ? ((Press y|Y for Yes, any other key for No) : 

4. Whether to delete anonymous users, enter Y:

Remove anonymous users? (Press y|Y for Yes, any other key for No) : 

 5. Whether to allow remote login, enter Y so that Windows can connect to the Linux database:

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) :

 6. Whether to delete the test database, just enter Y. We don’t need this database:

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : 

7. Whether to refresh the table permissions immediately, enter Y:

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : 

When you see the following prompt, it means that the database initialization configuration is complete!​ 

All done!

2.4 View MySQL service

Execute the following command to view the MySQL service:

systemctl status mysql.service

You will see output similar to the following, which means that the MySQL service is already running on the Linux system!

● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since 六 2023-02-11 11:56:58 CST; 12min ago
 Main PID: 12078 (mysqld)
   CGroup: /system.slice/mysql.service
           └─12078 /usr/sbin/mysqld

2月 11 11:56:57 sky-virtual-machine systemd[1]: Starting MySQL Community Server...
2月 11 11:56:58 sky-virtual-machine systemd[1]: Started MySQL Community Server.

 3. Basic use of MySQL database under Linux

3.1 Start the MySQL database service

sudo service mysql start
sudo systemctl start mysql.service

3.2 Restart the MySQL database service

sudo service mysql restart

Entering this command may result in the following content:

sh: 0: getcwd() failed: No such file or directory

 Just ignore it here and execute the following restart command.

sudo systemctl restart mysql.service

3.3 Stop the MySQL database service

sudo service mysql stop
sudo systemctl stop mysql.service

3.4 View MySQL running status

sudo service mysql status
sudo systemctl status mysql.service

3.5 Set the MySQL service to start automatically at boot

sudo service mysql enable
sudo systemctl enable mysql.service

3.6 Stop the MySQL service from starting at boot

sudo service mysql disable
sudo systemctl disable mysql.service

3.7 Modify the MySQL configuration file

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

One thing that needs to be modified here is to comment out a line in the content of the mysqld.cnf file. Otherwise, Navicat under Windows will not be able to connect to MySQL and error 2003 will be reported! :

# bind-address          = 127.0.0.1

Then press esc, enter a colon, enter wq to save and exit.​ 

4. Change password and access permissions

Next we can use the password set in step 2.2 to log in to MySQL!

4.1 Change password

First log in to MySQL:

mysql -uroot -p

Enter the password and press Enter to enter the MySQL command line mode, and then execute the change password command:

Set the minimum number of digits for the password and set it as needed. Here I set the minimum 6 digits.

set global validate_password_length=6;

 Set the password security level to facilitate learning and communication. Generally set it to LOW, because setting other passwords above is illegal:

set global validate_password_policy=LOW;

 Set a new password. You must set a password that meets the minimum number of digits or above, otherwise an error will be reported when logging in later:

The following is the password I set myself. You can set it according to your own habits.

set password=password('123456@');

4.2 Open permissions

Execute the following command:

grant all on *.* to 'root'@'%' identified by '123456@';

 This means granting all permissions to any host logged in as root.

The last step is very important, refresh permissions: 

flush privileges;

At this point we are ready to change the MySQL password and open permissions for external connections!

You can enter the exit; command to exit MySQL.

Next comes the crucial step, restarting the MySQL service.

The command is in step 3.2. After execution, enter the fifth link of testing.

5. Use Navicat connection test

Open navicat under Windows and conduct a connection test:

2d223f90657946b4b0bd75aa33a6342a.png

 Click on the connection test, the connection is successful!

At this point, the installation of MySQL (Ubuntu version) under Linux has been successful.

If you have questions about any of these steps

Everyone is welcome to comment and communicate, and answer questions and solve them together. Thank you all.

df47b58ed00b4f759112a3d9fe563845.gif

Guess you like

Origin blog.csdn.net/qq_62592360/article/details/128981652#comments_29916373