利用docker-compose安装lnmp(Nginx mariadb php7.0 )

对于Docker来说,最大的便利就是能快速的搭建起一个个的容器,容器之间可以通过网络和文件来进行通信。

之前我已经将自己的博客使用docker搭建起来了,这里简单记录一下docker-compose文件内容。

我的博客的架构为lnmp,依赖的容器有:

  • Nginx(Port:80)

  • mariadb(Port:3306)

  • wordpress+php7.0-fpm(Port:9000)

  • phpmyadmin(Port:8009)

docker-compose.yml文件内容如下

nginx:
    image: nginx:latest
    ports:
        - '80:80'
    volumes:
        - ./nginx:/etc/nginx/conf.d
        - ./logs/nginx:/var/log/nginx
        - ./jialeens:/var/www/html
    links:
        - wordpress
    restart: always

mysql:
    image: mariadb
    ports:
        - '3306:3306'
    volumes:
        - ./db-data:/var/lib/mysql
    environment:
        - MYSQL_ROOT_PASSWORD=******
    restart: always

wordpress:
    image: wordpress:4.8.0-php7.0-fpm
    ports:
        - '9000:9000'
    volumes:
        - ./jialeens:/var/www/html
    environment:
        - WORDPRESS_DB_NAME=***
        - WORDPRESS_TABLE_PREFIX=wp_
        - WORDPRESS_DB_HOST=mysql
        - WORDPRESS_DB_PASSWORD=*****
    links:
        - mysql
    restart: always
phpmyadmin:
  image: phpmyadmin/phpmyadmin
  links:
    - mysql
  environment:
    PMA_HOST: mysql
    PMA_PORT: 3306
  ports:
    - '8009:80'

Nginx配置文件:

jialeens.com.conf

server {
    listen 80;
    server_name jialeens.com www.jialeens.com;

    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    client_max_body_size 100m;
    root /var/www/html;
    index index.php;

    access_log /var/log/nginx/jialeens-access-http.log;
    error_log /var/log/nginx/jialeens-error-http.log;

    if ($host = 'jialeens.com') {
        return 301 http://www.jialeens.com$request_uri;
    }
    location ~* ^.+.(js|ico|gif|jpg|jpeg|png|html|htm)$ {
       log_not_found off;
       access_log off;
       expires 7d;
    }
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ .php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass wordpress:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PHP_VALUE "upload_max_filesize=128M n post_max_size=128M";
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

因为流量不大,所以没做fastcgi的缓存,以后有空再弄吧。

Linux MariaDB数据库主从复制配置

摘要:/etc/my.cnf然后按i编辑,输入下图红框中两行数据3、退出之后重启数据库systemctl restart mariadb4、进入数据库mysql -u root -p查看。

未分类

1、需要两个数据库服务端,数据库版本一致,并且互通

2、先来配置主数据库

vi /etc/my.cnf

然后按i编辑,输入下图红框中两行数据

3、退出之后重启数据库

systemctl restart mariadb

4、进入数据库

mysql -u root -p

查看主数据库状态

show master statusG;

根据返回的信息可以得出结论,配置的主数据库文件没有问题

5、锁定所有的表,使其他人不能对表做出修改的操作

flush tables with read lock;

6、退出数据库,接下来对数据库做一个导出

mysqldump -hlocalhost -uroot -p3306 -p test > /home/test.sql

然后输入数据库密码即可

7、进入home目录,看test.sql是否已创建

8、利用scp命令把test.sql文件传到从数据库服务器上

scp test.sql [email protected]:/home/

输入从服务器密码

9、在从数据库服务器上查看文件是否上传成功

10、回到主数据库服务器,解除表的锁定

unlock tables;

11、在主数据库中把从数据库需要用的用户创建出来

grAnt replication slave on *.* to 'slave'@'192.168.2.197' identified by '123456';

‘slave’为用户名  ’192.168.2.197’为从数据库IP地址  ’123456’为密码

12、主数据库服务器配置完成,接下来配置从数据库服务器,在主数据库服务器中进入数据库界面,执行第4步操作,把界面放到旁边,待会用得到

13、操作从数据库服务器,一样的,也需要修改数据库配置文件参数

vi /etc/my.cnf

14、同样的,配置完成后重启数据库

15、进入数据库,设置主从复制

CHANGE MASTER TO MASTER_HOST='192.168.2.196',MASTER_USER='slave', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS= 555;

后面两项要填写的参数为第12步中显示的参数

16、开启主从复制

START SLAVE;

17、查看从数据库状态

show slave statusG;

可以看到,已经连接成功,配置完成!

CentOS6使用二进制安装mariadb

MariaDB 是一个采用Maria 存储引擎的MySQL分支版本,是由原来 MySQL 的作者Michael Widenius创办的公司所开发的免费开源的数据库服务器。MariaDB是目前最受关注的MySQL数据库衍生版,也被视为开源数据库MySQL的替代品。除了使用Linux 各发行版供应商的程序包安装,也可以选择基于二进制格式的程序包进行安装。具体安装步骤如下:

1、下载二进制源码

官网下载地址http://downloads.mariadb.org

2、创建系统用户

[root@Centos6 ~]# groupadd -r -g 36 mysql
[root@Centos6 ~]# useradd -r -u 36 -g 36 mysql

3、准备二进制程序

解压缩包到/usr/local

[root@Centos6 ~]# tar xf /root/mariadb-5.5.57-linux-x86_64.tar.gz -C /usr/local/

创建软链接并修改目录属组为mysql

[root@Centos6 local]#cd /usr/local
[root@Centos6 local]#ln -sv  mariadb-5.5.57-linux-x86_64/ mysql
    `mysql' -> `mariadb-5.5.57-linux-x86_64/'
[root@Centos6 local]# chown -R root:mysql /usr/local/mysql/

4、准备mysql数据存储目录

建议把mysql数据存在基于逻辑卷的单独分区

[root@Centos6 local]#lvcreate -L 20G -n mydata vg_centos6
[root@Centos6 local]#mkfs.ext4 /dev/vg_centos6/mydata

设置开机自动挂载逻辑卷mydata到/mydata

[root@Centos6 local]mkdir /mydata
[root@Centos6 local]vim /etc/fstab
    /dev/vg_centos6/mydata /mydata                  ext4    defaults        0 0
[root@Centos6 local]mount -a

创建存储目录/mydata/data并修改属主属组为mysql

[root@Centos6 local]chown mysql:mysql /mydata/data

5、创建数据库文件

安装包提供了自动生成数据库的脚本/usr/local/mysql/scripts/mysql_install_db,在/usr/local/mysql目录下运行该脚本

[root@Centos6 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/mydata/data   
[root@Centos6 mysql]# ./scripts/mysql_install_db --help                               <-- 可以查看脚本帮助
[root@Centos6 mysql]# ls /mydata/data
aria_log.00000001  aria_log_control  mysql  performance_schema  test

6、准备mysqld程序配置文件

​配置文件查找次序: /etc/my.cnf – > /etc/mysql/my.cnf– >– default-extrafile=/PATH/TO/CONF_FILE(第5步中脚本选项指定的配置文件) – > ~/. my.cnf

安装包提供了几种不同配置的模板配置文件,位于目录/usr/local/mysql/suport-files/;可以根据数据库的大小及服务器配置等选择合适的模板进行修改

[root@Centos6 mysql]# mkdir /etc/mysql
[root@Centos6 mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf
[root@Centos6 mysql]# vim /etc/mysql/my.cnf
datadir = /mydata/data                     <--指定数据库文件存储目录
innodb_file_per_table = on                 <--数据库中各表格以单个文件存储
skip_name_resolve = on                     <--禁止主机名解析

7、准备日志文件

Centos6–>/var/log/mysqld.log

注意在Centos7里自动生成,不用手动创建–>/var/log/mariadb/mariadb.log

[root@Centos6 mysql]# touch /var/log/mysqld.log
[root@Centos6 mysql]# chown mysql:mysql /var/log/mysqld.log

8、准备服务脚本,并启动服务

[root@Centos6 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@Centos6 mysql]# chkconfig --add mysqld
[root@Centos6 mysql]# chkconfig --list mysqld
[root@Centos6 ~]# vi /etc/profile.d/my.sh                       <--创建系统配置文件,将可执行程序mysql路径加入PATH变量
export PATH=/usr/local/mysql/bin/:$PATH
[root@Centos6 mysql]#service mysqld start

9、运行mysql命令–>交互式客户端程序

[root@Centos6 ~]#mysql                          <-- 默认空密码登录
MariaDB [mysql]> use mysql
Database changed
MariaDB [mysql]> select user,host,password from user;
+------+-----------+----------+
| user | host      | password |
+------+-----------+----------+
| root | localhost |          |
| root | centos6.9 |          |
| root | 127.0.0.1 |          |
| root | ::1       |          |
|      | localhost |          |              <-- 表示允许匿名登录
|      | centos6.9 |          |
+------+-----------+----------+
6 rows in set (0.01 sec)
  • mysql用户账号由两部分组成:’USERNAME’@’HOST’

  • HOST用于限制此用户可通过哪些远程主机连接mysql服务(限制客户端)

  • HOST支持CIDR IP表示法;也支持使用通配符:

  • % 匹配任意长度的任意字符 eg: 192.168.%.%

  • _ 匹配任意单个字符

10、安全初始化

从文章第9步可以看出,数据库默认是允许匿名登录及无密码登录,这是非常不安全的,因此,我们还需要进行安全初始化

[root@Centos6 ~]# /usr/local/mysql/bin/mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): 
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y                  
New password:                   <-- 设置root密码
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y               <-- 禁止匿名登录
 ... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y        <-- 禁止root远程登录
 ... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] 
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y        <-- 生效权限
 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
[root@Centos6 ~]# mysql                 <-- 无密码登录已经禁止
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@Centos6 ~]# mysql -uroot -p           <-- 正确登入
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 17
Server version: 5.5.57-MariaDB 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)]> use mysql
Database changed
MariaDB [mysql]> select user,host,password from user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | 127.0.0.1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | ::1       | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)

