ubuntu20.04 install mysql database

1. Install the mysql database:

sudo apt update
sudo apt install mysql-server

Generally, MYSQL service will start automatically after installation. Check whether MYSQL is running normally:

sudo systemctl status mysql

The following output indicates that the MYSQL service is enabled and running normally:

mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:>
     Active: active (running) since Sat 2021-06-05 09:37:36 CST; 3min 22s ago
   Main PID: 36265 (mysqld)
     Status: "Server is operational"
...

2. Set password:

The MySQL installation comes with a script called mysql_secure_installation. You can use it to set the security of our MYSQL server!

①Call this script:

sudo mysql_secure_installation

②According to the system prompts, follow the requirements step by step: the
first step: set the VALIDATE PASSWORD component: enter Y; the

Insert picture description here

second step: the password setting strategy is divided into three levels: low, medium, and high. There are also three levels of password format requirements, I choose the middle level, that is, enter 1;

Insert picture description here


Step 3: Set the password of the MYSQL root user (the bottom line of the figure below shows the strength of the password.);

Insert picture description here


Below: you will be asked to delete anonymous users, restrict root user access to the local computer, delete the test database and reload privileges table. All y is over!

3. Login:

To interact with the MySQL server using the Ubuntu command line, the MySQL client utility is used, which is installed as a dependency of the MySQL server software package.

Two ways to log in to MYSQL:

The first one: log in to the MYSQL server as the root user:
(On MySQL 8.0, auth_socket by default, the root user is authenticated through the plug-in, so this method does not need to enter a password to log in directly!)

sudo mysql
Insert picture description here


The second type: log in as the root user, but need to enter a password:
(more secure!)

mysql -u root -p

回车

输入密码
Insert picture description here

Note: If you want to use an external program to log in to the MYSQL server as the root user, you need to change the authentication method from auth_socket to mysql_native_password. This is done by running the following two lines of commands in the MYSQL service:

mysql > ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你设置的密码';
mysql > FLUSH PRIVILEGES;

Attention!
If you execute the previous two commands and change the MYSQL authentication method and then log in to MYSQL, you can only log in with a password!
mysql -u root -p way! ! !


To perform simple MYSQL command operations (for example: add and delete MYSQL users; add and delete user permissions...), click me to read this article! ! !