1. 程式人生 > >數據庫MySQL/mariadb知識點——操作篇(0)開始

數據庫MySQL/mariadb知識點——操作篇(0)開始

l命令 提示 change ever 知識點 空格 訪問 ets pid

技術分享圖片

啟動數據庫

啟動mysql或停止mysql的常用語句如下:

1、啟動服務:

  centos6:

service mysqld start

  centos7:

systemctl start mysqld

2、重啟服務:

  cento6:

service mysqld restart

  centos7:

systemctl restart mysqld

3、關閉服務:

  cento6:

service mysqld stop

  centos7:

systemctl stop mysqld

相關配置文件

主配置文件:從上到下檢查,如果有重復的配置參數則在後邊的配置文件中的參數生效,覆蓋檢索

技術分享圖片
1 /etc/my.cnf
2 /etc/mysql/my.cnf
3 /etc/sysconfig/my.cnf
4 /usr/local/etc/my.cnf
5 ~/.my.cnf
my.cnf

查看默認配置:

/usr/libexec/mysqld --print-defaults

默認數據庫庫文件存放目錄:

/var/lib/mysql/

套接字文件

/var/lib/mysql/mysql.sock

PID文件

/var/run/mariadb/mariadb.pid

日誌文件

/var/log/mariadb/mariadb.log

  

數據庫管理系統(DBMS)

MySQL 數據庫是一種C\S結構的軟件,即分為:客戶端和服務端。

若想訪問服務器,則必須通過客戶端;服務器應該一直運行,客戶端則在需要使用的時候運行。

連接數據庫

1、本地連接

mysql -u用戶名 -p密碼
例如:
mysql -uroot -p123456

其中 -u 與用戶名root之間可以有空格;-p 與密碼之間不能存在空格。

2、遠程連接

mysql -u用戶名 -p密碼 -h IP地址 -P 端口號
例如
mysql -u root -p123456 -h 192.168.1.103 -P 3306

在連接時我們還可以指定具體登錄的數據庫,例如指定連接到testdb數據庫

mysql -uroot -p123456 -D testdb

在本地連接到mysql時,可以指定通過特定套接文件連接到數據庫

mysql -uroot -p123456 -S /var/lib/mysql/mysql.sock

連接數據庫的同時執行對應命令,並返回命令結果,並不進入mysql提示符;即非交互式模式

mysql -uroot -p123123 -e ‘use mysql; select user,host,password from user;‘
mysql -uroot -p123123 -e ‘create database if not exists testdb; show databases;‘

數據庫的常用選項

技術分享圖片
 1 -A, --no-auto-rehash 禁止補全
 2 -u, --user= 用戶名,默認為root
 3 -h, --host= 服務器主機,默認為localhost
 4 -p, --passowrd= 用戶密碼,建議使用-p,默認為空密碼
 5 -P, --port= 服務器端口
 6 -S, --socket= 指定連接socket文件路徑
 7 -D, --database= 指定默認數據庫
 8 -C, --compress 啟用壓縮
 9 -e “SQL“ 執行SQL命令
10 -V, --version 顯示版本
11 -v --verbose 顯示詳細信息
12 --print-defaults 獲取程序默認使用的配置
選項

註:

1、prompt \u@[\D] \r:\m:\s-> 修改提示符,如果需要永久修改則在my.cnf中 [mysql] 下加入

prompt="(\u@\h) [\d]>

2、批量處理,將sql腳本導入庫執行

mysql < /path/somefile.sql

獲取幫助

使用help獲取幫助

技術分享圖片
 1 MariaDB [(none)]> help
 2 
 3 General information about MariaDB can be found at
 4 http://mariadb.org
 5 
 6 List of all MySQL commands:
 7 Note that all text commands must be first on line and end with ;
 8 ?         (\?) Synonym for `help.
 9 clear     (\c) Clear the current input statement.
10 connect   (\r) Reconnect to the server. Optional arguments are db and host.
11 delimiter (\d) Set statement delimiter.
12 edit      (\e) Edit command with $EDITOR.
13 ego       (\G) Send command to mysql server, display result vertically.
14 exit      (\q) Exit mysql. Same as quit.
15 go        (\g) Send command to mysql server.
16 help      (\h) Display this help.
17 nopager   (\n) Disable pager, print to stdout.
18 notee     (\t) Dont write into outfile.
19 pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
20 print     (\p) Print current command.
21 prompt    (\R) Change your mysql prompt.
22 quit      (\q) Quit mysql.
23 rehash    (\#) Rebuild completion hash.
24 source    (\.) Execute an SQL script file. Takes a file name as an argument.
25 status    (\s) Get status information from the server.
26 system    (\!) Execute a system shell command.
27 tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
28 use       (\u) Use another database. Takes database name as argument.
29 charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
30 warnings  (\W) Show warnings after every statement.
31 nowarning (\w) Dont show warnings after every statement.
32 
33 For server side help, type help contents
help

數據庫MySQL/mariadb知識點——操作篇(0)開始