Category Archives: MySQL 8

MySQL 8 – Exploring new features and performances

Skip Slave Counter with GTID_MODE ON #ERROR 1858 (HY000)

With generic asynchronous MySQL Replication we often use skip_slave_counter to tackle / skip anonymous transactions which creates errors or halt MySQL Replication. But if we try to skip slave counter whilst GTID_MOD is ON we get following error.

 ERROR 1858 (HY000): sql_slave_skip_counter can not be set when the server is running with @@GLOBAL.GTID_MODE = ON.  

In this case we should do following steps

  1. Stop Replication.
  2. .Set GTID_MODE to ON_PERMISSIVE
  3. Skip slave counter.
  4. Start slave.
  5. Set GTID_MODE to ON once it catches the master.

GTID_MODE is dynamic variable which can be set on the run. Here we need to look at GTID_MODE following options.

  • OFF: Both new and replicated transactions must be anonymous.
  • OFF_PERMISSIVE: New transactions are anonymous. Replicated transactions can be either anonymous or GTID transactions.
  • ON_PERMISSIVE: New transactions are GTID transactions. Replicated transactions can be either anonymous or GTID transactions.
  • ON: Both new and replicated transactions must be GTID transactions.

All these steps are shown below

mysql> stop slave;
mysql> set global gtid_mode=ON_PERMISSIVE; 
mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;
mysql> START SLAVE; 
mysql> SHOW SLAVE STATUS\G;
### Once caught up set it back
mysql> set global gtid_mode=ON;

Thanks for visiting , please dont forget to leave you feedback

Regards

Raja Naveed

GTID based Asynchronous Replication in MySQL 8

MySQL replication by default is asynchronous. The master writes events to its binary log but does not know whether or when a slave has retrieved and processed them. With asynchronous replication, if the master crashes, transactions that it has committed might not have been transmitted to any slave. Consequently, failover from master to slave in this case may result in failover to a server that is missing transactions relative to the master.

MySQL 8.0 supports different methods of replication.

Traditional Replication Method using Relay logs:

The traditional method is based on replicating events from the master’s binary log and requires the log files and positions in them to be synchronized between master and slave.

GTID Based Replication:

The newer method based on global transaction identifiers (GTIDs) is transactional and therefore does not require working with log files or positions within these files, which greatly simplifies many common replication tasks. Replication using GTIDs guarantees consistency between master and slave if all transactions committed on the master have also been applied on the slave

 In order to use GTID based replication we need to enable following parameters in my.cnf

server_id=[N]
enforce_gtid_consistency = on
gtid_mode = on
log_bin
log_slave_updates

Suppose we a stand-alone server and we would like to set up a slave/ standby to this primary server. Traditionally we mention master binlog name and current position in out change master statement as shown below.

CHANGE MASTER TO
MASTER_HOST='master2.example.com',
MASTER_USER='replication' ,
MASTER_PASSWORD='password',
MASTER_PORT=3306,
MASTER_LOG_FILE='master2-bin.001',
MASTER_LOG_POS=4,
MASTER_CONNECT_RETRY=10;

But using GTID method we don’t need to mention all the above parameters in change master statement, but we only need to mention gtid_purged and MASTER_AUTO_POSITION=1 as shown below.

  
CHANGE MASTER TO
MASTER_HOST='master2.example.com', 
MASTER_USER='replication' , 
MASTER_PASSWORD='password',  
MASTER_AUTO_POSITION=1; 

We also need to gtid_purged after before starting slave which can be obtained from primary server using show variables statement as show below.

mysql> show variables like '%gtid_purged%';
 +---------------+--------------------------------------------------+
 | Variable_name | Value                                            |
 +---------------+--------------------------------------------------+
 | gtid_purged   | 51a7ada5-36f8-11e6-a1e1-005056873fa8:1-620259803 |
 +---------------+--------------------------------------------------+

Now we can set the above gtid_purged on slave.

 SET @@GLOBAL.GTID_PURGED=' 
51a7ada5-36f8-11e6-a1e1-005056873fa8:1-620259803' 

