本文简要介绍mariadb数据库的基本操作,安装教程请参照【CentOS实用篇】之二进制安装mariadb http://www.jianshu.com/p/fb188a37ae76
在Mariadb初始化,并设置好密码连接之后,mysql命令不能直接登入数据库,需要指定用户和密码,在添加了外部主机的情况下,使用外部主机联机数据库,需要指定数据库主机的ip
-uUSERNAME: 用户名;默认为root
-hHOST: 服务器主机; 默认为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)]>
数据库的基础帮助
数据库的帮助使用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.
基础命令查询
查看数据库版本
MariaDB [(none)]> select version();
+--------------------+
| version() |
+--------------------+
| 10.2.8-MariaDB-log |
+--------------------+
1 row in set (0.00 sec)
查看当前用户
MariaDB [(none)]> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
SQL语句构成
Keyword组成clause
多条clause组成语句
SELECT * ------------------ SELECT子句
FROM products ----------- FROM子句
WHERE price>400 ------ WHERE子句
SQL语句:
DDL: Data DefinationLanguage ----------------- # 数据的定义语言
CREATE(创建), DROP(删除), ALTER(修改)
DML: Data Manipulation Language ------------ # 数据的操作语言
INSERT(添加), DELETE(删除), UPDATE(更新)
DCL:Data Control Language ------------------ # 数据的控制语言
GRANT(授权), REVOKE(取消权限)
DQL:Data Query Language ------------------- # 数据的查询语言
SELECT(查询)
SQL命令大小写不敏感,建议大写。字符串敞亮区分大小写。SQL语句可以单行写或者多行写,以分号;结尾,关键词不能跨行,也不能简写,必须写在一行。建议用缩进提高可读性。
注释:
/注释内容/ -------------------- # 多行注释
--注释内容--------------------- # 单行注释,注意有空格
MySQL注释:#
数据库对象的命名规则
必须以字母开头
可包括数字和三个特殊字符(# _ $)
不要使用MySQL的保留字
同一Schema下的对象不能同名
创建数据库
使用create命令创建magedb数据库,查询数据库文件夹内生成的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数据库,查看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