Files
Obsidian-Main/04. Programming/DB/MySQL.md
Awin Huang 5e4265b783 vault backup: 2022-09-30 21:53:03
Affected files:
.obsidian/workspace
03. 專注Study/Android/ADB 取得 APK 的 icon.md
03. 專注Study/Android/ADB.md
03. 專注Study/Android/AOSP.md
03. 專注Study/Android/Android programming.md
03. 專注Study/Android/AudioTrack.md
03. 專注Study/Android/Ktor.md
03. 專注Study/Android/Service.md
03. 專注Study/Android/Tools.md
03. 專注Study/Android/UI.md
03. 專注Study/C++/C++17.md
03. 專注Study/C++/Class template.md
03. 專注Study/C++/GCC.md
03. 專注Study/C++/Structured binding declaration.md
03. 專注Study/C++/for_each.md
03. 專注Study/C++/lambda.md
03. 專注Study/C++/lvalue.md
03. 專注Study/C++/move operator.md
03. 專注Study/C++/rvalue.md
03. 專注Study/C++/智慧指標.md
03. 專注Study/RxKotlin/20200207 - Study RxKotlin.md
04. Programming/COM/20210726 - COM Interface.md
04. Programming/DB/MySQL.md
04. Programming/DB/sqlite.md
04. Programming/Design Pattern.md
04. Programming/FFMPEG/00. Introduction.md
04. Programming/FFMPEG/01. Setup.md
04. Programming/FFMPEG/FFMpeg.md
04. Programming/Flask.md
04. Programming/Kotlin/class.md
04. Programming/Kotlin/run, let, with, also 和 apply.md
04. Programming/Media Foundation/20210604 - Windows media foundation.md
04. Programming/OpenCV.md
04. Programming/OpenGL.md
04. Programming/Python/argparse.ArgumentParser.md
04. Programming/Python/decorator.md
04. Programming/Python/logging.md
04. Programming/Python/opencv.md
04. Programming/Python/subprocess.md
04. Programming/Python/threading.md
04. Programming/Python/tkinter.md
04. Programming/Python/檢測工具.md
04. Programming/QT/Dropdown button.md
04. Programming/QT/QVariant.md
04. Programming/QT/Qt.md
04. Programming/UML.md
04. Programming/演算法.md
05. 資料收集/99. templates/blogHeader.md
05. 資料收集/99. templates/date.md
05. 資料收集/99. templates/front matter.md
05. 資料收集/99. templates/note.md
05. 資料收集/99. templates/table.md
05. 資料收集/99. templates/thisWeek.md
05. 資料收集/99. templates/日記.md
05. 資料收集/99. templates/讀書筆記.md
05. 資料收集/Linux/CLI/cut.md
05. 資料收集/Linux/CLI/scp.md
05. 資料收集/Linux/CLI/timedatectl.md
05. 資料收集/Linux/Programming.md
05. 資料收集/Linux/Ubuntu.md
05. 資料收集/Tool Setup/Hardware/RaspberryPi.md
05. 資料收集/Tool Setup/Software/Chrome.md
05. 資料收集/Tool Setup/Software/Obisidian.md
05. 資料收集/Tool Setup/Software/SublimeText.md
05. 資料收集/Tool Setup/Software/VirtualBox.md
05. 資料收集/Tool Setup/Software/Visual Studio Code.md
05. 資料收集/Tool Setup/Software/Windows Setup.md
05. 資料收集/Tool Setup/Software/Windows Terminal.md
05. 資料收集/Tool Setup/Software/freefilesync.md
05. 資料收集/Tool Setup/Software/vim.md
05. 資料收集/名言佳句.md
05. 資料收集/架站/Gitea.md
05. 資料收集/架站/HTTP Server/Apache.md
05. 資料收集/架站/HTTP Server/Nginx/Reverse Proxy(Layer4).md
05. 資料收集/架站/Pelican blog.md
05. 資料收集/架站/Proxmox VE.md
05. 資料收集/架站/SWAG Reverse proxy.md
05. 資料收集/架站/Storj.md
05. 資料收集/架站/Trojan.md
05. 資料收集/每週外食.md
05. 資料收集/科技/802.11.md
05. 資料收集/科技/HDR Sensor.md
05. 資料收集/科技/量子電腦.md
05. 資料收集/科技/鋰電池.md
05. 資料收集/興趣嗜好/RC/Traxxas Sledge.md
05. 資料收集/興趣嗜好/RC/好盈電變調整中立點.md
05. 資料收集/興趣嗜好/RC/差速器調教教學.md
05. 資料收集/興趣嗜好/模型/舊化作例.md
05. 資料收集/興趣嗜好/軍武/虎式.md
05. 資料收集/讀書筆記/20201201 - 學習如何學習.md
05. 資料收集/讀書筆記/20201218 - Kotlin權威2.0.md
05. 資料收集/讀書筆記/20201224 - 寫作是最好的自我投資.md
05. 資料收集/讀書筆記/20210119 - 中產悲歌.md
05. 資料收集/讀書筆記/20210220 - 最高學習法.md
05. 資料收集/讀書筆記/20210320 - 最高學以致用法.md
05. 資料收集/讀書筆記/20210406 - 精準購買.md
05. 資料收集/讀書筆記/20210723 - 高手學習.md
05. 資料收集/讀書筆記/20220526 - 深入淺出設計模式.md
05. 資料收集/讀書筆記/20220619 - 精確的力量.md
05. 資料收集/軟體工具/IPFS.md
05. 資料收集/軟體工具/MkDocs.md
05. 資料收集/軟體工具/Obsidian.md
05. 資料收集/軟體工具/docker.md
05. 資料收集/軟體工具/git/apply.md
05. 資料收集/軟體工具/git/submodule.md
05. 資料收集/軟體工具/youtube-dl.md
05. 資料收集/面試準備/技术面试最后反问面试官的话.md
2022-09-30 21:53:04 +08:00

