mysql學(xué)習(xí)筆記1
安裝
1.更新
sudo apt update
2.安裝
$ sudo apt install mysql-server
3.查看運(yùn)行狀況
$ sudo systemctl status mysql.service
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:>
Active: active (running) since Mon 2024-09-23 16:13:22 CST; 48s ago
Process: 3135 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=e>
Main PID: 3143 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 1917)
Memory: 366.0M
CPU: 980ms
CGroup: /system.slice/mysql.service
└─3143 /usr/sbin/mysqld
Sep 23 16:13:21 iZbp15gacvms2qkuahiq0oZ systemd[1]: Starting MySQL Community Se>
Sep 23 16:13:22 iZbp15gacvms2qkuahiq0oZ systemd[1]: Started MySQL Community Ser>
4.運(yùn)行安全腳本
$ sudo mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT 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 component?
Press y|Y for Yes, any other key for No:
輸入y,選擇啟用并設(shè)置密碼驗(yàn)證組建。
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:
```bash
有三個(gè)級(jí)別的密碼驗(yàn)證策略:
LOW:長(zhǎng)度至少為 8 個(gè)字符。
MEDIUM:長(zhǎng)度至少為 8 個(gè)字符,并且包含數(shù)字、大小寫字母和特殊字符。
STRONG:長(zhǎng)度至少為 8 個(gè)字符,并且包含數(shù)字、大小寫字母、特殊字符,并且不在字典文件中。
輸入 0 1 2分別代表低 中 高 三檔。 `1`
```bash
Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) :
詢問(wèn)是否需要移除匿名用戶。 y
Success.
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) :
是否禁止root用戶遠(yuǎn)程登陸 n
... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No)
是否刪除 test 數(shù)據(jù)庫(kù) n
... skipping.
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) :
是否重新加載權(quán)限表使設(shè)置生效?y
Success.
All done!
至此,安裝完成。
試運(yùn)行
1.登陸
$ sudo mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.39-0ubuntu0.22.04.1 (Ubuntu)
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
指令中-u代表-user,登陸用戶為root,-p代表-password,輸入密碼
2.查看現(xiàn)有數(shù)據(jù)庫(kù)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
show databases列出mysql服務(wù)器上所有數(shù)據(jù)庫(kù)。
information_schema:這是一個(gè)系統(tǒng)數(shù)據(jù)庫(kù),提供了關(guān)于所有其他數(shù)據(jù)庫(kù)的信息。
mysql:這是 MySQL 系統(tǒng)數(shù)據(jù)庫(kù),存儲(chǔ)了用戶賬戶、權(quán)限和其他系統(tǒng)級(jí)別的信息。
performance_schema:這是一個(gè)性能模式數(shù)據(jù)庫(kù),用于監(jiān)控 MySQL 服務(wù)器的性能。
sys:這是一個(gè)系統(tǒng)數(shù)據(jù)庫(kù),提供了一組視圖和過(guò)程,幫助分析 MySQL 性能。
3.創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)
mysql> create database test_database;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test_database |
+--------------------+
5 rows in set (0.00 sec)
使用create database + 數(shù)據(jù)庫(kù)名 可以新建數(shù)據(jù)庫(kù)。
4.選擇一個(gè)數(shù)據(jù)庫(kù)
mysql> use test_database
Database changed
mysql> select database();
+---------------+
| database() |
+---------------+
| test_database |
+---------------+
1 row in set (0.00 sec)
使用use進(jìn)行數(shù)據(jù)庫(kù)選擇,再使用select database();進(jìn)行查看已選擇的對(duì)象。
5.刪除數(shù)據(jù)庫(kù)
drop database test_database;
使用drop database+數(shù)據(jù)庫(kù)名,刪除數(shù)據(jù)庫(kù)。
刪除完成后,查看下
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
可見(jiàn)新建的test_database已被刪除。
6.退出
mysql> quit
Bye
或者
mysql> exit
Bye
補(bǔ)充:創(chuàng)建用戶
CREATE USER 'John_Lenon'@'%' IDENTIFIED BY 'Beatles!666';
Query OK, 0 rows affected (0.17 sec)

浙公網(wǎng)安備 33010602011771號(hào)