MySQL: Local installation of MySQL database in Ubuntu system and its basic usage method (1)
✨Note : As shown in the figure below, the MySQL version used in this article is 8.0.30 , and the operating system uses Ubuntu22.04. For the basic application of the database, the installation and use of other versions and operating systems can also refer to the installation and use methods in this article.
1. Introduction to MySQL installation and related concepts
1.1 MySQL installation
Enter the following command in the terminal to download and install MySQL:
sudo apt install mysql-server
Enter when you want to continue the installation y
, and then wait for the content shown in the following figure to indicate that the installation is successful:
After executing the above instructions, we have installed a MySQL database server locally in the Ubuntu system.
1.2 Several important concepts of MySQL database
- 1. The concept of database server
As shown in the figure below, MySQL follows the working pattern of client-server architecture, which is designed by end users of clients to access resources from a central server using web services.
The core of the MySQL database is the MySQL server, which can be used as a separate program and is responsible for processing all database instructions, statements, and commands.
- 2. The concept of database
A database is a collection used to store records in an organized form, which allows us to keep data into tables, rows, columns and indexes so that we can often find relevant information. MySQL implements the database as a directory for storing all files in the form of tables, and we can easily access and manage records through the database.
- 3. The concept of users
After installing the MySQL server, it creates a
root
user by default, which is only used to access and manage the database. However, sometimes when it is necessary to grant administrative rights to others in a targeted manner, it is necessary to create non-root
users and grant them permissions for specific database operation functions.
USER
A MySQL user is a record in the table of the MySQL server , including the login information, permissions, and host information of the MySQL account. For users, a user must be created in MySQL to access and manage the database. When we create a user, the MySQL server provides the following functions for the newly created user:
- Authentication
- SSL/TLS
- resource constraints
- Roles, Password Management Properties
Second, the basic use of MySQL
2.1 Login and logout of MySQL database
After installing MySQL locally in 1.1, the system will automatically configure the login password, and you can log in to MySQL locally without entering a password.
First log in to the MySQL database, the command is as follows:
sudo mysql
The command to exit the MySQL database is exit
.
2.2 Basic operation of database
The basic operations of the database on the MySQL server mainly include:
(1) display of the database
(2) creation/deletion of the database
(3) selection of the database
- 1. The display of the database
After logging in to the MySQL database, you can enter the following command to view the installed database in MySQL:
show databases; # 查看MySQL中已经创建好的数据库
The output is shown in the figure below, there are already four databases in MySQL.
The command to view the currently running database is:
select database(); # 查看当前运行的数据库
When we log in to MySQL, no database is running. At this time, the execution result of the code is shown in the figure below, showing that the currently running database is NULL
:
- 2. Database creation/deletion
However, if we want to create a custom database for a specific task securities_master
, we can create it with the following command:
create database securities_master; # 创建自定义名称为securities_master的数据库
At this point, as shown in the figure below, show databases;
you can use the command to find that securities_master
the database has been created in MySQL:
We can also delete securities_master
the database with the following command:
select user from mysql.user; # 删除securities_master的数据库
- 3. Database selection
The command to switch between databases is , for example, the following command to switch to the database use 数据库名
created above is:securities_master
use securities_master # 切换到securities_master数据库
The result of code execution is shown in the figure below:
At this point, you have switched to securities_master
the database. select database();
You can use the command to view the currently running database as securities_master
:
2.3 Basic user operations
The basic operations of users on the MySQL server mainly include:
(1) display of users
(2) creation/deletion of users
- 1. User display
The command to display the currently logged in user is as follows:
select user(); # 显示当前用户
The command to view a list of all users in the database server is as follows:
select user from mysql.user; # 显示数据库服务器中所有用户列表
The code execution result is shown in the left figure below. When MySQL is installed in the Ubuntu system, the system creates four users by default:
-
2. User creation/authorization/deletion
-
User creation:
The command to create a user is as follows:
# 创建一个名称为sec_user的新用户
create user 'sec_user'@'localhost' identified by 'password';
Among them, sec_user
is the newly created user name; localhost
is the host name; at this time, use to select user from mysql.user;
view all users in the system, as shown in the left picture above, there are sec_user
users now.
- User authorization:
Creating a user creates a new user with full access. So, if you want to grant permissions to users, you need to use grant
grant statements.
As shown in the following table, the MySQL server provides several types of privileges for new users:
permission name | describe |
---|---|
ALL PRIVILEGES |
Allow all permissions for the new user |
CREATE |
Allows users to be able to create databases and tables |
DROP |
Allow users to be able to drop databases and tables |
DELETE |
Allow users to be able to delete rows from a specific table |
INSERT |
Allows users to insert rows into specific tables |
SELECT |
Allow the user to be able to read the database |
UPDATE |
Allow users to be able to update table rows |
The following code grants all permissions to the user created above sec_user
:
# 将所有权限赋予上面创建的`sec_user`用户
grant all privileges on securities_master.* to 'sec_user'@'localhost';
Refresh the permission settings to take effect immediately:
flush privileges;
Use the following command to view sec_user
the user's permission status:
show grants for sec_user@localhost;
The execution result is shown in the figure below:
- User's delete operation:
The command to delete the one created above sec_user
looks like this:
drop user 'sec_user'@'localhost' # 删除sec_user用户的命令