Comprehensive guide to installing PXC on CentOS 7

Recently want to install Percona XtraDB Cluster + HAProxy + KeepAlived on CentOS 7, but could not find any all-in-one guide. So decided to write down all steps necessary for getting started and running HA solution using Open Source projects. Nowadays high availability is one of the main concerns faced by big and small companies.Minimum wasting of time (downtime per year), sustainably working of infrastructure and etc. is the main mission for all companies that they are trying to achieve.There are different approaches to this mission. One of them as large companies did, to buy expensive software and support from vendors. But small companies could not go through these steps. The true power of Open Source comes up at these moments.You can build your own High Availability solution using free but yet powerful Open Source projects. In this article we will show you how to achieve Database Clustering and Load Balancing. We will use:

  1. Percona XtraDB Cluster – Percona XtraDB Cluster is High Availability and Scalability solution for MySQL Users. What is PXC?

  2. HAproxy – HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications See for further information HAproxy official site

  3. KeepAlived – Keepalived is a routing software written in C. The main goal of this project is to provide simple and robust facilities for loadbalancing and high-availability to Linux system and Linux based infrastructures See for further information KeepAlived official site

General Architecture

Before diving in the step-by-step guide let’s to see what we want to accomplish at the end of tutorial: General Architecture Overview

As diagram shows there must be at least 3 CentOS 6.5/7 instance for PXC Galera Cluster setup. Total 3 CentOS 7 instance with all static local IPs and only 1 public IP for KeepAlived. So another advantage of this architecture that it will reduce public ip usage to 1.

{: .note } >

For this tutorial we will not use public IP as it is an test environment, all CentOSs will use local ips.

Prerequisites

We need 3 instances of CentOS 7 with minimal installation. Also you should give a static ips to all servers. Here is sample IPs that you may give to your CentOS instances for testing this tutorial:

  1. Node1 (1 GB RAM, 1 CPU VM) – 192.168.1.99 – (HAproxy1)
  2. Node2 (1 GB RAM, 1 CPU VM) – 192.168.1.88 – (HAproxy2)
  3. Node3 (1 GB RAM, 1 CPU VM) – 192.168.1.77 – (HAproxy3)

Also keep in mind that we need another IP for KeepAlived that will act as Virtual IP for HAproxy intances.

Installing and Configuring PXC

Installing and Running PXC:

The easy way to install is through official yum repo. You should activate Percona repo on all 3 Nodes:

    -- CentOS 7
    yum install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm

Save all 3 files on all 3 Nodes. Then run:

    [root@node1, 2, 3 ~]# yum install Percona-XtraDB-Cluster-56

All needed packages and dependencies will be resolved automatically and at this point you should be able to start newly installed PXCs:

    -- CentOS 7
    [root@node1, 2, 3 ~]# systemctl start mysql.service
    [root@node1, 2, 3 ~]# mysql_secure_installation

At this point, we have 3 running Percona Galera Cluster instances.

Installing Several other needed packages:

We need several other packages from epel repo, such as rsync for PXC SST See Documentation That’s why we must activate epel repo on all 3 Nodes:

    -- CentOS 7
    [root@node1, 2, 3 ~]# yum install socat rsync

Create binary log folder outside MySQL’s datadir

    [root@node1, 2, 3 ~]# mkdir /var/lib/mysql-binlog
    [root@node1, 2, 3 ~]# chown mysql:mysql /var/lib/mysql-binlog

    -- SElinux
    [root@centos7-node1 ~]# yum install policycoreutils-python
    [root@centos7-node1 ~]# semanage fcontext -a -t mysqld_db_t /var/lib/mysql-binlog
    [root@centos7-node1 ~]# restorecon -v /var/lib/mysql-binlog
        restorecon reset /var/lib/mysql-binlog context unconfined_u:object_r:var_lib_t:s0->unconfined_u:object_r:mysqld_db_t:s0

server.cnf configuration file for PXC nodes:

Now we must edit all 3 nodes server.cnf (default location is ‘/etc/my.cnf.d/server.cnf’) file to reflect our needs. Following content is the same on all 3 nodes:

    [mysqld]

    # General #
    datadir                       = /var/lib/mysql
    socket                        = /var/lib/mysql/mysql.sock
    default_storage_engine        = InnoDB
    lower_case_table_names        = 1


    # MyISAM #
    key_buffer_size                = 8M
    myisam_recover                 = FORCE,BACKUP


    # SAFETY #
    max_allowed_packet             = 16M
    max_connect_errors             = 1000000
    skip_name_resolve
    sysdate_is_now                 = 1
    innodb                         = FORCE
    thread_stack                   = 262144
    back_log                       = 2048
    performance_schema             = OFF
    skip_show_database

    # Binary Logging #
    log_bin                        = /var/lib/mysql-binlog/mysql-bin
    log_bin_index                  = /var/lib/mysql-binlog/mysql-bin
    expire_logs_days               = 14
    sync_binlog                    = 1
    binlog_format                  = row

    # CACHES AND LIMITS #
    tmp_table_size                 = 16M
    max_heap_table_size            = 16M
    query_cache_type               = 0
    query_cache_size               = 0
    query_cache_limit              = 0
    max_connections                = 10000
    thread_cache_size              = 500
    open-files-limit               = 65535
    table_definition_cache         = 7000
    table_open_cache               = 7000

    # InnoDB Related #

    innodb_log_files_in_group      = 2
    innodb_autoinc_lock_mode       =2
    #innodb_locks_unsafe_for_binlog =1
    innodb_log_file_size           =100M
    innodb_file_per_table
    innodb_flush_log_at_trx_commit =2
    innodb_buffer_pool_size        =150M

And now we provide per Node settings for configuration file:

    **Node1**

    #
    # * Galera-related settings
    #
    [galera]

    wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
    wsrep_cluster_address="gcomm://192.168.1.99,192.168.1.88,192.168.1.77"
    wsrep_cluster_name='TestingCluster'
    wsrep_node_address='192.168.1.99'
    wsrep_node_name='node1'
    wsrep_sst_method=rsync
    wsrep_sst_auth=sstuser:12345
    bind-address=0.0.0.0


    **Node2**

    #
    # * Galera-related settings
    #
    [galera]

    wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
    wsrep_cluster_address="gcomm://192.168.1.99,192.168.1.88,192.168.1.77"
    wsrep_cluster_name='TestingCluster'
    wsrep_node_address='192.168.1.88'
    wsrep_node_name='node1'
    wsrep_sst_method=rsync
    wsrep_sst_auth=sstuser:12345
    bind-address=0.0.0.0


    **Node3**

    #
    # * Galera-related settings
    #
    [galera]

    wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
    wsrep_cluster_address="gcomm://192.168.1.99,192.168.1.88,192.168.1.77"
    wsrep_cluster_name='TestingCluster'
    wsrep_node_address='192.168.1.77'
    wsrep_node_name='node1'
    wsrep_sst_method=rsync
    wsrep_sst_auth=sstuser:12345
    bind-address=0.0.0.0

Save file on all 3 nodes and exit file editing.

Afer editing configuration files. On all 3 Nodes you must create wsrep_sst_auth user as mentioned in server.cnf file its name is ‘sstuser’

    [root@node1, 2, 3 ~]# mysql -u root -p
    mysql> create user 'sstuser'@'%' identified by '12345';
    Query OK, 0 rows affected (0.01 sec)

    mysql> grant all on *.* to 'sstuser'@'%';
    Query OK, 0 rows affected (0.00 sec)

Set to permissive mode on all 3 Nodes. As SElinux prevents Galera to start:

    -- CentOS  7
    [root@node1, 2, 3 ~]# setenforce 0

‘/etc/hosts’ file contents for PXC Nodes:

**Node1**

    [root@node1 ~]# cat /etc/hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    127.0.0.1   node1.localdomain node1
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

    192.168.1.99 localhost localhost.localdomain node1.localdomain node1
    192.168.1.88 node2
    192.168.1.77 node3




**Node2**

    [root@node2 ~]# cat /etc/hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    127.0.0.1   node2.localdomain node2
    192.168.1.88 localhost localhost.localdomain node2.localdomain node2
    192.168.1.99 node1
    192.168.1.77 node3



**Node3**

    [root@node3 ~]# cat /etc/hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    127.0.0.1   node3.localdomain node3
    192.168.1.77 localhost localhost.localdomain node3
    192.168.1.99 node1
    192.168.1.88 node2

Iptables settings related to PXC nodes:

-- CentOS 7

** Node1 **

firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.88" port port="4567" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.88" port port="3306" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.77" port port="4567" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.88" port port="4444" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.77" port port="3306" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.77" port port="4444" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.77" port port="4568" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.88" port port="4568" protocol="tcp" accept' --permanent

firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.88" port port="9200" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.77" port port="9200" protocol="tcp" accept' --permanent

firewall-cmd --reload

** Node 2 **

firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.99" port port="4567" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.99" port port="3306" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.77" port port="4567" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.99" port port="4444" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.77" port port="3306" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.77" port port="4444" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.77" port port="4568" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.99" port port="4568" protocol="tcp" accept' --permanent

firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.99" port port="9200" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.77" port port="9200" protocol="tcp" accept' --permanent

firewall-cmd --reload

** Node 3 **

firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.88" port port="4567" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.88" port port="3306" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.99" port port="4567" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.88" port port="4444" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.99" port port="3306" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.99" port port="4444" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.99" port port="4568" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.88" port port="4568" protocol="tcp" accept' --permanent

firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.88" port port="9200" protocol="tcp" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.99" port port="9200" protocol="tcp" accept' --permanent
firewall-cmd --reload

Bootstrapping(starting) Cluster