CentOS 7.0安装LAMP服务器(PHP+MariaDB+Apache)

1、关闭firewall:

systemctl stop firew
alld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动

2、安装iptables防火墙(#可不安装)

yum install iptables-services #安装
vi /etc/sysconfig/iptables #编辑防火墙配置文件
//配置文件:
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
//:wq! #保存退出

关闭 SELINUX

vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存退出
setenforce 0 #使配置立即生效

一、Apache安装

yum install httpd #根据提示,输入Y安装即可成功安装
systemctl start httpd.service #启动apache
systemctl stop httpd.service #停止apache
systemctl restart httpd.service #重启apache
systemctl enable httpd.service #设置apache开机启动

二、安装MariaDB

yum install mariadb mariadb-server 
//#询问是否要安装,输入Y即可自动安装,直到安装完成
systemctl start mariadb.service #启动MariaDB
systemctl stop mariadb.service #停止MariaDB
systemctl restart mariadb.service #重启MariaDB
systemctl enable mariadb.service #设置开机启动
cp /usr/share/mysql/my-huge.cnf /etc/my.cnf 
//拷贝配置文件(注意:如果/etc目录下面默认有一个my.cnf,直接覆盖即可)

设置密码

mysql_secure_installation
systemctl restart mariadb.service

三、安装PHP

//主程序
yum install php
//安装模块
yum install php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash
systemctl restart mariadb.service #重启MariaDB
systemctl restart httpd.service #重启apache

四、安装phpMyAdmin

//主程序
sudo yum install phpmyadmin php-mcrypt
//修改配置文件
vi /etc/httpd/conf.d/phpMyAdmin.conf 

<Directory /usr/share/phpMyAdmin/>
  AddDefaultCharset UTF-8

  <IfModule mod_authz_core.c>
   # Apache 2.4
   <RequireAny>
    #Require ip 127.0.0.1
    #Require ip ::1
    Require all granted
   </RequireAny>
  </IfModule>
  <IfModule !mod_authz_core.c>
   Order Deny,Allow
   Deny from All
   Allow from 127.0.0.1
   Allow from ::1
  </IfModule>
</Directory>

<Directory /usr/share/phpMyAdmin/setup/>
  <IfModule mod_authz_core.c>
   # Apache 2.4
   <RequireAny>
    #Require ip 127.0.0.1
    #Require ip ::1
    Require all granted
   </RequireAny>
  </IfModule>
  <IfModule !mod_authz_core.c>
   Order Deny,Allow
   Deny from All
   Allow from 127.0.0.1
   Allow from ::1
  </IfModule>
</Directory>

systemctl restart httpd #重启httpd

CentOS 7.2安装配置MariaDB Galera Cluster(10.1.21-MariaDB) 三主集群环境

MariaDB Galera Cluster 介绍

Galera Cluster是由第三方公司Codership所研发的一套免费开源的集群高可用方案,实现了数据零丢失,官网地址为http://galeracluster.com/。其在MySQLInnoDB存储引擎基础上打了wrep(虚拟全同步复制),Percona/MariaDB已捆绑在各自的发行版本中。

MariaDB Galera Cluster是MariaDB同步多主机集群。它仅支持XtraDB/InnoDB存储引擎(虽然有对MyISAM实验支持,具体看wsrep_replicate_myisam系统变量)。

MariaDB Galera Cluster主要功能:

  • 同步复制

  • 真正的multi-master,即所有节点可以同时读写数据库

  • 自动的节点成员控制,失效节点自动被清除

  • 新节点加入数据自动复制

  • 真正的并行复制,行级

  • 用户可以直接连接集群,使用感受上与MySQL完全一致

优势:

  • 因为是多主,所以不存在Slavelag(延迟)

  • 不存在丢失事务的情况

  • 同时具有读和写的扩展能力

  • 更小的客户端延迟

  • 节点间数据是同步的,而Master/Slave模式是异步的,不同slave上的binlog可能是不同的

缺点:

  • 加入新节点时开销大,需要复制完整的数据

  • 不能有效地解决写扩展的问题,所有的写操作都发生在所有的节点

  • 有多少个节点,就有多少份重复的数据

  • 由于事务提交需要跨节点通信,即涉及分布式事务操作,因此写入会比主从复制慢很多,节点越多,写入越慢,死锁和回滚也会更加频繁

  • 对网络要求比较高,如果网络出现波动不稳定,则可能会造成两个节点失联,Galera Cluster集群会发生脑裂,服务将不可用

还有一些地方存在局限:

  • 仅支持InnoDB/XtraDB存储引擎,任何写入其他引擎的表,包括mysql.*表都不会被复制。但是DDL语句可以复制,但是insert into mysql.user(MyISAM存储引擎)之类的插入数据不会被复制

  • Delete操作不支持没有主键的表,因为没有主键的表在不同的节点上的顺序不同,如果执行select … limit …将出现不同的结果集

  • LOCK/UNLOCK TABLES/FLUSH TABLES WITH READ LOCKS不支持单表所锁,以及锁函数GET_LOCK()、RELEASE_LOCK(),但FLUSH TABLES WITH READ LOCK支持全局表锁

  • General Query Log日志不能保存在表中,如果开始查询日志,则只能保存到文件中

  • 不能有大事务写入,不能操作wsrep_max_ws_rows=131072(行),且写入集不能超过wsrep_max_ws_size=1073741824(1GB),否则客户端直接报错

  • 由于集群是乐观锁并发控制,因此,在commit阶段会有事务冲突发生。如果两个事务在集群中的不同节点上对同一行写入并提交,则失败的节点将回滚,客户端返回死锁报错

  • XA分布式事务不支持Codership Galera Cluster,在提交时可能会回滚

  • 整个集群的写入吞吐量取决于最弱的节点限制,集群要使用同一的配置

技术:

Galera集群的复制功能是基于认证的复制,其流程如下:

未分类

当客户端发出一个commit的指令,在事务被提交之前,所有对数据库的更改都会被write-set收集起来,并且将write-set 记录的内容发送给其他节点。

write-set 将在每个节点上使用搜索到的主键进行确认性认证测试,测试结果决定着节点是否应用write-set更改数据。如果认证测试失败,节点将丢弃 write-set ;如果认证测试成功,则事务提交,工作原理如下图:

未分类

关于新节点的加入,流程如下:

未分类

新加入的节点叫做Joiner,给Joiner提供复制的节点叫Donor。在该过程中首先会检查本地grastate.dat文件的seqno事务号是否在远端donor节点galera.cache文件里,如果存在,那么进行Incremental State Transfer(IST)增量同步复制,将剩余的事务发送过去;如果不存在那么进行State Snapshot Transfer(SST)全量同步复制。SST有三种全量拷贝方式:mysqldump、rsync和xtrabackup。SST的方法可以通过wsrep_sst_method这个参数来设置。

未分类

备注:

SST是指从donor到joiner的数据全量拷贝,它通常使用在一个新的节点加入时,为了与集群同步,新的节点不得不去一个已经在集群中的节点上拷贝数据,在PXC(Percona Xtradb Cluster)中,有三种SST的方法,mysqldump,rsync,Xtrabackup。

建议使用XtraBackup,另外对XtraBackup补充说明:

在XtraBackup 2.1.x版本里,使用innobackupex备份时,备份流程如下:

  1. 备份InnoDB表数据

  2. 执行全局表读锁FLUSH TABLES WITH READ LOCKS

  3. 拷贝.frm和MyISAM表数据

  4. 得到当前的binlog文件名和position点

  5. 完成redo log事务日志的后台复制

  6. 解锁UNLOCK TABLES

由上面可以看出如果备份好几张MyISAM存储的大表时,将会进行锁表。

环境信息

  • MariaDB Server:10.1.21-MariaDB

  • CentOS:CentOS Linux release7.2.1511 (Core)

MariaDB Galera Cluster 三个集群节点主机名和IP地址信息:

  • 192.168.1.104 mariadb-a03

  • 192.168.1.105 mariadb-a04

  • 192.168.1.106 mariadb-a05

环境准备

1、配置hosts文件

# cat /etc/hosts  
127.0.0.1 localhost.localdomain localhost  
192.168.1.104 mariadb-a03  
192.168.1.105 mariadb-a04  
192.168.1.106 mariadb-a05  

2、 /etc/security/limits.conf

* soft nofile 65536  
* hard nofile 65536  

3、 /etc/sysctl.conf

fs.file-max=655350  
net.ipv4.ip_local_port_range = 1025 65000  
net.ipv4.tcp_tw_recycle = 1  

最后执行:

# sysctl -p

4、 安装Percona XtraBackup热备份工具

下载地址:

https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.6/binary/tarball/percona-xtrabackup-2.4.6-Linux-x86_64.tar.gz

解压缩:

# tar -zxvf percona-xtrabackup-2.4.6-Linux-x86_64.tar.gz

拷贝脚本到指定位置:

# cd percona-xtrabackup-2.4.6-Linux-x86_64/bin/  
# cp -a * /usr/bin/  

安装依赖的一些包,比如lsof,socat,openssl,tar等

创建XtraBackup备份时用的用户名和密码:

MariaDB [(none)]> grant all on *.* to 'galera'@'localhost' identified by '123456';  

部署MariaDB

从MariaDB 10.1版本开始,Galera Cluster就已经包含在MariaDB包里面了,不需要单独部署MariaDB-Galera-server 和galera 包。

这里演示使用YUM方式部署MariaDB Galera Cluster。

步骤一:配置Yum源(192.168.1.104,192.168.1.105,192.168.1.106)

# touch /etc/yum.repos.d/MariaDB-IDC.repo  
添加如下内容:  
[mariadb]  
name = MariaDB  
baseurl =http://yum.mariadb.org/10.1/centos7-amd64  
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB  
gpgcheck=1  

由于我们的环境无法访问外网,单独部署一个MariaDB的Yum源:

# cat /etc/yum.repos.d/MariaDB-IDC.repo  
[MariaDB-10.1-IDC]  
name=MariaDB-10.1-IDC  
baseurl=http://192.168.1.100/repo/yum.mariadb.org/10.1/centos7-amd64  
gpgcheck=0  
enabled=1  

步骤二:安装MariaDB(192.168.1.104,192.168.1.105,192.168.1.106)

# yum install MariaDB-server MariaDB-clientgalera  -y  

配置 MariaDB Galera Cluster

下面我们开始配置MariaDB Galera Cluster,分别修改MariaDB Galera集群的每个节点上的/etc/my.cnf.d/server.cnf文件,具体每个节点的内容如下:

1、192.168.1.104节点的/etc/my.cnf.d/server.cnf文件内容:

[root@mariadb-a03 ~]# cat /etc/my.cnf.d/server.cnf | grep -v "#" | grep -v "^$"  
[server]  
[mysqld]  
server_id=128  
datadir=/app/galera  
user=mysql  
skip-external-locking  
skip-name-resolve  
character-set-server=utf8  

[galera]  
wsrep_causal_reads=ON  #节点应用完事务才返回查询请求  
wsrep_provider_options="gcache.size=4G"#同步复制缓冲池  
wsrep_certify_nonPK=ON   #为没有显式申明主键的表生成一个用于certificationtest的主键,默认为ON  
#log-bin=/app/galera/mysql-bin  #如果不接从库,注释掉  
#log_slave_updates=1         #如果不接从库,注释掉  
query_cache_size=0           #关闭查询缓存  
wsrep_on=ON   #开启全同步复制模式  
wsrep_provider=/usr/lib64/galera/libgalera_smm.so#galera library  
wsrep_cluster_name=MariaDB-Galera-Cluster  
    wsrep_cluster_address="gcomm://192.168.1.104,192.168.1.105,192.168.1.106"  #galera cluster URL  
wsrep_node_name=mariadb-a03  
wsrep_node_address=192.168.1.104  
binlog_format=row  
default_storage_engine=InnoDB  
innodb_autoinc_lock_mode=2   #主键自增模式修改为交叉模式  
wsrep_slave_threads=8  #开启并行复制线程,根据CPU核数设置  
innodb_flush_log_at_trx_commit=0   #事务提交每隔1秒刷盘  
innodb_buffer_pool_size=2G  
wsrep_sst_method=rsync  
[embedded]  
[mariadb]  
[mariadb-10.1]  

上面配置使用的是rsync方式同步数据,如果要使用xtrabackup方式(建议使用),需要设置:

wsrep_sst_auth=galera:123456

wsrep_sst_method=xtrabackup-v2 #默认是rsync全量拷贝,但是需要在donor节点上执行全局读锁(flushtables with read lock),建议采用xtrabackup热备份方式,只有在备份.frm表结构文件才会锁表

2、 192.168.1.105节点的/etc/my.cnf.d/server.cnf文件内容:

[root@mariadb-a04 ~]# cat /etc/my.cnf.d/server.cnf | grep -v "#" | grep -v "^$"  
[server]  
[mysqld]  
server_id=129  
datadir=/app/galera  
user=mysql  
skip-external-locking  
skip-name-resolve  
character-set-server=utf8  

[galera]  
wsrep_causal_reads=ON  
wsrep_provider_options="gcache.size=4G"  
wsrep_certify_nonPK=ON  
query_cache_size=0  
wsrep_on=ON  
wsrep_provider=/usr/lib64/galera/libgalera_smm.so  
wsrep_cluster_name=MariaDB-Galera-Cluster  
    wsrep_cluster_address="gcomm://192.168.1.104,192.168.1.105,192.168.1.106"  
wsrep_node_name=mariadb-a04  
wsrep_node_address=192.168.1.105  
binlog_format=row  
default_storage_engine=InnoDB  
innodb_autoinc_lock_mode=2  
wsrep_slave_threads=8  
innodb_flush_log_at_trx_commit=0  
innodb_buffer_pool_size=2G  
wsrep_sst_method=rsync  
[embedded]  
[mariadb]  
[mariadb-10.1]  

3、 192.168.1.106节点的/etc/my.cnf.d/server.cnf文件内容:

[root@mariadb-a05 yum.repos.d]# cat /etc/my.cnf.d/server.cnf | grep -v "#" | grep -v "^$"  
[server]  
[mysqld]  
server_id=130  
datadir=/app/galera  
user=mysql  
skip-external-locking  
skip-name-resolve  
character-set-server=utf8  

[galera]  
wsrep_causal_reads=ON  
wsrep_provider_options="gcache.size=4G"  
wsrep_certify_nonPK=ON  
query_cache_size=0  
wsrep_on=ON  
wsrep_provider=/usr/lib64/galera/libgalera_smm.so  
wsrep_cluster_name=MariaDB-Galera-Cluster  
    wsrep_cluster_address="gcomm://192.168.1.104,192.168.1.105,192.168.1.106"  
wsrep_node_name=mariadb-a05  
wsrep_node_address=192.168.1.106  
binlog_format=row  
default_storage_engine=InnoDB  
innodb_autoinc_lock_mode=2  
wsrep_slave_threads=8  
innodb_flush_log_at_trx_commit=0  
innodb_buffer_pool_size=2G  
wsrep_sst_method=rsync  
[embedded]  
[mariadb]  
[mariadb-10.1]  

MariaDB一个节点初始化安装(192.168.1.104):

# mysql_install_db --defaults-file=/etc/my.cnf.d/server.cnf --user=mysql  

在192.168.1.104节点上通过bootstrap启动(第一次启动一定要使用–wsrep-new-cluster,再次启动就不需要)

mysqld_safe --defaults-file=/etc/my.cnf.d/server.cnf --user=mysql  --wsrep-new-cluster &  

在192.168.1.104节点上设置root密码以及安全设置(192.168.1.104,192.168.1.105,192.168.1.106)

/usr/bin/mysql_secure_installation  
或  
mysql_secure_installation  

在192.168.1.105,192.168.1.106节点启动MariaDB:

mysqld_safe --defaults-file=/etc/my.cnf.d/server.cnf --user=mysql  &  

验证操作

登录三个节点查看

192.168.1.104节点:  
MariaDB [(none)]> SHOW STATUS LIKE 'wsrep_cluster_size';  
+--------------------+-------+  
| Variable_name      | Value |  
+--------------------+-------+  
| wsrep_cluster_size | 3     |  
+--------------------+-------+  
1 row in set (0.00 sec)  

MariaDB [(none)]> show global status like 'ws%';  
+------------------------------+-------------------------------------------------------------+  
| Variable_name                | Value                                                      |  
+------------------------------+-------------------------------------------------------------+  
| wsrep_apply_oooe             | 0.000000                                                   |  
| wsrep_apply_oool             | 0.000000                                                   |  
| wsrep_apply_window           | 1.000000                                                    |  
| wsrep_causal_reads           | 11                                                         |  
| wsrep_cert_deps_distance     | 1.000000                                                   |  
| wsrep_cert_index_size        | 2                                                           |  
| wsrep_cert_interval          | 0.000000                                                   |  
| wsrep_cluster_conf_id        | 3                                                          |  
| wsrep_cluster_size           | 3                                                          |  
| wsrep_cluster_state_uuid     |3108c722-ff29-11e6-a31f-bb500598d033                        |  
| wsrep_cluster_status         | Primary                                                     |  
| wsrep_commit_oooe            | 0.000000                                                   |  
| wsrep_commit_oool            | 0.000000                                                   |  
| wsrep_commit_window          | 1.000000                                                    |  
| wsrep_connected              | ON                                                         |  
| wsrep_desync_count           | 0                                                          |  
| wsrep_evs_delayed            |                                                            |  
| wsrep_evs_evict_list         |                                                            |  
| wsrep_evs_repl_latency       | 0/0/0/0/0                                                   |  
| wsrep_evs_state              | OPERATIONAL                                                |  
| wsrep_flow_control_paused    | 0.000000                                                   |  
| wsrep_flow_control_paused_ns | 0                                                           |  
| wsrep_flow_control_recv      | 0                                                          |  
| wsrep_flow_control_sent      | 0                                                          |  
| wsrep_gcomm_uuid             |3107a278-ff29-11e6-96d3-374133af7e21                        |  
| wsrep_incoming_addresses     | 192.168.1.105:3306,192.168.1.106:3306,192.168.1.104:3306|  
| wsrep_last_committed         | 3                                                           |  
| wsrep_local_bf_aborts        | 0                                                          |  
| wsrep_local_cached_downto    | 1                                                          |  
| wsrep_local_cert_failures    | 0                                                           |  
| wsrep_local_commits          | 0                                                          |  
| wsrep_local_index            | 2                                                          |  
| wsrep_local_recv_queue       | 0                                                          |  
| wsrep_local_recv_queue_avg   | 0.000000                                                   |  
| wsrep_local_recv_queue_max   | 1                                                          |  
| wsrep_local_recv_queue_min   | 0                                                          |  
| wsrep_local_replays          | 0                                                          |  
| wsrep_local_send_queue       | 0                                                           |  
| wsrep_local_send_queue_avg   | 0.000000                                                   |  
| wsrep_local_send_queue_max   | 1                                                          |  
| wsrep_local_send_queue_min   | 0                                                          |  
| wsrep_local_state            | 4                                                          |  
| wsrep_local_state_comment    | Synced                                                     |  
| wsrep_local_state_uuid       |3108c722-ff29-11e6-a31f-bb500598d033                        |  
| wsrep_protocol_version       | 7                                                          |  
| wsrep_provider_name          | Galera                                                      |  
| wsrep_provider_vendor        | Codership Oy<[email protected]>                           |  
| wsrep_provider_version       | 25.3.19(r3667)                                              |  
| wsrep_ready                  | ON                                                         |  
| wsrep_received               | 10                                                         |  
| wsrep_received_bytes         | 806                                                        |  
| wsrep_repl_data_bytes        | 1044                                                       |  
| wsrep_repl_keys              | 3                                                          |  
| wsrep_repl_keys_bytes        | 93                                                          |  
| wsrep_repl_other_bytes       | 0                                                          |  
| wsrep_replicated             | 3                                                          |  
| wsrep_replicated_bytes       | 1329                                                        |  
| wsrep_thread_count           | 9                                                          |  
+------------------------------+-------------------------------------------------------------+  
58 rows in set (0.00 sec)

注释:

  • wsrep_cluster_status为Primary,表示节点为主节点,正常读写。

  • wsrep_ready为ON,表示集群正常运行。

  • wsrep_cluster_size为3,表示集群有三个节点。

创建数据库测试

192.168.1.104节点:  
[root@mariadb-a03 my.cnf.d]# mysql -uroot –pxxxxxx  
MariaDB [(none)]> create databasetest_db;  
Query OK, 1 row affected (0.01 sec)  
192.168.1.105节点查看:  
[root@mariadb-a04 my.cnf.d]# mysql -uroot -pxxxxxx  
MariaDB [(none)]> show databases;  
+--------------------+  
| Database           |  
+--------------------+  
| information_schema |  
| mysql              |  
| performance_schema |  
| test_db            |  
+--------------------+  
192.168.1.106节点查看:  
[root@mariadb-a05 my.cnf.d]# mysql -uroot -pxxxxxx  
MariaDB [(none)]> show databases;  
+--------------------+  
| Database           |  
+--------------------+  
| information_schema |  
| mysql              |  
| performance_schema |  
| test_db            |  
+--------------------+  
4 rows in set (0.00 sec)  

可以看到集群正常使用。

创建MyISAM表测试

[root@mariadb-a03 my.cnf.d]# mysql -uroot –pxxxxxx  
MariaDB [(none)]> use test_db;  
Database changed  
MariaDB [test_db]> create table myisam_tbl (id int,name text) ENGINE MyISAM;  
Query OK, 0 rows affected (0.01 sec)  

MariaDB [test_db]> insert into myisam_tbl values(1,'hive');  
Query OK, 1 row affected (0.00 sec)  

MariaDB [test_db]> insert into myisam_tbl values(2,'hbase');  
Query OK, 1 row affected (0.00 sec)  

其他节点查看:

[root@mariadb-a04 my.cnf.d]# mysql -uroot -pxxxxxx  
MariaDB [(none)]> use test_db;  
Reading table information for completion of table and column names  
You can turn off this feature to get a quicker startup with -A  

Database changed  
MariaDB [test_db]> select * from myisam_tbl;  
Empty set (0.00 sec)  

[root@mariadb-a05 my.cnf.d]# mysql -uroot –pxxxxxx  
MariaDB [(none)]> use test_db;  
Reading table information for completion of table and column names  
You can turn off this feature to get a quicker startup with -A  

Database changed  
MariaDB [test_db]> select * from myisam_tbl;  
Empty set (0.00 sec)  

可以看到MyISAM存储的表,Galera不支持同步。它仅支持XtraDB/ InnoDB存储引擎(虽然有对MyISAM实验支持,具体看wsrep_replicate_myisam系统变量)。

验证InnoDB存储的表

[root@mariadb-a03 my.cnf.d]# mysql -uroot  –pxxxxxx  
MariaDB [test_db]> create table innodb_tbl(id int,name text) ENGINE InnoDB;  
Query OK, 0 rows affected (0.04 sec)  

MariaDB [test_db]> insert into innodb_tbl values(1,'hive');  
Query OK, 1 row affected (0.00 sec)  

MariaDB [test_db]> insert into innodb_tbl values(2,'hbase');  
Query OK, 1 row affected (0.00 sec)  

MariaDB [test_db]>  

其他节点查看:

[root@mariadb-a04 my.cnf.d]# mysql -uroot -pxxxxxx  
MariaDB [(none)]> use test_db;  
Reading table information for completion oftable and column names  
You can turn off this feature to get aquicker startup with -A  

Database changed  
MariaDB [test_db]> select * from innodb_tbl;  
+------+-------+  
| id  | name  |  
+------+-------+  
|   1 | hive  |  
|   2 | hbase |  
+------+-------+  
2 rows in set (0.00 sec)  

[root@mariadb-a05 my.cnf.d]# mysql -uroot –pxxxxxx  
MariaDB [(none)]> use test_db;  
Reading table information for completion of table and column names  
You can turn off this feature to get a quicker startup with -A  

Database changed  
MariaDB [test_db]> select * from innodb_tbl;  
+------+-------+  
| id  | name  |  
+------+-------+  
|   1 | hive  |  
|   2 | hbase |  
+------+-------+  
2 rows in set (0.00 sec)  

模拟节点故障

将192.168.1.104数据库停止掉:

[root@mariadb-a03 system]# mysqladmin -uroot -p "shutdown"  

然后在其他节点192.168.1.105执行:

MariaDB [test_db]> show global status like 'wsrep%';  
+------------------------------+-----------------------------------------------+  
| Variable_name                | Value                                         |  
+------------------------------+-----------------------------------------------+  
| wsrep_apply_oooe             | 0.000000                                      |  
| wsrep_apply_oool             | 0.000000                                      |  
| wsrep_apply_window           | 1.000000                                      |  
| wsrep_causal_reads           | 26                                            |  
| wsrep_cert_deps_distance     | 1.142857                                      |  
| wsrep_cert_index_size        |6                                            |  
| wsrep_cert_interval          | 0.000000                                      |  
| wsrep_cluster_conf_id        | 6                                             |  
| wsrep_cluster_size          | 2                                             |  
| wsrep_cluster_state_uuid     |3108c722-ff29-11e6-a31f-bb500598d033         |  
| wsrep_cluster_status         | Primary                                       |  
| wsrep_commit_oooe            | 0.000000                                      |  
| wsrep_commit_oool            | 0.000000                                      |  
| wsrep_commit_window          | 1.000000                                      |  
| wsrep_connected              | ON                                            |  
| wsrep_desync_count           | 0                                             |  
| wsrep_evs_delayed            |                                              |  
| wsrep_evs_evict_list         |                                              |  
| wsrep_evs_repl_latency       |0.000403989/0.000656768/0.0012094/0.0003239/4 |  
| wsrep_evs_state              | OPERATIONAL                                   |  
| wsrep_flow_control_paused    | 0.000000                                      |  
| wsrep_flow_control_paused_ns | 0                                             |  
| wsrep_flow_control_recv      | 0                                             |  
| wsrep_flow_control_sent      | 0                                             |  
| wsrep_gcomm_uuid             | 0ce8537e-ff2a-11e6-b037-8a383b6a8db5          |  
| wsrep_incoming_addresses    | 192.168.1.105:3306,192.168.1.106:3306       |  
| wsrep_last_committed         | 10                                            |  
| wsrep_local_bf_aborts        | 0                                            |  
| wsrep_local_cached_downto    | 4                                             |  
| wsrep_local_cert_failures    | 0                                             |  
| wsrep_local_commits          | 0                                             |  
| wsrep_local_index            | 0                                             |  
| wsrep_local_recv_queue       | 0                                             |  
| wsrep_local_recv_queue_avg   | 0.000000                                      |  
| wsrep_local_recv_queue_max   | 1                                             |  
| wsrep_local_recv_queue_min   | 0                                             |  
| wsrep_local_replays          | 0                                             |  
| wsrep_local_send_queue       | 0                                             |  
| wsrep_local_send_queue_avg   | 0.000000                                      |  
| wsrep_local_send_queue_max   | 1                                             |  
| wsrep_local_send_queue_min   | 0                                             |  
| wsrep_local_state            | 4                                             |  
| wsrep_local_state_comment    | Synced                                        |  
| wsrep_local_state_uuid       |3108c722-ff29-11e6-a31f-bb500598d033         |  
| wsrep_protocol_version       | 7                                             |  
| wsrep_provider_name          | Galera                                        |  
| wsrep_provider_vendor        | Codership Oy<[email protected]>             |  
| wsrep_provider_version       | 25.3.19(r3667)                                |  
| wsrep_ready                  | ON                                            |  
| wsrep_received               | 14                                            |  
| wsrep_received_bytes         | 3908                                          |  
| wsrep_repl_data_bytes        | 0                                             |  
| wsrep_repl_keys              | 0                                             |  
| wsrep_repl_keys_bytes        | 0                                             |  
| wsrep_repl_other_bytes       | 0                                             |  
| wsrep_replicated             | 0                                             |  
| wsrep_replicated_bytes       | 0                                             |  
| wsrep_thread_count           | 9                                             |  
+------------------------------+-----------------------------------------------+

此时集群为自动将192.168.1.104故障节点剔除掉,并且正常提供服务。

最后我们恢复失败的节点:

[root@mariadb-a03 system]# mysqld_safe --defaults-file=/etc/my.cnf.d/server.cnf --user=mysql &  

再次查看集群环境:

MariaDB [test_db]> SHOW STATUS LIKE 'wsrep_cluster_size';  
+--------------------+-------+  
| Variable_name      | Value |  
+--------------------+-------+  
| wsrep_cluster_size | 3     |  
+--------------------+-------+  
1 row in set (0.00 sec  

模拟脑裂后的处理

下面模拟在网络抖动发生丢包的情况下,两个节点失联导致脑裂。首先,在192.168.1.105和192.168.1.106两个节点上分别执行:

iptables -A INPUT -p tcp --sport 4567 -j DROP

iptables -A INPUT -p tcp --dport 4567 -j DROP

以上命令用来禁止wsrep全同步复制4567端口通信。

然后我们在192.168.1.104节点查看:

MariaDB [(none)]> show global statuslike 'ws%';  
可以看到下面的几个值:  
wsrep_cluster_size    1  
wsrep_cluster_status  non-Primary  
wsrep_ready         OFF  

MariaDB [(none)]> use test_db;  
ERROR 1047 (08S01): WSREP has not yetprepared node for application use  

MariaDB [(none)]> select@@wsrep_node_name;  
ERROR 1205 (HY000): Lock wait timeoutexceeded; try restarting transaction  

现在已经出现脑裂的情况,并且集群无法执行任何命令。

为了解决这个问题,可以执行:

set global wsrep_provider_options="pc.bootstrap=true";

通过这个命令来强制恢复出现脑裂的节点。

下面我们来验证一下:

MariaDB [(none)]> select @@wsrep_node_name;  
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction  
MariaDB [(none)]> set global wsrep_provider_options="pc.bootstrap=true";  
Query OK, 0 rows affected (0.00 sec)  

MariaDB [(none)]> select @@wsrep_node_name;  
+-------------------+  
| @@wsrep_node_name |  
+-------------------+  
| mariadb-a03     |  
+-------------------+  
1 row in set (0.27 sec)  

MariaDB [(none)]> use test_db;  
Reading table information for completion oft able and column names  
You can turn off this feature to get a quicker startup with -A  

Database changed  
MariaDB [test_db]> show tables;  
+-------------------+  
| Tables_in_test_db |  
+-------------------+  
| innodb_tbl        |  
| myisam_tbl        |  
+-------------------+  

最后我们将节点192.168.1.105和192.168.1.106恢复一下,只要清理一下iptables表即可(因为我的是测试环境,生产环境需要删除上面的规则即可):

iptables –F

各个节点验证一下:

192.168.1.104:  
MariaDB [test_db]> SHOW STATUS LIKE  'wsrep_cluster_size';  
+--------------------+-------+  
| Variable_name      | Value |  
+--------------------+-------+  
| wsrep_cluster_size | 3     |  
+--------------------+-------+  
1 row in set (0.00 sec)  


192.168.1.105:  
MariaDB [(none)]> select @@wsrep_node_name;  
+-------------------+  
| @@wsrep_node_name |  
+-------------------+  
| mariadb-a04     |  
+-------------------+  

避免脏读

Galera Cluster不是真正意义上的全同步复制,存在延迟。我们可以在一个节点上面执行FLUSH TABLES WITH READ LOCK;全局读锁。

然后在其他节点执行写操作,观察延迟情况。

比如我们在192.168.1.106节点执行全局读锁设置:

MariaDB [test_db]> flush tables with read lock;  
Query OK, 0 rows affected (0.00 sec)  

MariaDB [test_db]> select * from innodb_tbl;  
+------+------+  
| id  | name |  
+------+------+  
|   1 | hive |  
+------+------+  
1 row in set (0.00 sec)  


然后在192.168.1.104节点插入操作:  
MariaDB [test_db]> select @@wsrep_node_name;  
+-------------------+  
| @@wsrep_node_name |  
+-------------------+  
| mariadb-a03     |  
+-------------------+  
1 row in set (0.00 sec)  

MariaDB [test_db]> insert into innodb_tbl values(2,'hbase');  
Query OK, 1 row affected (0.00 sec)  

MariaDB [test_db]> select * from innodb_tbl;  
+------+-------+  
| id  | name  |  
+------+-------+  
|   1 | hive  |  
|   2 | hbase |  
+------+-------+  
2 rows in set (0.00 sec)  

在节点192.168.1.106上测试查询操作:  
MariaDB [test_db]> select * from innodb_tbl;  
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction  

这里之所以没有读取到脏数据,是因为我在MariaDB配置文件中设置了wsrep_causal_reads=ON;

我们将wsrep_causal_reads修改为0或OFF来看一下效果:

MariaDB [test_db]> set wsrep_causal_reads=0;  
Query OK, 0 rows affected, 1 warning (0.00sec)  

MariaDB [test_db]> select * from innodb_tbl;  
+------+------+  
| id  | name |  
+------+------+  
|   1 | hive |  
+------+------+  
1 row in set (0.00 sec)  

MariaDB [test_db]>  

总结

通过上面的一系列测试,最后总结一下:

1、 在生产环境下应该避免使用大事务,不建议在高并发写入场景下使用Galera Cluster架构,会导致集群限流,从而引起整个集群hang住,出现生产故障。针对这种情况可以考虑主从,实现读写分离等手段。

2、对数据一致性要求较高,并且数据写入不频繁,数据库容量也不大(50GB左右),网络状况良好的情况下,可以考虑使用Galera方案。

mysql(mariadb)启动报错数据恢复过程

一、启动mysql(mariadb)报错

(注:后文中mysql==mariadb):

未分类

二、查看mysql日志:

vim /var/log/mariadb/mariadb.log
InnoDB: End of page dump

160226 11:00:21  InnoDB: Page checksum 913642282 (32bit_calc: 472052024), prior-to-4.0.14-form checksum 2048873750
InnoDB: stored checksum 913642282, prior-to-4.0.14-form stored checksum 1622372148
InnoDB: Page lsn 0 142354744, low 4 bytes of lsn at page end 142348560
InnoDB: Page number (if stored to page already) 589,
InnoDB: space id (if created with >= MySQL-4.1.1 and stored already) 0
InnoDB: Page may be an update undo log page
InnoDB: Database page corruption on disk or a failed
InnoDB: file read of page 589.
InnoDB: You may have to recover from a backup.
InnoDB: It is also possible that your operating
InnoDB: system has corrupted its own file cache
InnoDB: and rebooting your computer removes the
InnoDB: error.
InnoDB: If the corrupt page is an index page
InnoDB: you can also try to fix the corruption
InnoDB: by dumping, dropping, and reimporting
InnoDB: the corrupt table. You can use CHECK
InnoDB: TABLE to scan your table for corruption.
InnoDB: See also  http://dev.mysql.com/doc/refman/5.5/en/forcing-innodb-recovery.html
InnoDB: about forcing recovery.
160226 11:00:21 [ERROR] mysqld got signal 6 ;
This could be because you hit a bug. It is also possible that this binary
or one of the libraries it was linked against is corrupt, improperly built,
or misconfigured. This error can also be caused by malfunctioning hardware.

To report this bug, see  http://kb.askmonty.org/en/reporting-bugs

We will try our best to scrape up some info that will hopefully help
diagnose the problem, but since we have already crashed,
something is definitely wrong and this may fail.

Server version: 5.5.44-MariaDB
key_buffer_size=134217728
read_buffer_size=131072
max_used_connections=0
max_threads=153
thread_count=0
It is possible that mysqld could use up to
key_buffer_size + (read_buffer_size + sort_buffer_size)*max_threads = 466713 K  bytes of memory
Hope that's ok; if not, decrease some variables in the equation.

Thread pointer: 0x0x0
Attempting backtrace. You can use the following information to find out
InnoDB: End of page dump
160226 11:00:30  InnoDB: Page checksum 913642282 (32bit_calc: 472052024), prior-to-4.0.14-form checksum 2048873750
InnoDB: stored checksum 913642282, prior-to-4.0.14-form stored checksum 1622372148
InnoDB: Page lsn 0 142354744, low 4 bytes of lsn at page end 142348560
InnoDB: Page number (if stored to page already) 589,
InnoDB: space id (if created with >= MySQL-4.1.1 and stored already) 0
InnoDB: Page may be an update undo log page
InnoDB: Database page corruption on disk or a failed
InnoDB: file read of page 589.
InnoDB: You may have to recover from a backup.
InnoDB: It is also possible that your operating
InnoDB: system has corrupted its own file cache
InnoDB: and rebooting your computer removes the
InnoDB: error.
InnoDB: End of page dump
160226 11:00:30  InnoDB: Page checksum 913642282 (32bit_calc: 472052024), prior-to-4.0.14-form checksum 2048873750
InnoDB: stored checksum 913642282, prior-to-4.0.14-form stored checksum 1622372148
InnoDB: Page lsn 0 142354744, low 4 bytes of lsn at page end 142348560
InnoDB: Page number (if stored to page already) 589,
InnoDB: space id (if created with >= MySQL-4.1.1 and stored already) 0
InnoDB: Page may be an update undo log page
InnoDB: Database page corruption on disk or a failed
InnoDB: file read of page 589.
InnoDB: You may have to recover from a backup.
InnoDB: It is also possible that your operating
InnoDB: system has corrupted its own file cache
InnoDB: and rebooting your computer removes the
InnoDB: error.
InnoDB: If the corrupt page is an index page
InnoDB: you can also try to fix the corruption
InnoDB: by dumping, dropping, and reimporting
InnoDB: the corrupt table. You can use CHECK
InnoDB: TABLE to scan your table for corruption.
InnoDB: See also  http://dev.mysql.com/doc/refman/5.5/en/forcing-innodb-recovery.html
InnoDB: about forcing recovery.
InnoDB: Ending processing because of a corrupt database page.
160226 11:00:30  InnoDB: Assertion failure in thread 140329989404736 in file buf0buf.c line 4032
InnoDB: We intentionally generate a memory trap.
InnoDB: Submit a detailed bug report to  http://bugs.mysql.com.
InnoDB: If you get repeated assertion failures or crashes, even
InnoDB: immediately after the mysqld startup, there may be
InnoDB: corruption in the InnoDB tablespace. Please refer to
InnoDB:  http://dev.mysql.com/doc/refman/5.5/en/forcing-innodb-recovery.html
InnoDB: about forcing recovery.
160226 11:00:30 [ERROR] mysqld got signal 6 ;
This could be because you hit a bug. It is also possible that this binary
or one of the libraries it was linked against is corrupt, improperly built,
160226 11:00:28  InnoDB: Page dump in ascii and hex (16384 bytes):
max_threads=153
thread_count=0 
It is possible that mysqld could use up to 
key_buffer_size + (read_buffer_size + sort_buffer_size)*max_threads = 466713 K  bytes of memory
Hope that's ok; if not, decrease some variables in the equation.

Thread pointer: 0x0x0 
Attempting backtrace. You can use the following information to find out
160226 11:00:19  InnoDB: Page dump in ascii and hex (16384 bytes):
InnoDB: End of page dump
160226 11:00:21  InnoDB: Page checksum 913642282 (32bit_calc: 472052024), prior-to-4.0.14-form checksum 2048873750
InnoDB: stored checksum 913642282, prior-to-4.0.14-form stored checksum 1622372148
InnoDB: Page lsn 0 142354744, low 4 bytes of lsn at page end 142348560
InnoDB: Page number (if stored to page already) 589,
InnoDB: space id (if created with >= MySQL-4.1.1 and stored already) 0
InnoDB: Page may be an update undo log page
InnoDB: Database page corruption on disk or a failed
InnoDB: file read of page 589.
InnoDB: You may have to recover from a backup.
InnoDB: It is also possible that your operating
InnoDB: system has corrupted its own file cache
InnoDB: and rebooting your computer removes the
InnoDB: error.
InnoDB: If the corrupt page is an index page
InnoDB: you can also try to fix the corruption
InnoDB: by dumping, dropping, and reimporting
InnoDB: the corrupt table. You can use CHECK
InnoDB: TABLE to scan your table for corruption.
InnoDB: See also  http://dev.mysql.com/doc/refman/5.5/en/forcing-innodb-recovery.html
InnoDB: about forcing recovery.
InnoDB: Ending processing because of a corrupt database page.
160226 11:00:21  InnoDB: Assertion failure in thread 139871429470272 in file buf0buf.c line 4032
InnoDB: We intentionally generate a memory trap.
InnoDB: Submit a detailed bug report to  http://bugs.mysql.com.
InnoDB: If you get repeated assertion failures or crashes, even
InnoDB: immediately after the mysqld startup, there may be
InnoDB: corruption in the InnoDB tablespace. Please refer to
InnoDB: http://dev.mysql.com/doc/refman/5.5/en/forcing-innodb-recovery.html
InnoDB: about forcing recovery.
160226 11:00:21 [ERROR] mysqld got signal 6 ;
This could be because you hit a bug. It is also possible that this binary
or one of the libraries it was linked against is corrupt, improperly built,
or misconfigured. This error can also be caused by malfunctioning hardware.

To report this bug, see  http://kb.askmonty.org/en/reporting-bugs

We will try our best to scrape up some info that will hopefully help
diagnose the problem, but since we have already crashed,
something is definitely wrong and this may fail.

Server version: 5.5.44-MariaDB
key_buffer_size=134217728
read_buffer_size=131072
max_used_connections=0
max_threads=153
thread_count=0
It is possible that mysqld could use up to
key_buffer_size + (read_buffer_size + sort_buffer_size)*max_threads = 466713 K  bytes of memory
Hope that's ok; if not, decrease some variables in the equation.

Thread pointer: 0x0x0
Attempting backtrace. You can use the following information to find out
InnoDB: End of page dump
160226 11:00:30  InnoDB: Page checksum 913642282 (32bit_calc: 472052024), prior-to-4.0.14-form checksum 2048873750
InnoDB: stored checksum 913642282, prior-to-4.0.14-form stored checksum 1622372148
InnoDB: Page lsn 0 142354744, low 4 bytes of lsn at page end 142348560
InnoDB: Page number (if stored to page already) 589,
InnoDB: space id (if created with >= MySQL-4.1.1 and stored already) 0
InnoDB: Page may be an update undo log page
InnoDB: Database page corruption on disk or a failed
InnoDB: file read of page 589.
InnoDB: You may have to recover from a backup.
InnoDB: It is also possible that your operating
InnoDB: system has corrupted its own file cache
InnoDB: and rebooting your computer removes the
InnoDB: error.
InnoDB: If the corrupt page is an index page
InnoDB: you can also try to fix the corruption
InnoDB: by dumping, dropping, and reimporting
or misconfigured. This error can also be caused by malfunctioning hardware.

To report this bug, see  http://kb.askmonty.org/en/reporting-bugs

We will try our best to scrape up some info that will hopefully help
diagnose the problem, but since we have already crashed,
something is definitely wrong and this may fail.

Server version: 5.5.44-MariaDB
key_buffer_size=134217728
read_buffer_size=131072
max_used_connections=0
max_threads=153
thread_count=0
It is possible that mysqld could use up to
key_buffer_size + (read_buffer_size + sort_buffer_size)*max_threads = 466713 K  bytes of memory
Hope that's ok; if not, decrease some variables in the equation.

Thread pointer: 0x0x0
Attempting backtrace. You can use the following information to find out
where mysqld died. If you see no messages after this, something went
terribly wrong...
stack_bottom = 0x0 thread_stack 0x48000
/usr/libexec/mysqld(my_print_stacktrace+0x3d)[0x7fa11fb574ed]
/usr/libexec/mysqld(handle_fatal_signal+0x515)[0x7fa11f76d385]
/lib64/libpthread.so.0(+0xf100)[0x7fa11ee9d100]
/lib64/libc.so.6(gsignal+0x37)[0x7fa11d6515f7]
/lib64/libc.so.6(abort+0x148)[0x7fa11d652ce8]
/usr/libexec/mysqld(+0x6971a2)[0x7fa11f9651a2]
/usr/libexec/mysqld(+0x6a8b17)[0x7fa11f976b17]
/usr/libexec/mysqld(+0x6919ee)[0x7fa11f95f9ee]
/usr/libexec/mysqld(+0x66313a)[0x7fa11f93113a]
/usr/libexec/mysqld(+0x655f93)[0x7fa11f923f93]
/usr/libexec/mysqld(+0x656dfc)[0x7fa11f924dfc]
/usr/libexec/mysqld(+0x65954e)[0x7fa11f92754e]
/usr/libexec/mysqld(+0x64290e)[0x7fa11f91090e]
/usr/libexec/mysqld(+0x5fbb9c)[0x7fa11f8c9b9c]
/usr/libexec/mysqld(_Z24ha_initialize_handlertonP13st_plugin_int+0x48)[0x7fa11f76f408]
/usr/libexec/mysqld(+0x37bff5)[0x7fa11f649ff5]
/usr/libexec/mysqld(_Z11plugin_initPiPPci+0x551)[0x7fa11f64fa61]
/usr/libexec/mysqld(+0x2ee4ba)[0x7fa11f5bc4ba]
/usr/libexec/mysqld(_Z11mysqld_mainiPPc+0x546)[0x7fa11f5bf5d6]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7fa11d63db15]
/usr/libexec/mysqld(+0x2e869d)[0x7fa11f5b669d]
The manual page at  http://dev.mysql.com/doc/mysql/en/crashing.html contains
information that should help you find out what is causing the crash.
160226 11:00:30 mysqld_safe mysqld from pid file /var/run/mariadb/mariadb.pid ended

三、接下来使用官方推荐的恢复数据方法:

1、设置恢复模式启动mysql(http://dev.mysql.com/doc/refman/5.5/en/forcing-innodb-recovery.html)

vim /etc/my.cnf

添加配置项:

innodb_force_recovery = 1

其中后面的值设置为1、如果1不想再逐步增加为2/3/4等。直到能启动mysql为止!!!

未分类

2、使用恢复模式重启mysql

systemctl restart mariadb

未分类

重启成功!!!!
测试数据库连接:mysql -uroot -p123456;

未分类

正常!!!

3、备份全部数据库表:

mysqldump -uroot -p123456 --all-databases  > all_mysql_backup.sql

未分类

4、清除mysql数据(清除之前务必先stop mysql服务):

未分类

systemctl stop mariadb
cp -r  /var/lib/mysql/ /var/lib/mysql.bak
rm -rf /var/lib/mysql/*

重启mysql服务:

未分类

正常模式在启动mysql:

vim /etc/my.cnf

注释配置项:

#innodb_force_recovery = 1

再重启:

systemctl restart mariadb

5、数据库恢复为以前密码123456:

mysqladmin -u root password 123456

未分类

6、使用之间备份的sql文件恢复数据:

mysql -uroot -p123456 -e "source /root/all_mysql_backup.sql"

未分类

查看恢复好的数据:

未分类

实验完成!!!

mysql(mariadb)新建用户及用户授权管理

仅新建一个newuser用户

方法一:

MariaDB [(none)]> create user newuser@localhost identified by '123456';
Query OK, 0 rows affected (0.22 sec)

MariaDB [(none)]> select user from mysql.user;
+---------+
| user    |
+---------+
| aa      |
| root    |
| root    |
|         |
| aa      |
| bb      |
| lcz     |
| my      |
| mytest  |
| newuser |
| nome    |
| root    |
|         |
| root    |
+---------+
14 rows in set (0.00 sec)

MariaDB [(none)]> 

方法二:

MariaDB [(none)]> insert into mysql.user(user,host,password) values('ggo','localhost',password('1234'));
Query OK, 1 row affected, 4 warnings (0.24 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.25 sec)

效果

[root@localhost ~]# mysql -uggo -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 4
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]> 

新建一个my用户并且授权全部操作权限

MariaDB [(none)]> grant all privileges on *.* to my@localhost identified by '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select user from mysql.user;
+--------+
| user   |
+--------+
| aa     |
| root   |
| root   |
|        |
| aa     |
| bb     |
| lcz    |
| my     |
| mytest |
| nome   |
| root   |
|        |
| root   |
+--------+
13 rows in set (0.14 sec)

MariaDB [(none)]>

查看用户权限

MariaDB [(none)]> show grants for my@localhost;
+--------------------------------------------------------------------------------------------------------------------+
| Grants for my@localhost                                                                                            |
+--------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'my'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
+--------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> 

指定部分授权

grant insert,update,delete,select on *.* to mytest@localhost;

centos7.3 系统MariaDB Galera Cluster多主集群搭建

1. 环境

CentOS Linux release 7.3
MariaDB 10.1.25

2. 安装MariaDB

配置mariadb10.1的yum源

[root@centos7-compute1 ~]# cat /etc/yum.repos.d/MariaDB.repo
[root@centos7-compute2 ~]# cat /etc/yum.repos.d/MariaDB.repo
[root@centos7-compute3 ~]# cat /etc/yum.repos.d/MariaDB.repo
MariaDB.repo:
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

3. 使用yum安装

[root@centos7-compute1 ~]# sudo yum install MariaDB-server MariaDB-client galera
[root@centos7-compute2 ~]# sudo yum install MariaDB-server MariaDB-client galera
[root@centos7-compute3 ~]# sudo yum install MariaDB-server MariaDB-client galera

4. 安全配置

[root@centos7-compute1 ~]# /usr/bin/mysql_secure_installation
[root@centos7-compute2 ~]# /usr/bin/mysql_secure_installation
[root@centos7-compute3 ~]# /usr/bin/mysql_secure_installation
compute1,compute2,compute3 三节点 启动MariaDB并赋权:
grant all privileges on *.* to root@"%" identified by "123456";
flush privileges;
然后全部节点关闭数据库
[root@centos7-compute1 ~]# systemctl stop mariadb
[root@centos7-compute2 ~]# systemctl stop mariadb
[root@centos7-compute3 ~]# systemctl stop mariadb
注意:此时需要全部节点关闭selinx,防火墙。防止接下来的影响集群通讯
 systemctl stop firewalld
systemctl disable firewalld

5. 配置MariaDB Galera Cluster

修改三台节点上的/etc/my.cnf.d/server.cnf 文件
compute1 配置如下:

[mysqld]

[galera]
wsrep_provider = /usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name='my_wsrep_cluster'
wsrep_cluster_address = "gcomm://192.168.140.197,192.168.140.141,192.168.140.192"
wsrep_node_name = centos7-compute1
wsrep_node_address=192.168.140.197
wsrep_on=ON
binlog_format=ROW
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
wsrep_slave_threads=1
innodb_flush_log_at_trx_commit=2
innodb_buffer_pool_size=1024M
wsrep_sst_method=rsync
wsrep_sst_auth=root:123456

[embedded]

[mariadb]

[mariadb-10.1]

compute2 配置如下:

[mysqld]

[galera]
wsrep_provider = /usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name='my_wsrep_cluster'
wsrep_cluster_address = "gcomm://192.168.140.197,192.168.140.141,192.168.140.192"
wsrep_node_name = centos7-compute2
wsrep_node_address=192.168.140.141
wsrep_on=ON
binlog_format=ROW
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
wsrep_slave_threads=1
innodb_flush_log_at_trx_commit=2
innodb_buffer_pool_size=1024M
wsrep_sst_method=rsync
wsrep_sst_auth=root:123456

[embedded]

[mariadb]

[mariadb-10.1]

compute3 配置如下:

[mysqld]

[galera]
wsrep_provider = /usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name='my_wsrep_cluster'
wsrep_cluster_address = "gcomm://192.168.140.197,192.168.140.141,192.168.140.192"
wsrep_node_name = centos7-compute3
wsrep_node_address=192.168.140.192
wsrep_on=ON
binlog_format=ROW
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
wsrep_slave_threads=1
innodb_flush_log_at_trx_commit=2
innodb_buffer_pool_size=1024M
wsrep_sst_method=rsync
wsrep_sst_auth=root:123456

[embedded]

[mariadb]

[mariadb-10.1]

6. 启动集群

引导集群创建
只在compute1操作:

[root@centos7-compute1 ~]# /usr/sbin/mysqld --wsrep-new-cluster --user=root &amp;

查看集群状态

MariaDB [(none)]> show  status like "wsrep_cluster_size";  
+--------------------+-------------+  
| Variable_name      | Value |  
+--------------------+-------------+  
| wsrep_cluster_size | 1     |  
+--------------------+-------------+  


MariaDB [(none)]> show  status like "wsrep%";  
+------------------------------+------------------------------------------------+  
| Variable_name                | Value                                          |  
+------------------------------+------------------------------------------------+  
| wsrep_apply_oooe             | 0.000000                                       |  
| wsrep_apply_oool             | 0.000000                                       |  
| wsrep_apply_window           | 0.000000                                       |  
| wsrep_causal_reads           | 0                                              |  
| wsrep_cert_deps_distance     | 0.000000                                       |  
| wsrep_cert_index_size        | 0                                              |  
| wsrep_cert_interval          | 0.000000                                       |  
| wsrep_cluster_conf_id        | 1                                              |  
| wsrep_cluster_size           | 1                                              |  
| wsrep_cluster_state_uuid     | 1e434901-71b5-11e7-b190-7bb2f4bbed7a           |  
| wsrep_cluster_status         | Primary                                        |  
| wsrep_commit_oooe            | 0.000000                                       |  
| wsrep_commit_oool            | 0.000000                                       |  
| wsrep_commit_window          | 0.000000                                       |  
| wsrep_connected              | ON                                             |  
| wsrep_desync_count           | 0                                              |  
| wsrep_evs_delayed            |                                                |  
| wsrep_evs_evict_list         |                                                |  
| wsrep_evs_repl_latency       | 5.592e-06/1.25208e-05/2.5685e-05/8.62896e-06/5 |  
| wsrep_evs_state              | OPERATIONAL                                    |  
| wsrep_flow_control_paused    | 0.000000                                       |  
| wsrep_flow_control_paused_ns | 0                                              |  
| wsrep_flow_control_recv      | 0                                              |  
| wsrep_flow_control_sent      | 0                                              |  
| wsrep_gcomm_uuid             | 35623b8e-71ad-11e7-af9f-52f25b42ebcf           |  
| wsrep_incoming_addresses     | 192.168.140.197:3306                           |  
| wsrep_last_committed         | 0                                              |  
| wsrep_local_bf_aborts        | 0                                              |  
| wsrep_local_cached_downto    | 18446744073709551615                           |  
| wsrep_local_cert_failures    | 0                                              |  
| wsrep_local_commits          | 0                                              |  
| wsrep_local_index            | 0                                              |  
| wsrep_local_recv_queue       | 0                                              |  
| wsrep_local_recv_queue_avg   | 0.500000                                       |  
| wsrep_local_recv_queue_max   | 2                                              |  
| wsrep_local_recv_queue_min   | 0                                              |  
| wsrep_local_replays          | 0                                              |  
| wsrep_local_send_queue       | 0                                              |  
| wsrep_local_send_queue_avg   | 0.000000                                       |  
| wsrep_local_send_queue_max   | 1                                              |  
| wsrep_local_send_queue_min   | 0                                              |  
| wsrep_local_state            | 4                                              |  
| wsrep_local_state_comment    | Synced                                         |  
| wsrep_local_state_uuid       | 1e434901-71b5-11e7-b190-7bb2f4bbed7a           |  
| wsrep_protocol_version       | 7                                              |  
| wsrep_provider_name          | Galera                                         |  
| wsrep_provider_vendor        | Codership Oy <[email protected]>              |  
| wsrep_provider_version       | 25.3.20(r3703)                                 |  
| wsrep_ready                  | ON                                             |  
| wsrep_received               | 2                                              |  
| wsrep_received_bytes         | 155                                            |  
| wsrep_repl_data_bytes        | 0                                              |  
| wsrep_repl_keys              | 0                                              |  
| wsrep_repl_keys_bytes        | 0                                              |  
| wsrep_repl_other_bytes       | 0                                              |  
| wsrep_replicated             | 0                                              |  
| wsrep_replicated_bytes       | 0                                              |  
| wsrep_thread_count           | 2                                              |  
+------------------------------+------------------------------------------------+

向集群中添加其他节点:

[root@centos7-compute2 ~]# systemctl start mariadb
[root@centos7-compute3 ~]# systemctl start mariadb

查看集群状态:

MariaDB [(none)]> show status like "wsrep_cluster_size";  
+--------------------+-------+  
| Variable_name      | Value |  
+--------------------+-------+  
| wsrep_cluster_size | 3     |  
+--------------------+-------+  
1 row in set (0.00 sec)  

7. 测试数据同步

在 compute1上创建数据库,表并插入数据,观察compute2,compute3 数据情况

MariaDB [(none)]> create database galera;
MariaDB [galera]> create table t (id int primary key);
insert into t value(1);
insert into t value(2);
insert into t value(3);

查看compute2,compute3数据库

MariaDB [galera]> show tables;  
+------------------+  
| Tables_in_galera |  
+------------------+  
| t                |  
+------------------+  
1 row in set (0.00 sec)  
MariaDB [galera]> select * from t;  
+----+  
| id |  
+----+  
|  1 |  
|  2 |  
|  3 |  
+----+

经过测试,删除数据库,增删查改表都可以实时同步。

8. 故障测试

[root@centos7-compute3 ~]# systemctl stop mariadb

然后在compute1,compute2 分别插入数据时,等待compute3启动后自己会同步

并且此时集群节点为两个。

MariaDB [galera]> SHOW STATUS LIKE 'wsrep_cluster_size';  
+--------------------+-------+  
| Variable_name      | Value |  
+--------------------+-------+  
| wsrep_cluster_size | 2     |  
+--------------------+-------+  

通过mariadb二进制日志实现数据库增量备份

何为增量备份,简单理解就是使用日志记录每天数据库的操作情况,只需要每天把这个日志里的数据库操作还原到数据库中,从而避免每天都进行完全备份,这种情况下,每周进行一次完全备份即可
首先我们需要配置以下mariadb的配置文件,我使用的是yum安装,其配置文件位于/etc/my.cnf,内容如下

[mysqld]
log-bin=mysql-bin                   #只需要增加这行就可以了
#binlog_format=row
#skip-grant
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

进入mariadb进行操作

[root@localhost mysql]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 4
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]> use bp
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [bp]> show tables;
+--------------+
| Tables_in_bp |
+--------------+
| mytest       |
| test         |
+--------------+
2 rows in set (0.00 sec)

MariaDB [bp]> create table bptest(id int ,name varchar(20));
Query OK, 0 rows affected (0.01 sec)

MariaDB [bp]> insert into bptest values(1,'a');
Query OK, 1 row affected (0.00 sec)

MariaDB [bp]> insert into bptest values(2,'b');
Query OK, 1 row affected (0.01 sec)

MariaDB [bp]> select * from bptest;
+------+------+
| id   | name |
+------+------+
|    1 | a    |
|    2 | b    |
+------+------+
2 rows in set (0.01 sec)

MariaDB [bp]> flush logs;                       #这里我还有点不明白,我是简单理解为日志的开始位置
Query OK, 0 rows affected (0.01 sec)

MariaDB [bp]> insert into bptest values(3,'c');
Query OK, 1 row affected (0.01 sec)

MariaDB [bp]> insert into bptest values(4,'d');
Query OK, 1 row affected (0.01 sec)

MariaDB [bp]> flush logs;                       #日志结束位置,该日志文件我们可以在/var/lib/mysql里面找到
Query OK, 0 rows affected (0.02 sec)

MariaDB [bp]> delete from bptest where id =3;
Query OK, 1 row affected (0.01 sec)

MariaDB [bp]> delete from bptest where id=1;
Query OK, 1 row affected (0.00 sec)

MariaDB [bp]> flush logs;
Query OK, 0 rows affected (0.02 sec)

MariaDB [bp]> truncate table bptest;#为了让效果更明显,我们直接清空表内容
Query OK, 0 rows affected (0.13 sec)

MariaDB [bp]> select * from bptest;
Empty set (0.00 sec)

我们可以进入/var/lib/mysql文件夹内查看,可以看到mysql-bin.000001,mysql-bin.000002文件
接下来我们来看一下日志文件内容

[root@localhost mysql]# mysqlbinlog mysql-bin.000001 
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#170725  2:04:19 server id 1  end_log_pos 245   Start: binlog v 4, server v 5.5.52-MariaDB created 170725  2:04:19
BINLOG '
kwl3WQ8BAAAA8QAAAPUAAAAAAAQANS41LjUyLU1hcmlhREIAbG9nAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAA2QAEGggAAAAICAgCAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAKUTwPA==
'/*!*/;
# at 245
#170725  2:04:51 server id 1  end_log_pos 311   Query   thread_id=4 exec_time=0 error_code=0
SET TIMESTAMP=1500973491/*!*/;
SET @@session.pseudo_thread_id=4/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=0/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN /*!*/;
# at 311
#170725  2:04:51 server id 1  end_log_pos 404   Query   thread_id=4 exec_time=0 error_code=0
use `bp`/*!*/;
SET TIMESTAMP=1500973491/*!*/;
insert into bptest values(3,'c') /*!*/;
# at 404
#170725  2:04:51 server id 1  end_log_pos 431   Xid = 47
COMMIT/*!*/;
# at 431
#170725  2:04:56 server id 1  end_log_pos 497   Query   thread_id=4 exec_time=0 error_code=0
SET TIMESTAMP=1500973496/*!*/;
BEGIN /*!*/;
# at 497
#170725  2:04:56 server id 1  end_log_pos 590   Query   thread_id=4 exec_time=0 error_code=0
SET TIMESTAMP=1500973496/*!*/;
insert into bptest values(4,'d') /*!*/;
# at 590
#170725  2:04:56 server id 1  end_log_pos 617   Xid = 48
COMMIT/*!*/;
# at 617
#170725  2:05:00 server id 1  end_log_pos 660   Rotate to mysql-bin.000002  pos: 4
DELIMITER ;
# End of log file ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@localhost mysql]#

在这个日志文件里面我们可以看到sql语句,且这些语句都位于mariadb操作里面的flush logs之间
现在我们就来进行备份的还原吧
现在我们使用mysql-bin.000001进行操作

[root@localhost mysql]# mysqlbinlog mysql-bin.000001|mysql -uroot -p
Enter password: 
[root@localhost mysql]# 

执行完毕,没有报错,我们再进数据库里面看看是否成功还原备份

MariaDB [bp]> select * from bptest;  #还原前
Empty set (0.00 sec)

MariaDB [bp]> select * from bptest;  #还原后
+------+------+
| id | name | +------+------+
|    3 | c    |
| 4 | d | +------+------+
2 rows in set (0.00 sec)

MariaDB [bp]>

MariaDB YUM安装及忘记密码解决方法

一、添加源

官方源

[mariadb] 
name = MariaDB 
baseurl = http://yum.mariadb.org/10.1/centos7-amd64 
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB 
gpgcheck=1

国内源

[mariadb]
name = MariaDB
baseurl = https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/10.1/centos7-amd64
gpgkey = https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum//RPM-GPG-KEY-MariaDB
gpgcheck = 1

yum-complete-transaction错误处理

$ yum install yum-utils
$ yum clean all
$ /usr/sbin/yum-complete-transaction --cleanup-only

安装

$ yum install MariaDB-server MariaDB-client MariaDB-devel

二、MariaDB的root密码忘记后的解决方法

编辑/usr/lib/systemd/system/mariadb.service文件,在Service段中添加

# 在Server段中的ExecStart出添加如下;
ExecStart=/usr/bin/mysqld_safe --basedir=/usr --skip-grant-tables --skip-networking

键入systemctl daemon-reload使其立即生效

$ systemctl daemon-reload

重新启动MariaDB服务

$ systemctl restart mariadb.service

完结。