vault backup: 2025-03-04 11:17:00
This commit is contained in:
134
20.01. Programming/DB/MySQL.md
Normal file
134
20.01. Programming/DB/MySQL.md
Normal file
@@ -0,0 +1,134 @@
|
||||
## 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/)
|
||||
13
20.01. Programming/DB/sqlite.md
Normal file
13
20.01. Programming/DB/sqlite.md
Normal file
@@ -0,0 +1,13 @@
|
||||
## Build static library
|
||||
1. Navigate to [https://www.sqlite.org/download.html](https://www.sqlite.org/download.html) and download latest `amalgamation` source version of SQLite.
|
||||
2. Extract all the files into your project directory, or your include path, or separate path that you will add/added as include path in your project properties.
|
||||
3. Run `Developer Command Prompt for VS ****` which is usually available at `Start -> Programs -> Visual Studio **** -> Visual Studio Tools`.
|
||||
4. Navigate with command prompt to that directory where we extracted our SQLite.
|
||||
5. Run next command to compile: `cl /c /EHsc sqlite3.c`
|
||||
6. Run next command to create static library: `lib sqlite3.obj`
|
||||
7. Open properties of your project and add `sqlite3.lib` to `Linker -> Input -> Additional Dependencies`.
|
||||
|
||||
Now you ready to include and use `sqlite3.h` in your project.
|
||||
|
||||
### 參考
|
||||
- [How to add SQLite into your VS project as Static Library](https://gist.github.com/payalord/c87cbd1d12ea7712449657d1c6583e12)
|
||||
Reference in New Issue
Block a user