本文簡要介紹mariadb數(shù)據(jù)庫的基本操作碑幅,安裝教程請參照【CentOS實用篇】之二進制安裝mariadb http://www.reibang.com/p/fb188a37ae76
在Mariadb初始化淮菠,并設(shè)置好密碼連接之后闭树,mysql命令不能直接登入數(shù)據(jù)庫蚌斩,需要指定用戶和密碼霸奕,在添加了外部主機的情況下秀姐,使用外部主機聯(lián)機數(shù)據(jù)庫养泡,需要指定數(shù)據(jù)庫主機的ip
-uUSERNAME: 用戶名;默認為root
-hHOST: 服務(wù)器主機; 默認為localhost
-pPASSWORD:用戶的密碼;建議使用-p,默認為空密碼
也可以在-p后面不跟密碼迄损,程序會自動提示輸入密碼定躏,以靜默的方式輸入密碼,避免密碼的泄露
[root@c7 ~]#mysql -uroot -pmagedu
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 27
Server version: 10.2.8-MariaDB-log MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
數(shù)據(jù)庫的基礎(chǔ)幫助
數(shù)據(jù)庫的幫助使用help查看芹敌,也可以使用 \h; 查詢痊远,注意前面的斜杠和后面的分號不能落下
MariaDB [(none)]> help
General information about MariaDB can be found at
http://mariadb.org
List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
? (\?) Synonym for `help'.
clear (\c) Clear the current input statement.
connect (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit (\e) Edit command with $EDITOR.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to mysql server.
help (\h) Display this help.
nopager (\n) Disable pager, print to stdout.
notee (\t) Don't write into outfile.
pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print (\p) Print current command.
prompt (\R) Change your mysql prompt.
quit (\q) Quit mysql.
rehash (\#) Rebuild completion hash.
source (\.) Execute an SQL script file. Takes a file name as an argument.
status (\s) Get status information from the server.
system (\!) Execute a system shell command.
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument.
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
基礎(chǔ)命令查詢
查看數(shù)據(jù)庫版本
MariaDB [(none)]> select version();
+--------------------+
| version() |
+--------------------+
| 10.2.8-MariaDB-log |
+--------------------+
1 row in set (0.00 sec)
查看當(dāng)前用戶
MariaDB [(none)]> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
SQL語句構(gòu)成
Keyword組成clause
多條clause組成語句
SELECT * ------------------ SELECT子句
FROM products ----------- FROM子句
WHERE price>400 ------ WHERE子句
SQL語句:
DDL: Data DefinationLanguage ----------------- # 數(shù)據(jù)的定義語言
CREATE(創(chuàng)建), DROP(刪除), ALTER(修改)
DML: Data Manipulation Language ------------ # 數(shù)據(jù)的操作語言
INSERT(添加), DELETE(刪除), UPDATE(更新)
DCL:Data Control Language ------------------ # 數(shù)據(jù)的控制語言
GRANT(授權(quán)), REVOKE(取消權(quán)限)
DQL:Data Query Language ------------------- # 數(shù)據(jù)的查詢語言
SELECT(查詢)
SQL命令大小寫不敏感,建議大寫氏捞。字符串敞亮區(qū)分大小寫碧聪。SQL語句可以單行寫或者多行寫,以分號液茎;結(jié)尾逞姿,關(guān)鍵詞不能跨行,也不能簡寫捆等,必須寫在一行滞造。建議用縮進提高可讀性。
注釋:
/注釋內(nèi)容/ -------------------- # 多行注釋
--注釋內(nèi)容--------------------- # 單行注釋栋烤,注意有空格
MySQL注釋:#
數(shù)據(jù)庫對象的命名規(guī)則
必須以字母開頭
可包括數(shù)字和三個特殊字符(# _ $)
不要使用MySQL的保留字
同一Schema下的對象不能同名
創(chuàng)建數(shù)據(jù)庫
使用create命令創(chuàng)建magedb數(shù)據(jù)庫谒养,查詢數(shù)據(jù)庫文件夾內(nèi)生成的magedb目錄
[root@c7 dbdata]#mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 30
Server version: 10.2.8-MariaDB-log MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database magedb;
Query OK, 1 row affected (0.00 sec)
[root@c7 ~]#ll /app/dbdata/
total 122980
-rw-rw---- 1 mysql mysql 16384 Sep 25 17:21 aria_log.00000001
-rw-rw---- 1 mysql mysql 52 Sep 25 17:21 aria_log_control
-rw-rw---- 1 mysql mysql 5 Sep 25 17:22 c7.pid
-rw-rw---- 1 mysql mysql 2799 Sep 25 17:21 ib_buffer_pool
-rw-rw---- 1 mysql mysql 12582912 Sep 25 17:22 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Sep 25 17:22 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Sep 25 17:15 ib_logfile1
-rw-rw---- 1 mysql mysql 12582912 Sep 25 17:22 ibtmp1
drwx------ 2 mysql mysql 20 Sep 25 22:17 magedb
使用drop命令刪除magedb數(shù)據(jù)庫明郭,查看magedu文件已刪除
MariaDB [(none)]> drop database magedb;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]>
[root@c7 ~]#ls /app/dbdata/
aria_log.00000001 ibdata1 multi-master.info mysql-bin.000003
aria_log_control ib_logfile0 mysql mysql-bin.index
c7.pid ib_logfile1 mysql-bin.000001 performance_schema
ib_buffer_pool ibtmp1 mysql-bin.000002 test