MariaDB是MySQL创始人基于MySQL的另起的一个分支,其功能上绝大部分兼容于MySQL。MariaDB的官网为https://mariadb.com/。

    MySQL基本架构如下所示:

wKiom1UvE3uy8Y1nAAMHMbDG6Ws779.jpg

    MySQL核心组件:

       连接池作用:认证、线程、连接数限制,内存检查、缓存;

       SQL接口:DML(数据操作语言),DDL(数据定义语言)、关系型数据库的基本抽象;

       Parser(分析器):查询转换、对象权限检查;

       优化器:访问路径、性能相关的统计数据;

       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

wKiom1UvIw6wcaDLAAFlYgvh4ao232.jpg     

    所有连接都是通过线程来实现的,线程池满了以后,连接需要在队列中等待
    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: 显示服务器变量