134 lines
3.9 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## Install MySQL
- 下載MySQL https://downloads.mysql.com/archives/installer/
- 安裝
- 安裝之後執行MSQL Installer - Community
- ![[Pasted image 20210423152623.png]]
- 選擇MySQL Server的Reconfigure
- ![[Pasted image 20210423152747.png]]
- 依序設定port、帳號密碼。
## Install MariaDB
1. `sudo apt install mariadb-server`
2. Configuring MariaDB: `sudo mysql_secure_installation`
這步驟會問幾個問題,大概是:
1. 輸入目前root的密碼
2. 是否要切換為unix_socket -> No
3. 要變更root的密碼嗎-> No
3. Testing MariaDB: `sudo systemctl status mariadb`
4. Show version: `sudo mysqladmin version`
## Syntax
### Basic
login as root: `mysql -u root -p`
### User management
#### Create user
```sql
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'test123test!';
```
#### Remove user
```sql
DROP USER 'testuser'@'localhost';
```
#### Grant user's permission
Below commands grant user's permission to *CREATE* and *DROP*.
```sql
GRANT CREATE ON *.* TO 'testuser'@'localhost';
GRANT DROP ON tutorial_database.* TO 'testuser'@'localhost';
FLUSH PRIVILEGES;
```
#### Remove user's permission
```sql
REVOKE <permission> ON database.table FROM 'user'@'localhost';
```
上面命令中的`<permission>`必須替換成下面的其中一個:
- ALL - Allow complete access to a specific database. If a database is not specified, then allow complete access to the entirety of MySQL.
- CREATE - Allow a user to create databases and tables.
- DELETE - Allow a user to delete rows from a table.
- DROP - Allow a user to drop databases and tables.
- EXECUTE - Allow a user to execute stored routines.
- GRANT OPTION - Allow a user to grant or remove another user's privileges.
- INSERT - Allow a user to insert rows from a table.
- SELECT - Allow a user to select data from a database.
- SHOW DATABASES- Allow a user to view a list of all databases.
- UPDATE - Allow a user to update rows in a table.
例如:`REVOKE CREATE ON *.* FROM 'testuser'@'localhost';`
最後,不要忘記`FLUSH PRIVILEGES;`
#### Show user's permission
```sql
SHOW GRANTS FOR 'testuser'@'localhost';
```
#### Change user's password
```sql
ALTER USER 'user-name'@'localhost' IDENTIFIED BY 'NEW_USER_PASSWORD';
```
#### View all users
```sql
SELECT User,Host FROM mysql.user;
```
### Database
#### Show all databases
```sql
SHOW DATABASES;
```
### Create table
The rule:
```sql
CREATE TABLE table_name (column_name column_type);
```
Example:
```sql
CREATE TABLE tutorials_tbl(
tutorial_id INT NOT NULL AUTO_INCREMENT,
tutorial_title VARCHAR(100) NOT NULL,
tutorial_author VARCHAR(40) NOT NULL,
submission_date DATE,
PRIMARY KEY ( tutorial_id )
);
```
```sql
use autostation;
CREATE TABLE station_state(
id CHAR(255) NOT NULL,
ip CHAR(255) NOT NULL,
dhcp_ip CHAR(255) NOT NULL,
name CHAR(255) NOT NULL,
setupfilemd5 CHAR(255) NOT NULL,
setupfileversion CHAR(255) NOT NULL,
status CHAR(255) NOT NULL,
update_time DATETIME NOT NULL,
PRIMARY KEY ( id )
);
```
### Insert
The rule:
```
INSERT INTO <table_name> ( field1, field2,...fieldN )
VALUES
( value1, value2,...valueN );
```
Example:
```
INSERT INTO client_state ( field1, field2,...fieldN )
VALUES
( value1, value2,...valueN );
```
## 參考
- [Create a MySQL User on Linux via Command Line | Liquid Web](https://www.liquidweb.com/kb/create-a-mysql-user-on-linux-via-command-line/)
- [Grant Permissions to a MySQL User on Linux via Command Line | Liquid Web](https://www.liquidweb.com/kb/grant-permissions-to-a-mysql-user-on-linux-via-command-line/)
- [Remove Permissions for a MySQL User on Linux via Command Line - Liquid Web](https://www.liquidweb.com/kb/remove-permissions-for-a-mysql-user-on-linux-via-command-line/)
- [Remove a MySQL User on Linux via Command Line - Liquid Web](https://www.liquidweb.com/kb/remove-a-mysql-user-on-linux-via-command-line/)