【nginx配置】 proxy_pass反向代理配置中url後面加不加/的說明
1. 關係型資料庫介紹
資料結構模型
資料結構模型主要有:
-
層次關係
-
網狀結構
-
關係模型
關係模型
二維關係 :
row row:表中的每一行,又稱為一條記錄(record)
column column:表中的每一列,稱為屬性,欄位(field)
資料庫管理系統:DBMS
關係:Relational RDBMS
RDBMS專業名詞
常見的關係型資料庫管理系統:
-
MySQL:MySQL,MariaDB,Percona-Server
-
PostgreSQL:簡稱為pgsql
-
Oracle
-
MSSQL
SQL:Structure Query Language,結構化查詢語言
約束:constraint,向資料表提供的資料要遵守的限制
-
主鍵約束:一個或多個欄位的組合,填入的資料必須能在本表中唯一標識本行。且必須提供資料,不能為空(NOT NULL)。 一個表只能存在一個
-
惟一鍵約束:一個或多個欄位的組合,填入的資料必須能在本表中唯一標識本行。允許為空(NULL) 一個表可以存在多個
-
外來鍵約束:一個表中的某欄位可填入資料取決於另一個表的主鍵已有的資料
-
檢查性約束:欄位值在一定範圍內(例:年齡1-150歲之間)
索引:將表中的一個或多個欄位中的資料複製一份另存,並且這些資料需要按特定次序排序儲存
2.mysql的程式組成
客戶端
-
mysql:CLI互動式客戶端程式
-
mysql_secure_installation:安全初始化,強烈建議安裝完以後執行此命令
-
mysqldump:mysql備份工具
-
mysqladmin
伺服器端
mysqld
3.SQL語句有3種類型:
DDL(Data Defination Language) : 資料定義語言
DML(Data Manipulation Language) : 資料操作語言
DCL(Data Control Language) :資料控制語言
SQL語句型別 | 對應操作 |
DDL |
CREATE:建立 DROP:刪除 ALTER:修改 |
DML |
INSERT:向表中插入資料 DELETE:刪除表中的資料 UPDATE:更新表中的資訊 SELECT:查詢表中的資訊 |
DCL |
GRANT:授權 RECVOKE:移除授權 |
mysql工具使用
語法:mysql [OPTIONS] [database] 常用的OPTIONS: -uUSERNAME 指定使用者名稱,預設為root -hHOST 指定伺服器主機,預設為localhost,推薦使用ip地址 -pPASSWORD 指定使用者的密碼 -P 指定資料庫監聽的埠,這裡的#需用實際的埠號代替,如-P3307 -V 檢視當前使用的mysql版本 -e 不登入mysql執行sql語句後退出,常用於指令碼
4.安裝Mariadb
MariaDB資料庫管理系統是MySQL的一個分支完全相容MySQL
[root@localhost ~]# yum -y install mariadb* .................................... tzdata-java-2019c-1.el8.noarch unixODBC-2.3.7-1.el8.x86_64 Complete
設定為開機自啟動並且現在啟動Mariadb檢視埠號
[root@localhost ~]# systemctl enable --now mariadb Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service. Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service. Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service. [root@localhost ~]# ss -tanlp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 80 0.0.0.0:3306 0.0.0.0:* users:(("mysqld",pid=4827,fd=21)) LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=979,fd=5)) LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=979,fd=7))
使用命令 mysql_secure_installation安全初始化
root@localhost ~]# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and ..............................................
修改密碼
MariaDB [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' ;
5.使用者操作
mysql使用者帳號由兩部分組成,如'USERNAME'@'HOST',表示此USERNAME只能從此HOST上遠端登入
HOST其值可為:
IP地址如:192.168.122.1
萬用字元:%(允許從任何地方登入主機)
_ (匹配任意單個字元)
資料庫使用者的建立
資料庫使用者建立語法:CREATE USER 'username'@'host' [IDENTIFIED BY 'password']; MariaDB [(none)]> CREATE USER 'yonghu'@'192.168.248.130' IDENTIFIED BY '123!'; Query OK, 0 rows affected (0.001 sec) 使用新建立的使用者和密碼登入 [root@localhost ~]# mysql -uyonghu -p123! -h 192.168.248.130 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 10 Server version: 10.3.17-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 刪除資料庫使用者 刪除資料庫使用者語法: DROP USER 'username'@'host'; MariaDB [(none)]> DROP USER 'yonghu'@'192.168.248.130'; Query OK, 0 rows affected (0.001 se
DDL操作
資料庫操作
建立資料庫語法:CREATE DATABASE [IF NOT EXISTS] 'DB_NAME'; MariaDB [(none)]> CREATE DATABASE IF NOT EXISTS test ; Query OK, 1 row affected (0.001 sec) MariaDB [(none)]> show databases ; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 刪除資料庫語法:DROP DATABASE [IF EXISTS] 'DB_NAME'; MariaDB [mysql]> DROP DATABASE IF EXISTS test ; Query OK, 0 rows affected (0.001 sec) MariaDB [mysql]> SHOW DATABASES ; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.000 sec)
表操作
建立表語法:CREATE TABLE table_name (col1 datatype 修飾符,col2 datatype 修飾符) ENGINE='儲存引擎型別'; mariaDB [test]> CREATE TABLE biao(id int auto_increment not null,name varchar(50),age tinyint,primary key(id)); Query OK, 0 rows affected (0.003 sec) MariaDB [test]> DESC biao; 查看錶結構 +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(50) | YES | | NULL | | | age | tinyint(4) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 3 rows in set (0.001 sec) 修改表結構語法:ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name; MariaDB [test]> ALTER TABLE biao ADD class varchar(10); Query OK, 0 rows affected (0.002 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [test]> DESC biao; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(50) | YES | | NULL | | | age | tinyint(4) | YES | | NULL | | | class | varchar(10) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.001 sec) 刪除表語法:DROP TABLE [ IF EXISTS ] 'table_name'; MariaDB [test]> DROP TABLE IF EXISTS biao -> ; Query OK, 0 rows affected (0.004 sec) MariaDB [test]> SHOW TABLES ; Empty set (0.001 sec)
DML操作
DML操作包括增(INSERT)、刪(DELETE)、改(UPDATE)、查(SELECT),均屬針對表的操作。
INSERT語句
語法:INSERT [INTO] table_name [(column_name,...)] {VALUES | VALUE} (value1,...),(...),...; MariaDB [test]> INSERT INTO biao (id,name,age,class) VALUE (1,'huahua',20,'freshman'); Query OK, 1 row affected (0.001 sec) MariaDB [test]> INSERT INTO biao(name,age,class) VALUE('chaochao',20,'freshman'),('lisi',19,'freshman'),('meijianbiao',50,'freshman'); Query OK, 3 rows affected (0.002 sec) Records: 3 Duplicates: 0 Warnings: 0 MariaDB [test]> SELECT * FROM biao; +----+-------------+------+----------+ | id | name | age | class | +----+-------------+------+----------+ | 1 | huahua | 20 | freshman | | 2 | chaochao | 20 | freshman | | 3 | lisi | 19 | freshman | | 4 | meijianbiao | 50 | freshman | +----+-------------+------+----------+ 4 rows in set (0.001 sec)
SELECT語句
欄位column表示:
表示符 | 代表什麼 |
* | 所有欄位 |
as |
欄位別名,如col1 AS alias1 |
條件判斷語句WHERE:
操作型別 | 常用操作符 |
操作符 | >,<,>=,<=,=,!= BETWEEN column# AND column# LIKE:模糊匹配 RLIKE:基於正則表示式進行模式匹配 IS NOT NULL:非空 IS NULL:空 |
條件邏輯操作 | AND OR NOT |
ORDER BY:排序,預設為升序(ASC):
ORDER BY語句 | 意思 |
ORDER BY ‘column_name' | 根據column_name進行升序排序 |
ORDER BY 'column_name' DESC | 根據column_name進行降序排序 |
ORDER BY ’column_name' LIMIT 2 | 根據column_name進行升序排序 並只取前2個結果 |
ORDER BY ‘column_name' LIMIT 1,2 | 根據column_name進行升序排序 並且略過第1個結果取後面的2個結果 |
SELECT語法:SELECT column1,column2,... FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n]; MariaDB [test]> SELECT * FROM biao; +----+-------------+------+----------+ | id | name | age | class | +----+-------------+------+----------+ | 1 | huahua | 20 | freshman | | 2 | chaochao | 20 | freshman | | 3 | lisi | 19 | freshman | | 4 | meijianbiao | 50 | freshman | +----+-------------+------+----------+ 4 rows in set (0.000 sec) MariaDB [test]> SElECT id FROM biao ORDER BY id; +----+ | id | +----+ | 1 | | 2 | | 3 | | 4 | +---- MariaDB [test]> SELECT id FROM biao ORDER BY id DESC ; +----+ | id | +----+ | 4 | | 3 | | 2 | | 1 | +----+ 4 rows in set (0.000 sec) MariaDB [test]> SELECT name FROM biao WHERE name like 'm%'; +-------------+ | name | +-------------+ | meijianbiao | +-------------+ 1 row in set (0.000 sec) MariaDB [test]> SELECT age FROM biao WHERE age >=20 ; +------+ | age | +------+ | 20 | | 20 | | 50 | +------+ 3 rows in set (0.000 sec) MariaDB [test]> SELECT * FROM biao WHERE age between 20 and 50 ; +----+-------------+------+----------+ | id | name | age | class | +----+-------------+------+----------+ | 1 | huahua | 20 | freshman | | 2 | chaochao | 20 | freshman | | 4 | meijianbiao | 50 | freshman | +----+-------------+------+----------+ MariaDB [test]> SELECT * FROM biao WHERE age >=20 or age <=50 ; +----+-------------+------+----------+ | id | name | age | class | +----+-------------+------+----------+ | 1 | huahua | 20 | freshman | | 2 | chaochao | 20 | freshman | | 3 | lisi | 19 | freshman | | 4 | meijianbiao | 50 | freshman | +----+-------------+------+----------+ 4 rows in set (0.001 sec) MariaDB [test]> SELECT * FROM biao WHERE age is not null ; +----+-------------+------+----------+ | id | name | age | class | +----+-------------+------+----------+ | 1 | huahua | 20 | freshman | | 2 | chaochao | 20 | freshman | | 3 | lisi | 19 | freshman | | 4 | meijianbiao | 50 | freshman | +----+-------------+------+----------+ 4 rows in set (0.000 sec) MariaDB [test]> SELECT * FROM biao WHERE age is null ; Empty set (0.000 sec)
update語句
語法:UPDATE table_name SET column1 = new_value1[,column2 = new_value2,...] [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n]; MariaDB [test]> UPDATE biao set age = 25 WHERE name ='meijianbiao' ; Query OK, 1 row affected (0.001 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [test]> SELECT * FROM biao; +----+-------------+------+----------+ | id | name | age | class | +----+-------------+------+----------+ | 1 | huahua | 20 | freshman | | 2 | chaochao | 20 | freshman | | 3 | lisi | 19 | freshman | | 4 | meijianbiao | 25 | freshman | +----+-------------+------+----------+ 4 rows in set (0.000 sec) MariaDB [test]> UPDATE biao set name = 'haha',age = 30 WHERE id = 4 ; Query OK, 1 row affected (0.001 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [test]> SELECT * FROM biao; +----+----------+------+----------+ | id | name | age | class | +----+----------+------+----------+ | 1 | huahua | 20 | freshman | | 2 | chaochao | 20 | freshman | | 3 | lisi | 19 | freshman | | 4 | haha | 30 | freshman | +----+----------+------+----------+ 4 rows in set (0.000 sec)
delete語句
語法:DELETE FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n]; MariaDB [test]> SELECT * FROM biao; +----+----------+------+----------+ | id | name | age | class | +----+----------+------+----------+ | 1 | huahua | 20 | freshman | | 2 | chaochao | 20 | freshman | | 3 | lisi | 19 | freshman | | 4 | haha | 30 | freshman | +----+----------+------+----------+ 4 rows in set (0.000 sec) MariaDB [test]> DELETE FROM biao WHERE id = 4; 刪除記錄 Query OK, 1 row affected (0.001 sec) MariaDB [test]> SELECT * FROM biao; +----+----------+------+----------+ | id | name | age | class | +----+----------+------+----------+ | 1 | huahua | 20 | freshman | | 2 | chaochao | 20 | freshman | | 3 | lisi | 19 | freshman | +----+----------+------+----------+ 3 rows in set (0.000 sec) MariaDB [test]> DELETE FROM biao; 刪除表中所有資料 Query OK, 3 rows affected (0.001 sec) MariaDB [test]> SELECT * FROM biao; Empty set (0.000 sec)
truncate語句
delete與truncate的區別
delete:DELETE刪除表內容時僅刪除內容,但會保留表結構,DELETE語句每次刪除一行,並在事務日誌中為所刪除的每行記錄一項,可以通過回滾事務日誌恢復資料,非常佔用空間。
truncate:刪除表中所有資料,且無法恢復表結構、約束和索引等保持不變,新新增的行計數值重置為初始值,執行速度比DELETE快,且使用的系統和事務日誌資源少,通過釋放儲存表資料所用的資料頁來刪除資料,並且只在事務日誌中記錄頁的釋放,對於有外來鍵約束引用的表,不能使用TRUNCATE TABLE刪除資料,不能用於加入了索引檢視的表。
語法:TRUNCATE table_name; MariaDB [test]> select * from biao; +----+------+------+-------+ | id | name | age | class | +----+------+------+-------+ | 1 | haha | 1 | NULL | | 2 | ww | 3 | NULL | | 3 | xx | 9 | NULL | +----+------+------+-------+ 3 rows in set (0.000 sec) MariaDB [test]> truncate biao; Query OK, 0 rows affected (0.004 sec)
DCL操作
建立授權grant
用法: grant 許可權 on 資料庫物件 to 使用者
MariaDB [test]> GRANT DELETE on test.biao TO 'hhh'@'%' IDENTIFIED BY '1234!'; 使用者hhh在所有位置遠端訪問test資料庫有DELETE許可權 Query OK, 0 rows affected (0.000 sec) MariaDB [test]> GRANT ALL on *.* TO 'xxx'@'%' IDENTIFIED BY '1234!'; 使用者xxx在所有位置上遠端登入訪問資料庫擁有所有許可權 Query OK, 0 rows affected (0.000 sec)
檢視使用者許可權SHOW GRANTS
MariaDB [test]> SHOW GRANTS; 產看當前登入使用者的許可權 +----------------------------------------------------------------------------------------------------------------------------------------+ | Grants for root@localhost | +----------------------------------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' WITH GRANT OPTION | | GRANT PROXY ON ''@'%' TO 'root'@'localhost' WITH GRANT OPTION | +----------------------------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.000 sec)
檢視指定使用者許可權
MariaDB [test]> SHOW GRANTS FOR xxx; +-------------------------------------------------------------------------------------------------------------+ | Grants for xxx@% | +-------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'xxx'@'%' IDENTIFIED BY PASSWORD '*9BE02DCF8FEE75750C9262B0875091F94C620EF2' | +-------------------------------------------------------------------------------------------------------------+ 1 row in set (0.000 sec) MariaDB [test]> SHOW GRANTS FOR hhh; +----------------------------------------------------------------------------------------------------+ | Grants for hhh@% | +----------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'hhh'@'%' IDENTIFIED BY PASSWORD '*9BE02DCF8FEE75750C9262B0875091F94C620EF2' | | GRANT DELETE ON `test`.`biao` TO 'hhh'@'%' | +----------------------------------------------------------------------------------------------------+ 2 rows in set (0.000 sec)