As we have 3 Nodes, one of them must be in some words, the “Head” node, at first time we must choose one of Nodes as start point. For us this is Node1. Stop Node1 and run following command:

    -- CentOS 7

    [root@node1 ~]# systemctl stop mysql.service
    [root@node1 ~]# systemctl start mysql@bootstrap.service
    Bootstrapping the clusterStarting MySQL.. SUCCESS!

    (There is no bootstrap command for CentOS 7 for MariaDB cluster - already reported as: [MDEV-7752](https://mariadb.atlassian.net/browse/MDEV-7752))

Then you must start other 2 nodes as usual:

    -- CentOS 7

    [root@node2 ~]# systemctl start mysql.service
    [root@node3 ~]# systemctl start mysql.service

After success messages login to PXC Node1 and run:

    PXC [(none)]> show status like 'wsrep%';

The important part of output is:

    | wsrep_cluster_conf_id        | 5                                                     |
    | wsrep_cluster_size           | 3                                                     |
    | wsrep_connected              | ON                                                    |
    | wsrep_ready                  | ON                                                    |
    | wsrep_cluster_status         | Primary                                               |

As you see the cluster size is 3 and it means all 3 Nodes connected and working. Also you can see from output that Cluster is ready for accepting connection. Let’s create database from Node3 and test its creation from other Nodes:

    [root@node3 ~]# mysql -u root -p
    PXC [(none)]> create database pxc_test;
    Query OK, 1 row affected (0.10 sec)

    [root@node2 ~]# mysql -u root -p
    PXC [(none)]> show databases like 'pxc%';
    +-----------------+
    | Database (lin%) |
    +-----------------+
    | pxc_test        |
    +-----------------+
    1 row in set (0.00 sec)

    [root@node1 ~]# mysql -u root -p
    PXC [(none)]> show databases like 'pxc%';
    +-----------------+
    | Database (lin%) |
    +-----------------+
    | pxc_test        |
    +-----------------+
    1 row in set (0.00 sec)

Enabling SElinux :

Disabling SElinux permanently or running in permissive mode for all time is dangerous related to security. As we have set SElinux into permissive mode, it will not prevent any Galera actions, instead it has already logged all related information into audit.log file. Using this file we should create rules and reenable SElinux. On all 3 nodes:

-- CentOS 7
[root@node1, 2, 3 ~]# yum install policycoreutils-python
[root@node1, 2, 3 ~]# grep mysql /var/log/audit/audit.log | audit2allow -M galera
[root@node1, 2, 3 ~]# semodule -i galera.pp

[root@node1, 2, 3 ~]# setenforce 1

Configuring Clustercheck script for cluster health check

This script will be installed with PXC(/usr/bin/clustercheck). Clustercheck is simple script o make a proxy (i.e HAProxy) capable of monitoring Percona XtraDB Cluster nodes properly.

Before setup install xinetd in all 3 nodes:

    -- CentOS 7

    [root@node1, 2, 3 ~]# yum install xinetd
    [root@node1, 2, 3 ~]# systemctl start xinetd.service
    [root@node1, 2, 3 ~]# systemctl enable xinetd.service

After installing Xinetd and ensuring that it will start on system reboot automatically, we need configure mysqlchk file:

    [root@node1, 2, 3 ~]# nano /etc/xinetd.d/mysqlchk

    -- Add following lines to this file

    service mysqlchk
    {
        disable = no
        flags = REUSE
        socket_type = stream
        port = 9200
        wait = no
        user = nobody
        server = /usr/bin/clustercheck
        log_on_failure += USERID
        only_from = 0.0.0.0/0
        per_source = UNLIMITED
    }

Then edit /etc/services file. Locate wap-wsp word in this file and comment out as follows, then add mysqlchk entry:

    [root@node1, 2, 3 ~]# nano /etc/services
    mysqlchk        9200/tcp                # SERVICE for Checking PXC Cluster      
    #wap-wsp         9200/tcp                # WAP connectionless session service
    #wap-wsp         9200/udp                # WAP connectionless session service

Now create clustercheck database user on ALL Nodes:

    create user 'clustercheckuser'@'localhost' identified by 'clustercheckpassword!';
    grant process on *.* to 'clustercheckuser'@'localhost';

Restart Xinetd on all Nodes:

    -- CentOS 7

    [root@node1, 2, 3 ~]# systemctl restart xinetd

And final step for this section is to check this new script:

    [root@node1, 2, 3 ~]# clustercheck 
    HTTP/1.1 200 OK
    Content-Type: text/plain
    Connection: close
    Content-Length: 40

    Percona XtraDB Cluster Node is synced.

As you see everything is OK and it works.

Installing and Configuring HAproxy

As we have mentioned we have not got VMs dedicated to HAproxy. That’s why HAproxy instances will be in same machines as PXCs.

Install HAproxy:

    [root@node1, 2, 3 ~]# yum install haproxy
    [root@node1, 2, 3 ~]# systemctl enable haproxy.service

Fix HAproxy logging: By default HAproxy does not log anything, we must fix it by adding haproxy.conf file into /etc/rsyslog.d directory:

    [root@node1, 2, 3 ~]# cd /etc/rsyslog.d/
    -- Add following lines into file:
    [root@node1, 2, 3 rsyslog.d]# nano haproxy.conf
    $ModLoad imudp
    $UDPServerRun 514
    $template Haproxy,"%msg%n"
    local0.=info -/var/log/haproxy.log;Haproxy
    local0.notice -/var/log/haproxy-status.log;Haproxy
    local0.* ~

    -- SElinux related command:
    [root@node1, 2, 3 ~]# /sbin/restorecon -v -F /etc/rsyslog.d/haproxy.conf
    [root@node1, 2, 3 ~]# systemctl restart rsyslog.service

Edit haproxy.cfg config file:

    **HAproxy1, 2, 3**

    [root@node1, 2, 3 ~]# cd /etc/haproxy/
    [root@node1, 2, 3 ~]# mv haproxy.cfg haproxy.cfg.orig
    [root@node1, 2, 3 ~]# nano haproxy.cfg

    global
        log         127.0.0.1   local0
        log         127.0.0.1   local1 notice
        maxconn     4096
        user        haproxy
        group       haproxy
        nbproc      1
        pidfile     /var/run/haproxy.pid

    defaults
        log         global
        option      tcplog
        option      dontlognull
        retries     3
        maxconn     4096
        option      redispatch
        timeout     connect 50000ms
        timeout     client  50000ms
        timeout     server  50000ms

    listen PXC-writes
        bind 0.0.0.0:3311
        mode tcp
        #    option mysql-check user haproxy
        option httpchk 
        server node1 192.168.1.99:3306 check port 9200
        server node2 192.168.1.88:3306 check port 9200 backup
        server node3 192.168.1.77:3306 check port 9200 backup

    listen PXC-reads
        bind 0.0.0.0:3312
        mode tcp
        balance leastconn
    #    option mysql-check user haproxy
        option httpchk
        server node1 192.168.1.99:3306 check port 9200
        server node2 192.168.1.88:3306 check port 9200
        server node3 192.168.1.77:3306 check port 9200

    # HAProxy web ui
        listen stats 0.0.0.0:9000
        mode http
        stats enable
        stats uri /haproxy
        stats realm HAProxy Statistics
        stats auth haproxy:haproxy
        stats admin if TRUE


    -- SElinux related command:

    [root@node1 ~]# /sbin/restorecon -v -F /etc/haproxy/haproxy.cfg
    [root@node1 ~]# setsebool haproxy_connect_any on
    [root@node1 ~]# systemctl start haproxy.service

Lets explore config file a bit more: The default port number 9000 is for WEB UI for HAproxy monitoring. Another thing to remember that when using PXC Galera Cluster with SST options mysqldump and rsync (default) it will lock each other nodes from getting updates while DDL, DML statements executing. To avoid such situation and not to stuck with Deadlocks, we decide to separate Write operations. In other words, Write operations (e.g insert, update, delete etc.) will go only to Node1. So on the application side, you should send write operations to port 3310 as we put in haproxy.cfg file, and for read operations to port number 3311. There is an available non-locking SST option XtraBackup (the famous hot online backup tool for MySQL), but it is the subject of another topic.

And ofcourse we should check our work:

Check for listening ports:

    [root@node1 ~]# netstat -ntpl | grep haproxy
    tcp        0      0 0.0.0.0:9000                0.0.0.0:*                   LISTEN      11902/haproxy       
    tcp        0      0 0.0.0.0:3310                0.0.0.0:*                   LISTEN      11902/haproxy       
    tcp        0      0 0.0.0.0:3311                0.0.0.0:*                   LISTEN      11902/haproxy

    [root@node2 haproxy]# netstat -ntpl | grep haproxy
    tcp        0      0 0.0.0.0:9000                0.0.0.0:*                   LISTEN      12846/haproxy       
    tcp        0      0 0.0.0.0:3310                0.0.0.0:*                   LISTEN      12846/haproxy       
    tcp        0      0 0.0.0.0:3311                0.0.0.0:*                   LISTEN      12846/haproxy

As we have mentioned port number 9000 for WEB Ui related to HAproxy statistics. So lets connect to IP of on of the HAproxy servers. The connection URL for me is: http://192.168.1.88:9000/haproxy_stats. In Very first connection there will be an pop-screen for login and password. These information is stored in /etc/haproxy/haproxy.cfg file stats auth haproxy:haproxy. So the Login is: haproxy and Password is: haproxy too.

You will see the statistics page with correctly working Cluster Nodes:

HAproxy Web Statistics Page

Now lets check MySQL connectivity over HAproxy. For this purpose create sample database user on PXC Cluster nodes. Our cluster is ready and that’s why it is sufficient to run this command one of the nodes and other will pick-up automatically

    create user 'all'@'%' identified by '12345';
    grant all on *.* to 'all'@'%';

Before doing, open these ports:

    [root@node1, 2, 3 ~]# firewall-cmd --add-port=3311/tcp --permanent
    success
    [root@node1, 2, 3 ~]# firewall-cmd --add-port=3312/tcp --permanent
    success
    [root@node1, 2, 3 ~]# firewall-cmd --reload
    success

Our testing result should be something like:

    -- make connection through HAproxy2(node2) server to port 3311 (our write node)

    sh@sh--work:~$ mysql -u all -p --host=192.168.1.88 --port=3311 -e "select @@version, @@hostname";
    Enter password: 
    +--------------------+---------------+
    | @@version          | @@hostname    |
    +--------------------+---------------+
    | 5.6.24-72.2-56-log | node1 |
    +--------------------+---------------+


    -- Second to port 3312 (one of our read nodes)

    sh@sh--work:~$ mysql -u all -p --host=192.168.1.88 --port=3312 -e "select @@version, @@hostname";
    Enter password: 
    +--------------------+---------------+
    | @@version          | @@hostname    |
    +--------------------+---------------+
    | 5.6.24-72.2-56-log | node1         |
    +--------------------+---------------+
    sh@sh--work:~$ mysql -u all -p --host=192.168.1.88 --port=3312 -e "select @@version, @@hostname";
    Enter password: 
    +--------------------+---------------+
    | @@version          | @@hostname    |
    +--------------------+---------------+
    | 5.6.24-72.2-56-log | node2         |
    +--------------------+---------------+
    sh@sh--work:~$ mysql -u all -p --host=192.168.1.88 --port=3312 -e "select @@version, @@hostname";
    Enter password: 
    +--------------------+---------------+
    | @@version          | @@hostname    |
    +--------------------+---------------+
    | 5.6.24-72.2-56-log | node3         |
    +--------------------+---------------+

Installing and Configuring KeepAlived

The Final step in our environment is installing and configuring KeepAlived , acting as Virtual IP and switch-over between HAproxy instances. The idea is, when one of HAproxy servers fails, another one must do same work without interrupting all architecture. The new IP address for us is 192.168.1.199 – which is Virtual IP for our HAproxy servers. We will connect to MySQL(PXC) through this IP and KeepAlived will decide to which HAproxy instance it must send this connection. Lets test:

    -- Pay Attention to host IP address

    sh@sh--work:~$ mysql -u all -p --host=192.168.1.199 --port=3311 -e "select @@version, @@hostname";
    Enter password: 
    +--------------------+---------------+
    | @@version          | @@hostname    |
    +--------------------+---------------+
    | 5.6.24-72.2-56-log | node1         |
    +--------------------+---------------+


    sh@sh--work:~$ mysql -u all -p --host=192.168.1.199 --port=3312 -e "select @@version, @@hostname";
    Enter password: 
    +--------------------+---------------+
    | @@version          | @@hostname    |
    +--------------------+---------------+
    | 5.6.24-72.2-56-log | node2         |
    +--------------------+---------------+

We send connection to KeepAlived virtual IP and it passes to one of HAproxy servers. Enough theory lets achieve our goal.

Installing/Configuring

    **HAproxy1**

    [root@node1, 2, 3 ~]# yum install keepalived
    [root@node1, 2, 3 ~]# systemctl enable keepalived.service

    [root@node1, 2, 3 ~]# cd /etc/keepalived/
    [root@node1, 2, 3 ~]# mv keepalived.conf keepalived.conf.orig
    [root@node1, 2, 3 ~]# nano keepalived.conf
    -- Add following lines to file        
    vrrp_script chk_haproxy {
        script "killall -0 haproxy" # verify the pid is exist or not
        interval 2                      # check every 2 seconds
        weight 2                        # add 2 points of prio if OK
    }

    vrrp_instance VI_1 {
        interface eth0                  # interface to monitor
        state MASTER
        virtual_router_id 51            # Assign one ID for this route
        priority 101                    # 101 on master, 100 on backup1, 99 on backup2
        authentication {
                 auth_type PASS
                 auth_pass 12345
        }
    virtual_ipaddress {
        192.168.1.199/24                # the virtual IP
    }
track_script {
        chk_haproxy
    }
    }

    -- Save and exit file editing.

    [root@node1, 2, 3 ~]# /sbin/restorecon -v -F /etc/keepalived/keepalived.conf

Starting KeepAlived

    [root@node1, 2, 3 ~]# systemctl start keepalived.service

Testing MySQL connection with KeepAlived

    sh@sh--work:~$ mysql -u all -p --host=192.168.1.199 --port=3311 -e "select @@version, @@hostname";
    Enter password: 
    +---------------------------+------------+
    | @@version                 | @@hostname |
    +---------------------------+------------+
    | 10.0.15-PXC-wsrep-log | node1      |
    +---------------------------+------------+


    sh@sh--work:~$ mysql -u all -p --host=192.168.1.199 --port=3312 -e "select @@version, @@hostname";
    Enter password: 
    +---------------------------+------------+
    | @@version                 | @@hostname |
    +---------------------------+------------+
    | 10.0.15-PXC-wsrep-log | node2      |
    +---------------------------+------------+

Conclusion

In this tutorial you learn how to install and configure PXC on CentOS 7 and also how to load balance this cluster using wellknown HAproxy. As bonus you learn to install/configure KeepAlived and access database over Virtual IP which is a next level of redundancy in High Availability approach. If you have any suggesstions or errors, please do not hesitate to contact. Thank you for reading.

MySQL Optimizer Tracer usage case with count(*)

What is Optimizer Trace? After reading topic about Optimizer Tracer by [Morgan Tocker][1] decided to test it. From [Optimizer Trace and EXPLAIN FORMAT=JSON in 5.7][2]: Optimizer trace is a new diagnostic tool introduced in MySQL 5.6 to show how the optimizer is working internally. It is similar to EXPLAIN, with a few notable differences: It doesn’t just show the intended execution plan, it shows the alternative choices. You enable the optimizer trace, then you run the actual query. It is far more verbose in its output. For understanding goal of article please read previous one about related verified optimizer BUG: [Playing with count() optimizer work][3] ** We have 2 queries: **select count() from sales; select count(*) from sales where sales_id > 0; Firstly let’s get explain plan for query with JSON format and as regular:

       -- JSON 
          mysql> explain format=json select count(*) from sales; |
          { "query_block": 
          { "select_id": 1, 
          "table": { "table_name": "sales", 
                     "access_type":  "index", 
                     "key": "sales_cust_idx", 
                     "used_key_parts": [ "CUSTOMER_ID" ] 
                    /*  used_key_parts */, 
                    "key_length": "4", 
                    "rows": 2489938, 
                    "filtered": 100,
                     "using_index": true }  
            /* table */ } 
           /* query_block */ } 

        mysql> explain select count(*) from salesG 
        *************************** 1. row *************************** 
        id: 1 
        select_type: SIMPLE 
        table: sales 
        type: index 
        possible_keys: NULL 
        key: sales_cust_idx 
        key_len: 4 
        ref: NULL 
        rows: 2489938 
        Extra: Using index 1 row in set (0.00 sec) 

Second query:

       -- JSON  
       mysql> explain format=json select count(*) from sales where sales_id > 0G         *************************** 1. row *************************** 
    EXPLAIN: 
    { "query_block": 
    { "select_id": 1, 
    "table": 
    { "table_name": "sales", 
      "access_type": "range",
      "possible_keys": [ "PRIMARY" ], "key": "PRIMARY", 
      "used_key_parts": [ "SALES_ID" ], "key_length": "4", 
      "rows": 1244969, 
      "filtered": 100, 
      "using_index": true, 
     "attached_condition": "(`sales`.`sales`.`SALES_ID` > 0)" } } } 


     mysql> explain select count(*) from sales where sales_id > 0G 
     *************************** 1. row *************************** 
     id: 1 
     select_type: SIMPLE 
     table: sales 
     type: range 
     possible_keys: PRIMARY 
     key: PRIMARY 
     key_len: 4 
     ref: NULL 
     rows: 1244969 
     Extra: Using where; Using index 1 row in set (0.00 sec)  

From Explain plan it is obvious that, first query will use “Index Scan”, second will use “Range Scan + Index”. First query will use, sales_cust_idx in customer_id column, second query will use primary key in sales_id column. From first view, there now difference between queries, but optimizer estimates half of rows when attaching sales_id > 0 condition. See related BUG: [#68814][4] Now let’s examine problem with Optimizer Tracer. So before running query you should enable optimizer trace:

      SET OPTIMIZER_TRACE="enabled=on",END_MARKERS_IN_JSON=on;
      SET OPTIMIZER_TRACE_MAX_MEM_SIZE=1000000; 

After run first query:

      mysql> select count(*) from sales; 
      +----------+ 
      | count(*) |
      +----------+
      | 2500003 |
      +----------+ 
     1 row in set (0.58 sec) 

Query to OPTIMIZER_TRACE table from information_schema:

     mysql> select query, trace from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
     select count(*) from sales | 
    { "steps": 
   [ { "join_preparation": 
    { "select#": 1, 
    "steps": [ { "expanded_query": "/* select#1 */ select count(0) AS `count(*)` from `sales`" } ] /* steps */ }
    /* join_preparation */ },
    { "join_optimization": 
       { "select#": 1, 
         "steps": [ { "table_dependencies": [ { "table": "`sales`", "row_may_be_null": false, "map_bit": 0, "depends_on_map_bits": [ ] 
    /* depends_on_map_bits */ } ] 
    /* table_dependencies */ }, 
      { "rows_estimation": 
           [ { "table": "`sales`", "table_scan": { "rows": 2489938, "cost": 10347 }
          /* table_scan */ } ] 
          /* rows_estimation */ }, 
           { "considered_execution_plans": [ { "plan_prefix": [ ] 
           /* plan_prefix */,
           "table": "`sales`", 
           "best_access_path": { "considered_access_paths": [ { "access_type": "scan", "rows": 2.49e6, "cost": 508335, "chosen": true } ] 
          /* considered_access_paths */ } 
          /* best_access_path */, 
         "cost_for_plan": 508335,
         "rows_for_plan": 2.49e6,
         "chosen": true } ]
         /* considered_execution_plans */ },
        { "attaching_conditions_to_tables": { "original_condition": null,  "attached_conditions_computation": [ ] 
        /* attached_conditions_computation */,
       "attached_conditions_summary": [ { "table": "`sales`", "attached": null } ] 
        /* attached_conditions_summary */ } 
        /* attaching_conditions_to_tables */ },
       { "refine_plan": [ { "table": "`sales`", "access_type": "index_scan" } ] 
        /* refine_plan */ } ] 
        /* steps */ } 
        /* join_optimization */ },
        { "join_execution": { "select#": 1, "steps": [ ] 
        /* steps */ } 
        /* join_execution */ } ] 
        /* steps */ 

Interesting part for query 1 is -> “cost_for_plan”: 508335, “rows_for_plan”: 2.49e6, “chosen”: true Cost is 508335, rows for plan is 2.49e6 = 2490000 rows, is roughly equal to explain plan estimation.
Now second query:

     mysql> select count(*) from sales where sales_id > 0;
     +----------+ 
     | count(*) |
     +----------+ 
     | 2500003 |
     +----------+ 1 row in set (1.18 sec) 

Query to OPTIMIZER_TRACE:

     mysql> select query, trace from INFORMATION_SCHEMA.OPTIMIZER_TRACE; 
     select count(*) from sales where sales_id > 0 | 
     { "steps": [ { "join_preparation": 
        { "select#": 1, 
          "steps": [ { "expanded_query": "/* select#1 */ select count(0) AS `count(*)` from `sales` where (`sales`.`SALES_ID` > 0)" } ] 
        /* steps */ } 
        /* join_preparation */ },
       { "join_optimization": 
          { "select#": 1, 
            "steps": [ { "condition_processing": { "condition": "WHERE", "original_condition": "(`sales`.`SALES_ID` > 0)", "steps": [ { "transformation": "equality_propagation", 
           "resulting_condition": "(`sales`.`SALES_ID` > 0)" }, 
          { "transformation": "constant_propagation", "resulting_condition": "(`sales`.`SALES_ID` > 0)" }, 
          { "transformation": "trivial_condition_removal", "resulting_condition": "(`sales`.`SALES_ID` > 0)" } ] 
         /* steps */ } 
         /* condition_processing */ }, 
        { "table_dependencies": [ { "table": "`sales`", "row_may_be_null": false, "map_bit": 0, "depends_on_map_bits": [ ] 
        /* depends_on_map_bits */ } ] 
        /* table_dependencies */ }, 
       { "ref_optimizer_key_uses": [ ] 
        /* ref_optimizer_key_uses */ },
       { "rows_estimation": [ { "table": "`sales`", "range_analysis": { "table_scan": {  "rows": 2489938, "cost": 508337 } 
       /* table_scan */, 
       "potential_range_indices": [ { "index": "PRIMARY", "usable": true, "key_parts": [ "SALES_ID" ] 
      /* key_parts */ }, 
      { "index": "sales_cust_idx", "usable": false, "cause": "not_applicable" } ] 
      /* potential_range_indices */,
     "best_covering_index_scan": { "index": "sales_cust_idx", "cost": 500418, "chosen": true } 
      /* best_covering_index_scan */,
      "setup_range_conditions": [ ] 
      /* setup_range_conditions */, 
      "group_index_range": { "chosen": false, "cause": "not_group_by_or_distinct" } 
      /* group_index_range */, 
      "analyzing_range_alternatives": { "range_scan_alternatives": [ { "index": "PRIMARY", "ranges": [ "0 < SALES_ID" ] 
     /* ranges */, 
     "index_dives_for_eq_ranges": true, 
     "rowid_ordered": true, 
     "using_mrr": false, "index_only": true, "rows": 1244969, "cost": 251364, "chosen": true } ] 
    /* range_scan_alternatives */,
   "analyzing_roworder_intersect": { "usable": false, "cause": "too_few_roworder_scans" }  /* analyzing_roworder_intersect */ } 
    /* analyzing_range_alternatives */,
   "chosen_range_access_summary": { "range_access_plan": { "type": "range_scan", "index": "PRIMARY", "rows": 1244969, "ranges": [ "0 < SALES_ID" ] 
   /* ranges */ } 
   /* range_access_plan */, 
   "rows_for_plan": 1244969, "cost_for_plan": 251364, "chosen": true } 
   /* chosen_range_access_summary */ } 
   /* range_analysis */ } ] 
   /* rows_estimation */ },
    { "considered_execution_plans": [ { "plan_prefix": [ ] /* plan_prefix */, "table": "`sales`", "best_access_path": { "considered_access_paths": [ { "access_type": "range", "rows": 1.24e6, "cost": 500357, "chosen": true } ] 
    /* considered_access_paths */ } 
    /* best_access_path */, 
    "cost_for_plan": 500357, "rows_for_plan": 1.24e6, "chosen": true } ] 
    /* considered_execution_plans */ },
    { "attaching_conditions_to_tables": { "original_condition": "(`sales`.`SALES_ID` > 0)", "attached_conditions_computation": [ ] 
   /* attached_conditions_computation */,
   "attached_conditions_summary": [ { "table": "`sales`", "attached": "(`sales`.`SALES_ID` > 0)" } ] 
   /* attached_conditions_summary */ } 
   /* attaching_conditions_to_tables */ }, 
   { "refine_plan": [ { "table": "`sales`", "access_type": "range" } ] /* refine_plan */ } ] 
   /* steps */ } 
   /* join_optimization */ }, 
  { "join_execution": { "select#": 1, "steps": [ ] /* steps */ } /* join_execution */ } ] /* steps */ 

It is much more complicated with second query and due to lacking documentation for all output, i am looking for explanations from experts.
First thing is, it says in “potential_range_indices” that “index”: “sales_cust_index” is not usable:

      "potential_range_indices": 
      [ { "index": "PRIMARY", "usable": true, "key_parts": [ "SALES_ID" ] 
      /* key_parts */ },
      { "index": "sales_cust_idx", "usable": false, "cause": "not_applicable" } ] 

But in “best_covering_index_scan”, “index”: “sales_cust_idx” is marked as “chosen”:true

       "best_covering_index_scan": { "index": "sales_cust_idx", "cost": 500418, "chosen": true } 

Second thing is, in “range_scan_alternatives” and,

        "analyzing_range_alternatives": 
        { "range_scan_alternatives": [ { "index": "PRIMARY", "ranges": [ "0 < SALES_ID" ]  
        /* ranges */, 
       "index_dives_for_eq_ranges": true, "rowid_ordered": true, "using_mrr": false, "index_only": true, "rows": 1244969, "cost": 251364, "chosen": true } 

in “chosen_range_access_summary”, “rows_for_plan” is 1244969 and “cost_for_plan” is 251364

       "chosen_range_access_summary": 
       { "range_access_plan": 
        { "type": "range_scan", "index": "PRIMARY", "rows": 1244969, "ranges": [ "0 < SALES_ID" ] 
         /* ranges */ } 
         /* range_access_plan */, 
        "rows_for_plan": 1244969, "cost_for_plan": 251364, "chosen": true } 

But for final “best_access_path” “cost_for_plan” is increased to 500357 and “rows_for_plan” is 1.24e6 = 1240000:

       "best_access_path": { "considered_access_paths": [ { "access_type": "range", "rows": 1.24e6, "cost": 500357, "chosen": true } ] 
       /* considered_access_paths */ } 
       /* best_access_path */, 
       "cost_for_plan": 500357, "rows_for_plan": 1.24e6, "chosen": true }

Third thing is that, sales_id > 0 is rewritten to 0 < sales_id

      "ranges": [ "0 < SALES_ID" ] 

*** After explanations from community this article will be updated ***

http://www.tocker.ca/
http://www.tocker.ca/2015/05/25/optimizer-trace-and-explain-formatjson-in-5-7.html
https://mysql.az/playing-with-count-optimizer-work/
http://bugs.mysql.com/bug.php?id=68814

MySQL LogRotate script

Did you ever try to use log rotate facility in Linux with MySQL? There is no need to script one for this purpose, it is already installed. From MySQL spec file, it looks for logrotate.d folder:

    # Ensure that needed directories exists 
    install -d $RBR%{_sysconfdir}/{logrotate.d,init.d}

As well as there is dedicated mysql-log-rotate.sh script for installing logrotate script. The script path is: /mysql-5.6.24/support-files/mysql-log-rotate.sh Again from spec file:

    # Install logrotate and autostart 
    install -m 644 $MBD/release/support-files/mysql-log-rotate $RBR%{_sysconfdir}/logrotate.d/mysql

After installing there will be mysql script in /etc/logrottate.d/.

   # The log file name and location can be set in
   # /etc/my.cnf by setting the "log-error" option
   # in either [mysqld] or [mysqld_safe] section as
   # follows:
   #
   # [mysqld]
   # log-error=/var/lib/mysql/mysqld.log
   #
   # In case the root user has a password, then you
   # have to create a /root/.my.cnf configuration file
   # with the following content:
   #
   # [mysqladmin]
   # password = <secret> 
   # user= root
   #
   # where "<secret>" is the password. 
   #
   # ATTENTION: The /root/.my.cnf file should be readable
   # _ONLY_ by root !

   /var/lib/mysql/mysqld.log {
         # create 600 mysql mysql
         notifempty
         daily
         rotate 5
          missingok
         compress
    postrotate
         # just if mysqld is really running
         if test -x /usr/bin/mysqladmin && 
          /usr/bin/mysqladmin ping &>/dev/null
       then
          /usr/bin/mysqladmin flush-logs
       fi
   endscript
  }

There is 2 simple and critical problem with this script. First of all the name of error log file. If MySQL started from default my.cnf file, path for error log file will be: /var/log/mysqld.log

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

But in script it is defined as: /var/lib/mysql/mysqld.log Even if you comment out default settings in my.cnf file, MySQL by default will create error log file as follows: datadir + host_name.err = /var/lib/mysql/host_name.err Again there will be no such error log file defined in log rotate script. Second problem is usage of mysqladmin. Script is calling mysqladmin without any password. We assume that there is no such MySQL instance without user, at least in production environment:

     [root@centos7 ~]# /usr/bin/mysqladmin ping /usr/bin/mysqladmin: connect to server at  'localhost' failed error: 'Access denied for user 'root'@'localhost' (using password: NO)'   

     [root@centos7 ~]# /usr/bin/mysqladmin flush-logs /usr/bin/mysqladmin: connect to server at  'localhost' failed error: 'Access denied for user 'root'@'localhost' (using password: NO)' 

From now we know that, after installing MySQL there will be non-working log rotate script, which will fail due to explanations above. Related report is: #73949

Partial table recovery from physical backup

In previous topic, we have covered “Transportable Tablespace” concept by copying and importing table’s tablespace to remote server. See -> Copying Tablespaces to Remote Server The idea is copying tablespace file to remote server, in remote server you must create identical database names and table names manually, then you should discard new table’s tablespace file and import new copied one. To achieve this you must have running MySQL version >= 5.6, innodb_file_per_table=1 and you must know “CREATE statement” of table. Let’s to change our test condition. Assume that, you have MySQL server and you have taken physical backup of your server (you can use Percona XtraBackup, cold backup for eg.). But one of the wonderful day somebody deleted all table data (say -> delete from table_name). In fact your table at this moment exists(.frm and .ibd), you can easily discard table’s tablespace and import tablespace from backup folder. But if table is dropped and you don’t know the create of table. Or even database is dropped. Our path will differ from previous one: *1. Create dropped database manually. 2. Create dropped table by extracting table’s create statement from .frm file which is in backed up directory. To extract table create statement from .frm file you can use **mysqlfrm tool from MySQL Utilities. 3. Discard table’s tablespace (ALTER TABLE t DISCARD TABLESPACE;) 4. Copy .ibd file from backup directory to MySQL’s datadir database directory 5. Import copied back tablespace file.(ALTER TABLE t IMPORT TABLESPACE;)*** You can also read about this concept from documentation -> tablespace-copying I have automatized this process adding table create statement extracting functionality to MySQL-AutoXtraBackup project as –partial recovery option. Here is a demo usage video:

If you tested and found issues, please report it to improve this opensource project.

lower_case_table_names option to lose databases and tables

To lose your data or make it unavailable there is an excellent option in MySQL, rather than drop or delete 🙂 Option name is lower_case_table_names. Default value of this setting is 0:

       mysql> select @@lower_case_table_names; 
       +--------------------------+ 
       | @@lower_case_table_names | 
       +--------------------------+
       | 0 | 
       +--------------------------+ 
       1 row in set (0.00 sec) 

Due to documentation value=0: Table and database names are stored on disk using the lettercase specified in the CREATE TABLE or CREATE DATABASE statement. Name comparisons are case sensitive. You should not set this variable to 0 if you are running MySQL on a system that has case-insensitive file names (such as Windows or OS X). If you force this variable to 0 with –lower-case-table-names=0 on a case-insensitive file system and access MyISAM tablenames using different lettercases, index corruption may result. So related to documentation, tables T1 and t1 will be different, as well as database DB1 and db1. Let’s create sample table and databases:

      mysql> create database DB1; 
      Query OK, 
      1 row affected (0.06 sec) 
      mysql> create database db3; 
      Query OK, 
      1 row affected (0.03 sec) 
      mysql> use db3; 
      Database changed 
      mysql> create table TABLE1(id int not null); 
      Query OK, 
      0 rows affected (0.04 sec) 
      mysql> insert into TABLE1(id) values(1),(2),(3),(4),(5); 
      Query OK, 
      5 rows affected (0.01 sec) 
      Records: 5 Duplicates: 0 Warnings: 0 

You are happy with your tables and databases, but then suddenly somebody with best practice brain says that, it is general rule to change this option equal to 1. Table names are stored in lowercase on disk and name comparisons are not case sensitive. MySQL converts all table names to lowercase on storage and lookup. This behavior also applies to database names and table aliases.
You read documentation , and there was no caution. Decided to change this option, edit my.cnf file and add following under [mysqld]: lower_case_table_names = 1 , then restarted MySQL. From now, you will not able to access to your UPPERCASE created database and tables.

      mysql> use DB1; 
      ERROR 1049 (42000): Unknown database 'db1' 
      mysql> drop database DB1; 
      ERROR 1008 (HY000): Can't drop database 'db1'; database doesn't exist 
      mysql> use db3;
      Database changed 
      mysql> show tables; 
      +---------------+ 
      | Tables_in_db3 | 
      +---------------+ 
      | TABLE1 | 
      +---------------+ 
      1 row in set (0.00 sec) 
      mysql> select * from TABLE1;
      ERROR 1146 (42S02): Table 'db3.table1' doesn't exist 

There is no WARNING/Caution in documentation related to this issue. It maybe critical for many applications, because many developers create database and table names using CamelCase pattern or they just begin everything with Uppercase. So be careful while changing this option. In documentation 2 steps provided for this purpose: 1. For individual tables you can rename it as -> RENAME TABLE TABLE1 to table1; 2. Or you can take backup of all databases, then drop all databases, change option and restart MySQL and then import taken backup. The best way is everytime you have a fresh install of MySQL change this option to 1.

http://dev.mysql.com/doc/refman/5.6/en/identifier-case-sensitivity.html

Error reading GTIDs from binary log: -1

Wonder how MySQL Slave server will act, when disk full condition occurs? Before in our articles we use only single MySQL server. Now think about replication topology, where slave server has problem with full partition. Firstly we will enable Binary Log/GTID on Slave side and will ensure that the changes also applied to binary log on Slave side:

      # BINARY LOGGING # 
      server_id = 2 
      log_bin = /opt/mysql/datadir/mysql-bin 
      log_bin_index = /opt/mysql/datadir/mysql-bin 
      expire_logs_days = 14 
      sync_binlog = 1 
      binlog_format = row 
      relay_log = /opt/mysql/datadir/mysql-relay-bin 
      log_slave_updates = 1 
      read_only = 1 
      gtid-mode = on 
      enforce-gtid-consistency = true 
      master-info-repository = TABLE 
      relay-log-info-repository = TABLE 
      slave-parallel-workers = 15 
      binlog-checksum = CRC32 
      master-verify-checksum = 1 
      slave-sql-verify-checksum = 1 
      binlog-rows-query-log_events = 1 

When disk full condition comes up, error log will be filled as follows:

     2015-05-01 04:42:10 2033 [Note] Slave SQL thread initialized, starting replication in log 'mysql-bin.000002' at position 557, relay log '/opt/mysql/datadir/mysql-relay-bin.000001' position: 4 2015-05-01 04:50:50 7f3b79865700 
     InnoDB: Error: Write to file ./test1/sales.ibd failed at offset 184549376. 
     InnoDB: 1048576 bytes should have been written, only 688128 were written. 
     InnoDB: Operating system error number 0. 
     InnoDB: Check that your OS and file system support files of this size. 
     InnoDB: Check also that the disk is not full or a disk quota exceeded. 
     InnoDB: Error number 0 means 'Success'. 
     InnoDB: Some operating system error numbers are described at 
     InnoDB: http://dev.mysql.com/doc/refman/5.6/en/operating-system-error-codes.html 2015-05-01 04:50:51 2033 
     [ERROR] /opt/mysql/bin/mysqld: The table 'sales' is full 2015-05-01 04:50:51 2033 
     [ERROR] Slave SQL: Worker 14 failed executing transaction '328e26e9-ea51-11e4-8023-080027745404:242' at master log mysql-bin.000002, end_log_pos 275717680; Could not execute Write_rows event on table test1.sales; 
      The table 'sales' is full, 
      Error_code: 1114; handler error HA_ERR_RECORD_FILE_FULL; the event's master log mysql-bin.000002, end_log_pos 275717680, 
     Error_code: 1114 2015-05-01 04:50:51 2033 
     [Warning] Slave SQL: ... The slave coordinator and worker threads are stopped, possibly leaving data in inconsistent state. A restart should restore consistency automatically, although using non-transactional storage for data or info tables or DDL queries  could lead to problems. In such cases you have to examine your data (see documentation for details). 
    Error_code: 1756 2015-05-01 04:50:51 2033 
    [Note] Error reading relay log event: slave SQL thread was killed 2015-05-01 04:50:51 2033 
    [Warning] Slave SQL: ... The slave coordinator and worker threads are stopped, possibly leaving data in inconsistent state. A restart should restore consistency automatically, although using non-transactional storage for data or info tables or DDL queries could lead to problems. In such cases you have to examine your data (see documentation for details). 
   Error_code: 1756 2015-05-01 04:50:51 2033 
   [Warning] Slave SQL: ... The slave coordinator and worker threads are stopped, possibly leaving data in inconsistent state. A restart should restore consistency automatically, although using non-transactional storage for data or info tables or DDL queries could lead to problems. In such cases you have to examine your data (see documentation for details). Error_code: 1756 2015-05-01 04:50:52 2033 
   [Warning] Disk is full writing '/opt/mysql/datadir/mysql-relay-bin.000003' (Errcode: 28 - No space left on device). Waiting for someone to free space... 

Interesting thing is OS error number which is equal to 0. And 0 equals to “success”. After if you try disable binary log/GTID and store only relay-log information in my.cnf as follows:

    # BINARY LOGGING # # 
    server_id = 2 
    #log_bin = /opt/mysql/datadir/mysql-bin 
    #log_bin_index = /opt/mysql/datadir/mysql-bin 
    #expire_logs_days = 14 
    #sync_binlog = 1 
    #binlog_format = row 
    relay_log = /opt/mysql/datadir/mysql-relay-bin 
    #log_slave_updates = 1 
    read_only = 1 
    #gtid-mode = on 
    #enforce-gtid-consistency = true 
    master-info-repository = TABLE 
    relay-log-info-repository = TABLE 
    #slave-parallel-workers = 15 
    #binlog-checksum = CRC32 
    #master-verify-checksum = 1 
    #slave-sql-verify-checksum = 1 
    #binlog-rows-query-log_events = 1 

If you try to start there will be some interesting errors in error log:

2015-05-01 05:05:09 2698 [ERROR] /opt/mysql/bin/mysqld: Found a Gtid_log_event or Previous_gtids_log_event when @@GLOBAL.GTID_MODE = OFF. 
2015-05-01 05:05:14 2698 [ERROR] Error in Log_event::read_log_event(): 'read error', data_len: 8178, event_type: 30 
2015-05-01 05:05:14 2698 [Warning] Error reading GTIDs from binary log: -1 
2015-05-01 05:05:15 2698 [Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
2015-05-01 05:05:15 2698 [Note] Slave I/O thread: connected to master 'repl@192.168.1.164:3307',replication started in log 'mysql-bin.000002' at position 204643802 
2015-05-01 05:05:16 2698 [ERROR] Slave I/O: The slave IO thread stops because the master has @@GLOBAL.GTID_MODE ON and this server has @@GLOBAL.GTID_MODE OFF, Error_code: 1593 
2015-05-01 05:05:16 2698 [Note] Slave I/O thread exiting, read up to log 'mysql-bin.000002', position 204643802 
2015-05-01 05:05:16 2698 [Note] Event Scheduler: Loaded 0 events 
2015-05-01 05:05:16 2698 [Note] /opt/mysql/bin/mysqld: ready for connections. Version: '5.6.24-debug' socket: '/opt/mysql/datadir/mysqld-new.sock' port: 3307 Shahriyar Rzayev's MySQL 
2015-05-01 05:05:16 2698 [Note] Slave SQL thread initialized, starting replication in log 'mysql-bin.000002' at position 274388137, relay log '/opt/mysql/datadir/mysql-relay-bin.000003' position: 274387894 
2015-05-01 05:05:16 2698 [ERROR] Slave SQL: @@SESSION.GTID_NEXT cannot be set to UUID:NUMBER when @@GLOBAL.GTID_MODE = OFF. Error_code: 1781 
2015-05-01 05:05:16 2698 [Warning] Slave: @@SESSION.GTID_NEXT cannot be set to UUID:NUMBER when @@GLOBAL.GTID_MODE = OFF. Error_code: 1781 
2015-05-01 05:05:16 2698 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'mysql-bin.000002' position 274388137 [/code] Errors about disabled GTID is normal and it must be here as usual. But must interesting is:  [ERROR] Error in Log_event::read_log_event(): 'read error', data_len: 8178, event_type: 30           
[Warning] Error reading GTIDs from binary log: -1  

[Warning] Error reading GTIDs from binary log: -1. Related BUG report -> #72437 If you have another issue path related to slave server’s disk usage, will be interesting to hear from you.

Testing MySQL with “read-only” filesystem

From previous articles about “disk full” conditions, you have some taste of testing MySQL with such approach:

1. Testing Disk Full Conditions

2. Using GDB, investigating segmentation fault in MySQL

But there is still untouched topic, about read-only mounted file system and how MySQL will act in such condition. In real life, i have encountered such situation that something happened with Linux server and file system suddenly goes to read-only mode.

Buffer I/O error on device sdb1, logical block 1769961 lost page write due to I/O error on sdb1 sd 0:0:1:0: timing out command, waited 360s sd 0:0:1:0: Unhandled error code sd 0:0:1:0: SCSI error: return code = 0x06000008 Result: hostbyte=DID_OK driverbyte=DRIVER_TIMEOUT,SUGGEST_OK mptscsih: ioc0: attempting task abort! (sc=ffff8100b629a6c0) sd 0:0:1:0: command: Write(10): 2a 00 00 d8 15 17 00 04 00 00 mptscsih: ioc0: task abort: SUCCESS (rv=2002) (sc=ffff8100b629a6c0) Aborting journal on device sdb1. 
ext3_abort called. EXT3-fs error (device sdb1): ext3_journal_start_sb: Detected aborted journal Remounting filesystem read-only __journal_remove_journal_head: freeing b_committed_data EXT3-fs error (device sdb1) in ext3_new_inode: Journal has aborted ext3_abort called. EXT3-fs error (device sdb1): ext3_remount: Abort forced by user ext3_abort called. EXT3-fs error (device sdb1): ext3_remount: Abort forced by user 

There was no error message of course because of read-only partition. That’s why we have no chance to detect why MySQL did not start, until we examine OS level issues. In contrast Oracle handles this condition:

[root@bsnew home]# su - oracle -bash-3.2$ sqlplus / as sysdba 
SQL*Plus: Release 11.2.0.4.0 Production on Mon Apr 7 11:35:10 2014 Copyright (c) 1982, 2013, Oracle. All rights reserved. ERROR: ORA-09925: Unable to create audit trail file Linux-x86_64 Error: 30: Read-only file system Additional information: 9925 ORA-09925: Unable to create audit trail file Linux-x86_64 Error: 30: Read-only file system Additional information: 9925 

Of course if you change error log file path to working path there will be messages:

    2015-04-28 08:04:16 7f27a6c847e0 InnoDB: Operating system error number 30 in a file operation. InnoDB: Error number 30 means 'Read-only file system'. InnoDB: Some operating system error numbers are described at InnoDB: http://dev.mysql.com/doc/refman/5.6/en/operating-system-error-codes.html 
    2015-04-28 08:04:16 1486 [ERROR] InnoDB: File ./ibdata1: 'create' returned OS error 130. Cannot continue operation 150428 08:04:17 mysqld_safe mysqld from pid file /home/error_log_dir/mysqld-new.pid ended 

But it is not useful at this moment, instead, there should be some message while trying starting MySQL directly to STDOUT.

If you have more test paths check related feature request and add them: #72259

Using GDB, investigating segmentation fault in MySQL

In previous article, we have covered some errors and issues with using MySQL in “disk full” environment. Where there was no space left on device.(See here: Testing Disk Full Conditions)

Today’s scenario is -> Starting MySQL with GTID/binary log enabled, in 0 space left Linux(CentOS 6.5) environment.

If you hit a bug or problem, general rule for helping community to fix it is to provide as much information as possible. Especially useful is to give gdb output from coredump. To get coredump you can read this wonderful article Hunting-The-Core

Now let’s explore our situation. Because our segfault is detected while starting MySQL, it is not possible to attach PID to GDB and also using strace. Our my.cnf file:

[mysqld] 
log_bin = /opt/mysql/datadir/mysql-bin 
log_bin_index = /opt/mysql/datadir/mysql-bin 
sync_binlog = 1 
binlog_format = row 
gtid-mode = on 
log_slave_updates = 1 
enforce-gtid-consistency = true
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES core-file 
[mysqld_safe]
core_file_size=unlimited 

Starting MySQL:

[root@localhost ~]# /opt/mysql/bin/mysqld_safe --defaults-file=/opt/mysql/my.cnf --user=mysql --datadir=/opt/mysql/datadir --socket=/opt/mysql/datadir/mysqld-new.sock --pid-file=/home/error_log_dir/mysqld-new.pid --port=3307 --log-error=/home/error_log_dir/error.err & 
[1] 2849 
[root@localhost ~]# 150427 06:31:42 mysqld_safe Logging to '/home/error_log_dir/error.err'. 150427 06:31:42 mysqld_safe Starting mysqld daemon with databases from /opt/mysql/datadir /opt/mysql/bin/mysqld_safe: line 166: 3110 Segmentation fault (core dumped) nohup /opt/mysql/bin/mysqld
--defaults-file=/opt/mysql/my.cnf 
--basedir=/opt/mysql    
--datadir=/opt/mysql/datadir 
--plugin-dir=/opt/mysql/lib/plugin --user=mysql     
--log-error=/home/error_log_dir/error.err 
--pid-file=/home/error_log_dir/mysqld-new.pid 
--socket=/opt/mysql/datadir/mysqld-new.sock 
--port=3307 < /dev/null >> /home/error_log_dir/error.err 2>&1 
150427 06:32:07 mysqld_safe mysqld from pid file /home/error_log_dir/mysqld-new.pid ended 

3110 Segmentation fault (core dumped) -> We have core dump. Using gdb with coredump:

[root@localhost coredumps]# gdb /opt/mysql/bin/mysqld core.3110 
BFD: Warning: /home/coredumps/core.3110 is truncated: expected core file size >= 803807232, found: 697573376. 
[New Thread 3110] 
[New Thread 3111] 
Cannot access memory at address 0x7f5ec00ff168 
Cannot access memory at address 0x7f5ec00ff168 
Cannot access memory at address 0x7f5ec00ff168 
Reading symbols from /lib64/ld-linux-x86-64.so.2...
Reading symbols from /usr/lib/debug/lib64/ld-2.12.so.debug...done. done. 
Loaded symbols for /lib64/ld-linux-x86-64.so.2 Failed to read a valid object file image from memory. 
Core was generated by `/opt/mysql/bin/mysqld --defaults-file=/opt/mysql/my.cnf --basedir=/opt/mysql --'. Program terminated with signal 11, Segmentation fault. #0 0x00007f5ebfccd8ac in ?? () 

(gdb) bt #0 0x00007f5ebfccd8ac in ?? () Cannot access memory at address 0x7fffb0aa89b8 

(gdb) bt full #0 0x00007f5ebfccd8ac in ?? () No symbol table info available. Cannot access memory at address 0x7fffb0aa89b8 

Such information is not sufficient further analysis, that’s why you should try to run MySQL start command with-in GDB as follows:

[root@localhost error_log_dir]# gdb /opt/mysql/bin/mysqld 
(gdb) run --defaults-file=/home/error_log_dir/my.cnf --basedir=/opt/mysql --datadir=/opt/mysql/datadir --plugin-dir=/opt/mysql/lib/plugin --user=mysql --log-error=/home/error_log_dir/error.err --pid-file=/home/error_log_dir/mysqld-new.pid --socket=/opt/mysql/datadir/mysqld-new.sock --port=3307 . . 
Program received signal SIGSEGV, Segmentation fault. 0x0000000000000000 in ?? () . . 

(gdb) bt 
    #0 0x0000000000000000 in ?? () 
    #1 0x0000000000ae6242 in my_printf_warning (format=0x1026f08 "Disk is full writing '%s' (Errcode: %d - %s). Waiting for someone to free space...") at /root/mysql-5.6.24/mysys/my_error.c:260 #2 0x0000000000ac9052 in wait_for_free_space (filename=0x1fe7ee0 "/opt/mysql/datadir/mysql-bin.~rec~", errors=0) at /root/mysql-5.6.24/mysys/errors.c:115 
#3 0x0000000000af1ac7 in my_write (Filedes=19, Buffer=0x1964d00 "/opt/mysql/datadir/mysql-bin.000003n", Count=36, MyFlags=52) at /root/mysql-5.6.24/mysys/my_write.c:89 
#4 0x0000000000acd5ae in inline_mysql_file_write (src_file=0x1027708 "/root/mysql-5.6.24/mysys/mf_iocache.c", src_line=1788, file=19, buffer=0x1964d00 "/opt/mysql/datadir/mysql-bin.000003n", count=36, flags=52) at /root/mysql-5.6.24/include/mysql/psi/mysql_file.h:1141 
#5 0x0000000000ad078c in my_b_flush_io_cache (info=0x183c1a8, need_append_buffer_lock=0) at /root/mysql-5.6.24/mysys/mf_iocache.c:1787 
#6 0x0000000000a7132b in MYSQL_BIN_LOG::sync_purge_index_file (this=0x183b400) at /root/mysql-5.6.24/sql/binlog.cc:4420 
#7 0x0000000000a6e206 in MYSQL_BIN_LOG::open_binlog (this=0x183b400, log_name=0x190d130 "/opt/mysql/datadir/mysql-bin", new_name=0x0, io_cache_type_arg=WRITE_CACHE, max_size_arg=1073741824, null_created_arg=false, need_lock_index=true, need_sid_lock=true, extra_description_event=0x0) at /root/mysql-5.6.24/sql/binlog.cc:3146 
#8 0x000000000063ad8e in init_server_components () at /root/mysql-5.6.24/sql/mysqld.cc:5012 
#9 0x000000000063b6e7 in mysqld_main (argc=19, argv=0x186de68) at /root/mysql-5.6.24/sql/mysqld.cc:5455 
#10 0x000000000062fc74 in main (argc=10, argv=0x7fffffffe3d8) at /root/mysql-5.6.24/sql/main.cc:25 ###################### 

(gdb) bt full 
#0 0x0000000000000000 in ?? () No symbol table info available. 
#1 0x0000000000ae6242 in my_printf_warning (format=0x1026f08 "Disk is full writing '%s' (Errcode: %d - %s). Waiting for someone to free space...") at /root/mysql-5.6.24/mysys/my_error.c:260 args = {{gp_offset = 32, fp_offset = 48, overflow_arg_area = 0x7fffffffd8a0, reg_save_area = 0x7fffffffd7e0}} wbuff = "Disk is full writing '/opt/mysql/datadir/mysql-bin.~rec~' (Errcode: 28 - No space left on device). Waiting for someone to free space...0033032737737737717700002103413773673771770000340267376367377177000027133224u00000000Zn33636737717700000000000000000000/35!3663771770000C00000000000000f223!36637717700000100000000000000P31!3663771770000LC_"... _db_stack_frame_ = {func = 0x102c53c "my_write", file = 0x102c518 "/root/mysql-5.6.24/mysys/my_write.c", level = 2147483654, prev = 0x7fffffffd9f0} 
#2 0x0000000000ac9052 in wait_for_free_space (filename=0x1fe7ee0 "/opt/mysql/datadir/mysql-bin.~rec~", errors=0) at /root/mysql-5.6.24/mysys/errors.c:115 errbuf = "No space left on device003252425700350000002303240201000000002723240201000000000033137737734000000 ܆01000000003400000006000000 ܆0100000000@3313773773771770000[3462570000000000 ܆010000000000܆0123000000<305020100000000303050201000000" 
#3 0x0000000000af1ac7 in my_write (Filedes=19, Buffer=0x1964d00 "/opt/mysql/datadir/mysql-bin.000003n", Count=36, MyFlags=52) at /root/mysql-5.6.24/mysys/my_write.c:89 writtenbytes = 18446744073709551615 sum_written = 0 errors = 0 initial_count = 36 _db_stack_frame_ = {func = 0x1027c7b "my_b_flush_io_cache", file = 0x1027708 "/root/mysql-5.6.24/mysys/mf_iocache.c", level = 2147483653, prev = 0x7fffffffdb10}  

From know developers should be able to see exact code line, for further examination. For further reading(to see “bt full” full output) refer related BUG report: #76852 It is already verified.

Testing “disk full” conditions in MySQL

How MySQL will act if there is no space left on hard disk? To answer this question, let’s test it: Our first test is with MySQL 5.6.24-debug with disabled binary log: Trying to import huge dump, after while it says table is full:

Query OK, 12725 rows affected (2.46 sec) Records: 12725 Duplicates: 0 Warnings: 0
Query OK, 12724 rows affected (2.40 sec) Records: 12724 Duplicates: 0 Warnings: 0
Query OK, 12726 rows affected (2.53 sec) Records: 12726 Duplicates: 0 Warnings: 0
ERROR 1114 (HY000): The table 'sales' is full 
ERROR 1114 (HY000): The table 'sales' is full ERROR 1114 (HY000): The table 'sales' is full

In error log you will see something like:

[root@localhost mysql]# tail -f /opt/mysql/datadir/error.err 
Version: '5.6.24-debug' socket: '/opt/mysql/datadir/mysqld-new.sock' port: 3307     Shahriyar Rzayev's MySQL 
2015-04-24 03:56:09 7fabeffff700 InnoDB: Error: Write to file ./sales2/sales.ibd failed at offset 34603008. InnoDB: 1048576 bytes should have been written, only 1011712 were written. InnoDB: Operating system error number 11. InnoDB: Check that your OS and file system support files of this size. 
InnoDB: Check also that the disk is not full or a disk quota exceeded. 
InnoDB: Error number 11 means 'Resource temporarily unavailable'. 
InnoDB: Some operating system error numbers are described at 
2015-04-24 03:56:09 21838 [ERROR] /opt/mysql/bin/mysqld: The table 'sales' is full 
2015-04-24 03:56:12 21838 [ERROR] /opt/mysql/bin/mysqld: The table 'sales' is full 
2015-04-24 03:56:15 21838 [ERROR] /opt/mysql/bin/mysqld: The table 'sales' is full 
2015-04-24 03:56:19 21838 [ERROR] /opt/mysql/bin/mysqld: The table 'sales' is full 
2015-04-24 03:56:20 21838 [ERROR] /opt/mysql/bin/mysqld: The table 'sales' is full 

At this point “Operating system error number 11.” and OS disk usage is:

[root@localhost ~]# df -h 
Filesystem Size Used Avail Use% Mounted on 
/dev/mapper/VolGroup-lv_root 6.7G 6.3G 24M 100% / 
tmpfs 246M 0 246M 0% /dev/shm 
/dev/sda1 485M 55M 405M 12% /boot

If we continue our import process with another database.
Error will change:

mysql> create database sales3; 
Query OK, 
1 row affected, 1 warning (0.08 sec) 
mysql> use sales3; 
Database changed 
ERROR 3 (HY000): Error writing file './sales3/Product_Codes.frm' (Errcode: 28 - No space left on device) 
ERROR 1146 (42S02): Table 'sales3.Product_Codes' doesn't exist 
ERROR 1146 (42S02): Table 'sales3.Product_Codes' doesn't exist Query OK, 0 rows affected (0.00 sec) 
ERROR 1146 (42S02): Table 'sales3.Product_Codes' doesn't exist Query OK, 0 rows affected (0.00 sec) 
ERROR 3 (HY000): Error writing file './sales3/account_balance.frm' (Errcode: 28 - No space left on device) 
ERROR 1146 (42S02): Table 'sales3.account_balance' doesn't exist 
ERROR 1146 (42S02): Table 'sales3.account_balance' doesn't exist 
ERROR 1146 (42S02): Table 'sales3.account_balance' doesn't exist  

Disk usage is:

[root@localhost ~]# df -h 
Filesystem Size Used Avail Use% Mounted on 
/dev/mapper/VolGroup-lv_root 6.7G 6.3G 16M 100% / 
tmpfs 246M 0 246M 0% /dev/shm 
/dev/sda1 485M 55M 405M 12% /boot

Of course there will be NO entry in error log because disk is full.
That’s why let’s change error log path to another directory and start from here:

[root@localhost error_log_dir]# setenforce 0 (use only in test environment)
[root@localhost error_log_dir]# chown mysql:mysql /home/error_log_dir/
[root@localhost mysql]# /opt/mysql/bin/mysqld_safe --defaults-file=/opt/mysql/my.cnf --user=mysql --datadir=/opt/mysql/datadir --socket=/opt/mysql/datadir/mysqld-new.sock --pid-file=/opt/mysql/datadir/mysqld-new.pid --port=3307 --log-error=/home/error_log_dir/error.err & 
[root@localhost mysql]# 150424 05:03:06 mysqld_safe Logging to '/home/error_log_dir/error.err'. 150424 05:03:06 mysqld_safe Starting mysqld daemon with databases from /opt/mysql/datadir 150424 05:03:13 mysqld_safe Number of processes running now: 0 150424 05:03:13 mysqld_safe mysqld restarted  

It will continuously restart, because of related 28 – No space left on device error:

2015-04-24 05:03:36 22371 [ERROR] /opt/mysql/bin/mysqld: Error writing file '/opt/mysql/datadir/mysqld-new.pid' (Errcode: 28 - No space left on device)
2015-04-24 05:03:36 22371 [ERROR] Can't start server: can't create PID file: No space left on device

If we change PID file path it will start as usual:

[root@localhost mysql]# /opt/mysql/bin/mysqld_safe --defaults-file=/opt/mysql/my.cnf --user=mysql --datadir=/opt/mysql/datadir --socket=/opt/mysql/datadir/mysqld-new.sock --pid-file=/home/error_log_dir/mysqld-new.pid --port=3307 --log-error=/home/error_log_dir/error.err & 

Another thing we should note that, after getting -> Errcode: 28 – No space left on device , if you try to create new database:

mysql> create database sales3; Query OK, 1 row affected, 1 warning (0.12 sec)
mysql> show warnings;  
| Error | 3 | Error writing file './sales3/db.opt' (Errcode: 28 - No space left on device) | 

db.opt file will be created as empty file. If you want to create new table:

mysql> create table t1(id int); 
ERROR 3 (HY000): Error writing file './sales3/t1.frm' (Errcode: 28 - No space left on device)

And now let’s enable binary log and then try to import dump. Related to Binary Log error’s due full disk there was a BUG fixed from 5.6.23 version of MySQL: #73365 And another one still waiting as OPEN:#72437 Where i got an error: [ERROR] Error in Log_event::read_log_event(): ‘read error’, data_len: 2070, event_type: 29 And interesting warning: [Warning] Error reading GTIDs from binary log: -1

For activating binary log and GTID add followings to my.cnf file:

log_bin = /opt/mysql/datadir/mysql-bin 
log_bin_index = /opt/mysql/datadir/mysql-bin 
sync_binlog = 1 
binlog_format = 
row gtid-mode = on 
log_slave_updates = 1 
enforce-gtid-consistency = true  

Then drop all previous imports(databases) and again try to import dump. You will get a new problem which i have reported, while writing this article: #76825 To reproduce this report, read “How to repeat” section. Another interesting thing, i want to note is, after detecting full disk error, if we try to create view, we will hit another reported issue: #76827 (I have disabled GTID/binary log while testing views)

UPDATE 1

If you try to create a procedure while disk is full, you will get a table corruption of mysql.proc table. From error log:

2015-04-27 05:30:22 1485 [Warning] Disk is full writing './mysql/proc.MYD' (Errcode: 28 - No space left on device). Waiting for someone to free space...
2015-04-27 05:30:22 1485 [Warning] Retry in 60 secs. Message reprinted in 600 secs 
2015-04-27 05:39:23 1485 [ERROR] /opt/mysql/bin/mysqld: Incorrect key file for table './mysql/proc.MYI'; try to repair it 
2015-04-27 05:39:23 1485 [ERROR] Got an error from thread_id=1, /root/mysql-5.6.24/storage/myisam/mi_write.c:223 
2015-04-27 05:39:23 1485 [ERROR] MySQL thread id 1, OS thread handle 0x7fe47ed20700, query id 756 localhost root System lock  

If you select from this table:

mysql> select * from mysql.proc; 
ERROR 1194 (HY000): Table 'proc' is marked as crashed and should be repaired 

Same thing if you create a function. And even with events:

2015-04-27 05:56:34 1485 [ERROR] /opt/mysql/bin/mysqld: Incorrect key file for table './mysql/event.MYI'; try to repair it 
2015-04-27 05:56:34 1485 [ERROR] Got an error from thread_id=7, /root/mysql-5.6.24/storage/myisam/mi_write.c:223 
2015-04-27 05:56:34 1485 [ERROR] MySQL thread id 7, OS thread handle 0x7fe47ed20700, query id 789 localhost root System lock

UPDATE 2

When disk full condition occurs you will not be able to create and drop indexes even from empty tables:

mysql> select count(*) from customers;
+----------+ 
| count(*) | 
+----------+ 
| 0 | 
+----------+ 
1 row in set (0.04 sec) 

mysql> alter table customers add index(COMMENT_ID); 
ERROR 3 (HY000): Error writing file './proc_func/#sql-5cd_a.frm' (Errcode: 28 - No space left on device) mysql> alter table customers drop index `customer_name_i`; 
ERROR 3 (HY000): Error writing file './proc_func/#sql-5cd_a.frm' (Errcode: 28 - No space left on device) 

UPDATE 3

It is impossible drop database after crashing ./mysql/proc table:

mysql> drop database proc_func; 
ERROR 145 (HY000): Table './mysql/proc' is marked as crashed and should be repaired mysql> drop database sales; 
ERROR 145 (HY000): Table './mysql/proc' is marked as crashed and should be repaired mysql> drop database sales2; 
ERROR 145 (HY000): Table './mysql/proc' is marked as crashed and should be repaired 

After repairing proc table if you try to drop database, there will be warnings about mysql.event table:

mysql> drop database test_disk; 
Query OK, 0 rows affected, 2 warnings (0.10 sec) 
mysql> show warnings; +-------+------+-------------------------------------------------------------------+ | Level | Code | Message | +-------+------+-------------------------------------------------------------------+ | Error | 145 | Table './mysql/event' is marked as crashed and should be repaired | 
| Error | 1194 | Table 'event' is marked as crashed and should be repaired | +-------+------+-------------------------------------------------------------------+ 
2 rows in set (0.02 sec)  

At this moment, that’s all. Will update this article every time, when i find related issues. Thank you for reading.

Playing with count(*) optimizer work

Article about bug report #68814 related to testing count(*) explain plan.
Our sales table huge enough to play with.

mysql> select count(*) from sales; 
+----------+ 
| count(*) | 
+----------+ 
| 2500003 | 
+----------+ 
1 row in set (0.56 sec)

First with regular count(*) without where clause:

mysql> explain select count(*) from salesG 
*************************** 1. row *************************** 
id: 1 
select_type: SIMPLE 
table: sales 
type: index 
possible_keys: NULL 
key: sales_cust_idx 
key_len: 4 
ref: NULL 
rows: 2489938 
Extra: Using index 1 row in set (0.00 sec) 

Estimated rows -> rows: 2489938 Then with {where sales_id > 0}:

mysql> explain select count(*) from sales where sales_id > 0G
*************************** 1. row *************************** 
id: 1 
select_type: SIMPLE 
table: sales 
type: range 
possible_keys: PRIMARY 
key: PRIMARY 
key_len: 4 
ref: NULL 
rows: 1244969 
Extra: Using where; Using index 1 row in set (0.00 sec)

Estimated rows -> rows: 1244969 -> so there is difference between query with {sales_id > 0} and with no clause.

Another one with {where sales_id > 1800000}:

mysql> explain select count(*) from sales where sales_id > 1800000G
*************************** 1. row *************************** 
id: 1 
select_type: SIMPLE 
table: sales 
type: range 
possible_keys: PRIMARY 
key: PRIMARY 
key_len: 4 
ref: NULL 
rows: 1244969 
Extra: Using where; Using index 
1 row in set (0.00 sec) 

Estimated rows -> rows: 1244969 So there is no difference between {sales_id > 1800000} and {sales_id > 0} (by mean of explain plan and estimated rows)

Another interesting thing:

-- 1 
mysql> explain select count(*) from sales where sales_id >0 or sales_id <0G
*************************** 1. row *************************** 
id: 1 
select_type: SIMPLE 
table: sales 
type: range 
possible_keys: PRIMARY 
key: PRIMARY 
key_len: 4 
ref: NULL 
rows: 1244970 
Extra: Using where; Using index 
1 row in set (0.00 sec)

Estimated rows -> rows: 1244969 + 1

-- 2 
mysql> explain select count(*) from sales where sales_id >0 or sales_id <=0G
*************************** 1. row *************************** 
id: 1 
select_type: SIMPLE 
table: sales 
type: index 
possible_keys: PRIMARY 
key: sales_cust_idx 
key_len: 4 
ref: NULL 
rows: 2489938 
Extra: Using where; Using index 
1 row in set (0.00 sec) 

Estimated rows: 2489938