MariaDB是MySQL創始人基於MySQL的另起的一個分支,其功能上絕大部分兼容於MySQL。MariaDB的官網爲https://mariadb.com/。mysql
MySQL基本架構以下所示:sql
MySQL核心組件:shell
鏈接池做用:認證、線程、鏈接數限制,內存檢查、緩存;數據庫
SQL接口:DML(數據操做語言),DDL(數據定義語言)、關係型數據庫的基本抽象;vim
Parser(分析器):查詢轉換、對象權限檢查;windows
優化器:訪問路徑、性能相關的統計數據;centos
Caches和buffers:與存儲引擎自身相關的I/O性能提高工具;緩存
存儲引擎: MyiSAM、InnoDB、NDB、Archive、Memory、Merge、Federated、CVS、Blackholl、Aria、Sphinx、TokuDB安全
安裝方式有3種:服務器
rpm包:操做系統供應商、MySQL官網的
通用二進制格式:MySQL官網上也有
源碼包:MySQL官網提供
環境: centos 6.6
下面以mariadb-10.0.13.tar.gz源碼包方式安裝,下載源碼包後,放在/usr/local/src/目錄下,源碼包編譯安裝須要先安裝好Development tools、Server Platform Development這兩個包組。
使用#yum groupinstall -y "Server Platform Development" "Development tools"安裝
安裝好後,把源碼包解壓放到/usr/local目錄下
[root@hostpc src]# tar xf mariadb-10.0.13.tar.gz -C /usr/local/
爲安裝前準備好環境,包括以下步驟:
1.建立沒有登陸權限的MySQL系統用戶
[root@hostpc local]# useradd -r -s /sbin/nologin mysql
2.建立MySQL數據目錄(爲了數據安全性,通常都是單獨存放在一個硬盤且有高可用的RAID上),修改數據目錄的屬主和屬組爲mysql
這裏使用lvm來演示
[root@hostpc local]# lvcreate -L 10G -n data vg_lvm 建立10G的邏輯卷
[root@hostpc local]# mkfs.ext4 /dev/vg_lvm/data 格式化剛建立的邏輯卷
[root@hostpc local]# mount /dev/vg_lvm/data /mysql 掛載
[root@hostpc local]# mkdir /mysql/data 建立數據目錄
[root@hostpc local]# chown -R mysql.mysql /mysql/data 改變屬主和屬組
MariaDB 10.0.13須要經過cmake來進行編譯,須要先安裝cmake
#yum install -y cmake
查看一下使用幫助
[root@hostpc mariadb-10.0.13]# cmake -LH
.......
// Path to a library.
AIO_LIBRARY:FILEPATH=AIO_LIBRARY-NOTFOUND
// Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel
CMAKE_BUILD_TYPE:STRING=RelWithDebInfo
// install prefix
CMAKE_INSTALL_PREFIX:PATH=/usr/local/mysql
// Set to true if this is a community build
COMMUNITY_BUILD:BOOL=ON
// Compile CONNECT storage engine with LIBXML2 support
CONNECT_WITH_LIBXML2:BOOL=ON
// Compile CONNECT storage engine with remote MySQL connection support
CONNECT_WITH_MYSQL:BOOL=ON
.........
啓用ssl和使用Sphinx存儲引擎
[root@hostpc mariadb-10.0.13]# cmake . -DMYSQL_DATADIR=/mysql/data -DWITH_SSL=system -DWITH_SPHINX_STORAGE_ENGINE=1
...
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
MYSAL_DATADIR
-- Build files have been written to: /usr/local/mariadb-10.0.13
說明配置ok
出現以下錯誤提示:
-- Could NOT find LibXml2 (missing: LIBXML2_LIBRARIES LIBXML2_INCLUDE_DIR)
Warning: Bison executable not found in PATH
須要安裝libxml2-devel
[root@hostpc mariadb-10.0.13]# make 執行時間看機器性能
[root@hostpc mariadb-10.0.13]# make install
執行過程會有進度顯示的達到100%後就ok了
[root@hostpc mariadb-10.0.13]# cd ../mysql/
[root@hostpc mysql]# ls
bin CREDITS EXCEPTIONS-CLIENT lib README sql-bench
COPYING data include man scripts support-files
COPYING.LESSER docs INSTALL-BINARY mysql-test share
support-files目錄下有配置文件和啓動文件 與通用二進制安裝同樣
scripts 有數據庫初始化文件
導出頭文件
導出二進制文件
導出man文檔
[root@hostpc mysql]# ln -sv /usr/local/mysql/include /usr/include/mysql
`/usr/include/mysql' -> `/usr/local/mysql/include'
[root@hostpc mysql]# echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
[root@hostpc mysql]# . /etc/profile.d/mysql.sh
[root@hostpc mysql]# vim /etc/man.config
在MANPATH出添加
MANPATH /usr/local/mysql/man
[root@hostpc mysql]# cp support-files/my-large.cnf /etc/my.cnf
修改配置文件添加數據目錄爲/mysql/data
[root@hostpc mysql]# vim /etc/my.cnf 在mysqld段下添加datadir=/mysql/data
[root@hostpc mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld 複製開機啓動文件
[root@hostpc mysql]# ls -l /etc/rc.d/init.d/mysqld
-rwxr-xr-x 1 root root 12052 Apr 16 11:20 /etc/rc.d/init.d/mysqld
[root@hostpc mysql]# chkconfig --add mysqld
[root@hostpc mysql]# chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
初始化mysql
[root@hostpc mysql]# scripts/mysql_install_db --user=mysql --datadir=/mysql/data
啓動mysql
[root@hostpc mysql]# service mysqld start
Starting MySQL. SUCCESS!
鏈接mysql
[root@hostpc mysql]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.0.13-MariaDB-log Source distribution
Copyright (c) 2000, 2014, Oracle, SkySQL Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases; 查看有哪些數據庫
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
。。。。。。。。。
MariaDB [(none)]> show engines\G sphinx存儲引擎啓用
Savepoints: NO
*************************** 8. row ***************************
Engine: SPHINX
Support: YES
Comment: Sphinx storage engine 2.1.5-release
Transactions: NO
XA: NO
Savepoints: NO
8 rows in set (0.00 sec)
MariaDB [(none)]> show global variables like '%ssl%';
+---------------+----------+
| Variable_name | Value |
+---------------+----------+
| have_openssl | YES | ssl功能支持了,只是還未啓用
| have_ssl | DISABLED |
| ssl_ca | |
| ssl_capath | |
| ssl_cert | |
| ssl_cipher | |
| ssl_crl | |
| ssl_crlpath | |
| ssl_key | |
+---------------+----------+
9 rows in set (0.00 sec)
MySQL的數據文件包括:文件和日誌
文件:數據文件和索引文件
日誌:事務日誌、二進制日誌、查詢日誌、慢查詢日誌、錯誤日誌、中繼日誌
MySQL的服務器變量查看:
SHOW {GLOBAL|SESSION} VARIABLES [LIKE CLAUSE];
MySQL的狀態變量查看:
SHOW {GLOBAL|SESSION} STATUS [LIKE CLAUSE];
MySQL Logical Archtecture
全部鏈接都是經過線程來實現的,線程池滿了之後,鏈接須要在隊列中等待
myisam存儲引擎不支持事務(讀多寫少性能較好)
安裝和訪問MySQL Server:
初始化:
給root用戶設置密碼:
mysql>SET PASSWORD FOR 'username'@'host' = PASSWORD('your_password');
更改表數據的方式來設定密碼
mysql>update mysql.user set password=PASSWORD('your_password') where user='username' and host='hostname or ip'
命令行方式修改
#mysqladmin -uUSERNMAE -hHOSTNAME_OR_IP -p password 'new_password'
刪除匿名用戶
mysql>drop user user[,user] ...
mysql>delete from tbl_name where where_condition
MariaDB [(none)]> select User,Host,Password from mysql.user;查看當前有哪些用戶
+------+-----------+----------+
| User | Host | Password |
+------+-----------+----------+
| root | localhost | |
| root | hostpc | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | hostpc | |
+------+-----------+----------+
6 rows in set (0.00 sec)
刪除匿名用戶
MariaDB [(none)]> drop user ''@localhost;
Query OK, 0 rows affected (0.03 sec)
MariaDB [(none)]> delete from mysql.user where User='';
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> select User,Host,Password from mysql.user;
+------+-----------+----------+
| User | Host | Password |
+------+-----------+----------+
| root | localhost | |
| root | hostpc | |
| root | 127.0.0.1 | |
| root | ::1 | |
+------+-----------+----------+
4 rows in set (0.00 sec)
設置MySQL用戶密碼
MariaDB [(none)]> update mysql.user set password=password('123456') where user='127.0.0.1';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
MariaDB [(none)]> set password for 'root'@'::1'=password('123456');
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> update mysql.user set password=password('123456') where host='127.0.0.1';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [(none)]> update mysql.user set password=password('123456') where host='hostpc';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [(none)]> flush privileges; 刷新受權表
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select User,Host,Password from mysql.user;
+------+-----------+-------------------------------------------+
| User | Host | Password |
+------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | hostpc | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | 127.0.0.1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | ::1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)
獲取幫助:mysqld --verbose --help 會顯示全部的MySQL變量,啓動MySQL時,能夠直接傳遞給MySQL的選項【有些選項只能在配置文件中用,有的只能在選項中使用】
--option, -option: 命令行選項
Usage: mysqld [OPTIONS] 默認讀取配置文件的順序
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
--print-defaults Print the program argument list and exit.
--defaults-extra-file=#:額外讀取的配置文件;Read this file after the global files are read.
--defaults-file=#: 僅讀取此處指定的配置文件 Read this file after the global files are read.
如今每次登錄時都要指定用戶名和密碼,爲其添加一個家目錄下的配置文件.my.cnf,在配置文件中指定用戶名和密碼
[root@hostpc mysql]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@hostpc mysql]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 177
Server version: 10.0.13-MariaDB-log Source distribution
Copyright (c) 2000, 2014, Oracle, SkySQL Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> \q
Bye
[root@hostpc mysql]# cd
[root@hostpc ~]# vim .my.cnf
[root@hostpc ~]# cat .my.cnf 用戶名和密碼都指定了
[mysql]
user = root
host = localhost
password = '123456'
[root@hostpc ~]# mysql 登陸就不須要提供密碼了
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 179
Server version: 10.0.13-MariaDB-log Source distribution
Copyright (c) 2000, 2014, Oracle, SkySQL Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
windows上配置文件位置讀取
With Windows servers the following order of precedence is used:
%WINDIR%\my.ini, %WINDIR%\my.cnf
C:\my.ini, C:\my.cnf
%INSTALLDIR%\my.ini, %INSTALLDIR%\my.cnf
/path/to/file when --defaults-extra-file=/path/to/file is specified
會挨個讀取全部的配置文件
/usr/local/mysql/bin/目錄下,mysqldxx是服務器端程序
mysqld_safe 線程安全的mysqld,大多數狀況下啓動的都是mysqld_safe
mysqld_multi 在MySQL主機上同時啓動多個MySQL程序
mysqlxx是客戶端程序,客戶端程序是指可以經過mysql協議請求連入服務器端,跟MySQL服務器進行交互
mysql程序的類別:
服務器端程序:啓動並監聽於套接字上;mysqld, mysqld_safe, mysqld_multi
客戶端程序:可經過mysql協議連入服務器併發出請求的;mysql, mysqlbinlog, mysqladmin, mysqldump等
工具程序:運行於服務器進程所在的主機,實現一些管理或維護操做,myisamchk
客戶端程序的通用選項:
-u, --user= 指定用戶
-u root, -uroot, --user=root 短選項和參數之間能夠沒有空格
-h, --host= 指定主機
-p, --password= 指定密碼
--protocol= 指定什麼方式進行通訊
tcp:
socket: unix sock 是實現本機通訊的,windows上沒有實現
pipe:
memory:
--port: 當Protocol是tcp時使用的端口;
--socket: 至關於--protocol socket
其它選項:
-D DBNAME 登陸時,進入到DBNAME
--database=
mysql客戶端程序:mysql
運行方式有兩類:
交互式模式:
批模式:mysql < /path/from/somefile.sql
交互式模式:
命令有兩類:
客戶端命令:help可列出全部命令
clear, \c: Clear the current input statement. 取消當前命令的執行
ego, \G: Send command to mysql server, display result vertically. 垂直顯示
go, \g: Send command to mysql server.
delimiter, \d: Set statement delimiter. 設置分隔符
quit, exit, \q: Exit mysql. Same as quit.
source, \. /path/from/somefile.sql: Execute an SQL script file. Takes a file name as an argument.
至關於mysql < /path/from/somefile.sql
system, \! COMMAND: 運行shell命令 Execute a system shell command.
use, \u DB_NAME: 將指定的庫設爲默認庫 Use another database. Takes database name as argument.
服務器端命令:help KEYWORD 須要在命令後加;號
SQL語句:
DDL 數據庫定義語言
DML 數據庫操做語言
help KEYWORD 查看幫助
選項:
-e 'SQL語句' 不連入MySQL服務器,直接執行sql語句
# mysql -e 'select User,Host,Password from mysql.user;'
MySQL客戶端程序鏈接服務器時,會首先讀取配置文件
mysql -->mysql protocol (TCP/IP) --> mysqld
mysqladmin工具:mysqladmin - client for administering a MySQL server 是一個客戶端工具
mysqladmin [options] command [arg] [command [arg]] ...
子命令:
create DB_NAME:
mysqldadmin [options] create DB_NAME;
drop DB_NAME: 會提示是否刪除
status:
顯示mysqld的簡要狀態信息,專用選項
[root@hostpc ~]# mysqladmin -u root -p status 須要指定用戶和密碼登陸
Enter password:
Uptime: 13838 Threads: 1 Questions: 41 Slow queries: 0 Opens: 4 Flush tables: 1 Open tables: 67 Queries per second avg: 0.002
[root@hostpc ~]# mysqladmin status
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'
若是想不輸入密碼,則能夠在.my.cnf配置文件添加一個客戶端段
[root@hostpc ~]# cat .my.cnf
[mysql]
user = root
host = localhost
password = '123456'
[client] 全部客戶端工具均可以經過此用戶來登陸了
user = root
host = localhost
password = '123456'
[root@hostpc ~]# mysqladmin status
Uptime: 15812 Threads: 1 Questions: 42 Slow queries: 0 Opens: 4 Flush tables: 1 Open tables: 67 Queries per second avg: 0.002
--sleep #: 每間隔1秒顯示一次
# mysqladmin status --sleep 1
--count #: 顯示的次數
#mysqladmin status --sleep 1 --count 5
extended-status: 顯示mysqld的全部服務器變量和他們的當前值
[root@hostpc ~]# mysqladmin extended-status | grep select
| Com_insert_select | 0 |
| Com_replace_select | 0 |
| Com_select | 8 |
| Connection_errors_select | 0 |
flush-privileges: 刷新受權表,至關於reload命令
flush-hosts: 清除dns緩存及被拒絕的客戶端列表緩存
flush-logs: 滾動日誌後就會從新開始記錄日誌, 通常是二進制日誌和中繼日誌
flush-status: 重置各狀態變量
flush-tables: 關閉當前打開的全部的表文件句柄;若是某文件句柄總被訪問,須要等待其訪問結束。
flush-treads: 重置線程緩存;
password: 設置密碼
ping: 測試服務器是否在線
[root@hostpc ~]# mysqladmin ping
mysqld is alive
processlist: 顯示當前服務器上的全部線程,每一個線程都有其id號 # mysqladmin processlist refresh: 至關於執行flush-hosts和flush-logs shutdown: 關閉服務器進程 ; start-slave, stop-slave: 這個是主從複製時會使用到,啓動、關閉從服務器線程; variables: 顯示服務器變量