#We can also use parallel threads to multiply replication threads for better performance if needed.

Note: GTID replication is very useful in database migration projects. Watch out for my Next blog Database MySQL 5.7 to MySQL 8 migration using Asynchronous GTID replication.  

Thanks for reading and visiting, Don’t forget to leave your feedback.

Raja Naveed

MySQL and memory Allocators malloc, tcmalloc and jemalloc

Memory Allocation in UNIX:

UNIX uses C dynamic memory allocation libraries for memory allocation. Namely malloc, realloc, calloc and free   are used.  Functions of these liberaries are given below.

 

Function Description
malloc allocates the specified number of bytes
realloc Increases or decreases the size of the specified block of memory. Reallocates it if needed
calloc allocates the specified number of bytes and initializes them to zero
free releases the specified block of memory back to the system

 

Best memory allocators other than above TCMALLOC and JEMALLOC.

Gperftools tcmalloc():

 

gperftools is Google Performance Tools which also provides malloc() called tcmalloc. Works particularly well with threads and Standard Template Library (STL). gperftools is thread-friendly heapchecker, heap –profiler and cp-profiler. TCMALLOC is available in repository for more details please visit https://github.com/gperftools

 

JEMALLOC : 

jemalloc is a general purpose malloc(3) implementation that emphasises fragmentation avoidance and scalable concurrency support   jemalloc first came into use as the FreeBSD libc allocator in 2005, and since then it has found its way into numerous applications that rely on its predictable behaviour.  In 2010 jemalloc development efforts broadened to include developer support features such as heap profiling and extensive monitoring/tuning hooks.  Modern jemalloc releases continue to be integrated back into FreeBSD, and therefore versatility remains critical.  Ongoing development efforts trend toward making jemalloc among the best allocators for a broad range of demanding applications, and eliminating/mitigating weaknesses that have practical repercussions for real world applications.

How to install tcmalloc:

yum  list gperftools-libs 

yum -y install gperftools-libs

Now you will see few new libraries:

bash-4.2$ rpm -ql gperftools-libs

/usr/lib64/libprofiler.so.0

/usr/lib64/libprofiler.so.0.4.14

/usr/lib64/libtcmalloc.so.4

/usr/lib64/libtcmalloc.so.4.4.5

/usr/lib64/libtcmalloc_and_profiler.so.4

/usr/lib64/libtcmalloc_and_profiler.so.4.4.5

/usr/lib64/libtcmalloc_debug.so.4

/usr/lib64/libtcmalloc_debug.so.4.4.5

/usr/lib64/libtcmalloc_minimal.so.4

/usr/lib64/libtcmalloc_minimal.so.4.4.5

/usr/lib64/libtcmalloc_minimal_debug.so.4

/usr/lib64/libtcmalloc_minimal_debug.so.4.4.5

 

How to install jemalloc:

yum list jemalloc

yum install jemalloc.x86_64

 

Now you will see few new libraries:

 

bash-4.2$ rpm -ql jemalloc

/usr/bin/jemalloc.sh

/usr/lib64/libjemalloc.so.1

/usr/share/doc/jemalloc-3.6.0

/usr/share/doc/jemalloc-3.6.0/COPYING

/usr/share/doc/jemalloc-3.6.0/README

/usr/share/doc/jemalloc-3.6.0/VERSION

/usr/share/doc/jemalloc-3.6.0/jemalloc.html

How and why to use tcmalloc or jemalloc with MySQL :

I will refer to used case. One of MySQL 5.6 instance I have been managing was struggling with memory leaks. MySQL have been using swap with all optimum recommended settings. As one of last resort I decided to use different memory allocation method and results were amazing.  First take a look at swap usage with tcmalloc and with default malloc .

With default malloc()

with TCMALLOC()

Other benefits includes reduced sort activity , smooth i/o activity , less table level locks , low temp table usage , positive memory usage on MyISAM tables for meta data dictionary as show below.  I also noticed the boost in performance and transactions as well because MySQL instance started generating more binary logs.

how to configure tcmalloc or jemalloc with MySQL:

there are two ways you can  make MySQL use tcmalloc or jemalloc once libraries are installed.

  •  configure library in my.cnf file under [mysqld_safe] as shown below.
[mysqld_safe]
#malloc settings
malloc-lib=/usr/lib64/libtcmalloc.so.4.4.5

or

[mysqld_safe]
#malloc settings
malloc-lib=/usr/lib64/libjemalloc.so.1
  • second method is to start MySQL directly using desired library on command line as show below
LD_PRELOAD=/usr/lib64/libtcmalloc.so.4.2.6 mysqld --defaults-file=/etc/my.cnf --daemonize

or

LD_PRELOAD=/usr/lib64/libjemalloc.so.1 mysqld --defaults-file=/etc/my.cnf --daemonize

 

I hope this will help you all, Don’t forget to like, share and comment

Thanks and Regards

Raja M Naveed

Deprecated variables in MySQL 8

The following system variables, status variables, and options have been deprecated in MySQL 8.0.

Deprecated :

expire_logs_days: Purge binary logs after this many days.

Added:

binlog_expire_logs_seconds: Sets the binary log expiration period in seconds.This is to replace the expire_logs_days.

Deprecated :

innodb_undo_tablespaces: The number of tablespace files that rollback segments are divided between. Deprecated as of MySQL 8.0.4.

Added:

No alternative added.

log_syslog: Whether to write error log to syslog. Deprecated as of MySQL 8.0.2.symbolic-links: Permit symbolic links for MyISAM tables. Deprecated as of MySQL 8.0.2.Symbolic link support, along with the the –symbolic-links option that controls it, is deprecated and will be removed in a future version of MySQL. In addition, the option is disabled by default. The related have_symlink system variable also is deprecated and will be removed in a future version of MySQL

Added:

No Alternative added

 

 

I hope this will help you all, Don’t forget to like, share and comment

Thanks and Regards

Raja M Naveed

 

 

MySQL 8 Invisible Indexes

Invisible Indexes

Invisible indexes is new feature added in MySQL 8.0. This feature can help us to mark an index as   unavailable for use by optimizer. This means that index will still be maintained and kept  up to date  in metadata dictionary as data is modified. These marked indexes are not permitted to be used by optimizer even by INDEX hint.

Indexes are visible by default. To control index visibility for a new index or existing one  VISIBLE OR INVISIBLE keywords are used.

for example: If we have following table with index j_idx

CREATE TABLE t1 ( 

i INT, 

j INT, 

k INT, 

INDEX i_idx (i) INVISIBLE 

                                 ) ENGINE = InnoDB; 

CREATE INDEX j_idx ON t1 (j) INVISIBLE; 

ALTER TABLE t1 ADD INDEX k_idx (k) INVISIBLE;

To change the visibility of existing  index we can use following statements.

ALTER TABLE t1 ALTER INDEX i_idx INVISIBLE; 

ALTER TABLE t1 ALTER INDEX i_idx VISIBLE;

Information about index, if it is visible or invisible can be extracted from INFORMATION_SCHEMA.STATISTICS as shown below.

mysql> SELECT INDEX_NAME, IS_VISIBLE 
FROM INFORMATION_SCHEMA.STATISTICS 
WHERE TABLE_SCHEMA = 'db1' AND TABLE_NAME = 't1'; 

+----------------+---------------+ 
| INDEX_NAME     | IS_VISIBLE | 
+----------------+---------------+ 
| i_idx          | YES           | 
| j_idx          | NO            | 
| k_idx          | NO            | 
+----------------+---------------+

Invisible Indexes can help us possible to test the effect of removing an index for query performance, without dropping or recreating index which are expensive operations.

Primary Key Index cannot be made invisible , if you try to do that you will get error as shown below

mysql> ALTER TABLE t2 ALTER INDEX j_idx INVISIBLE; 

ERROR 3522 (HY000): A primary key index cannot be invisible.

I hope this will help you all, Don’t forget to like, share and comment

Thanks and Regards

Raja M Naveed