Category Archives: Linux

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

Reduce startup and shutdown time by using warm up innodb buffer pool

Reduce startup and shutdown time by using warm up innodb buffer pool

For database servers having large memory utilisation and have big innodb_buffer_pool_size , it takes longer to stop and start the instance. This is because more data and indexes stored in huge  innodb_buffer_pool. This data and indexes are used by queries running on the server prior to go to the disk  if not found in innodb_buffer_pool. So bigger the buffer pool more time it will take to shutdown. One way of reducing the time is using warm up innodb buffer pool by dumping the state of buffer and reusing it when starting up.

We can do it by turning innodb_buffer_pool_dump_at_shutdown on. In normal restart , innodb_buffer_pool gets empty and after restart it take time to warm up again.  By using innodb_buffer_pool_dump_at_shutdown we can use pre- warmed up innodb_buffer_pool which can reduce the time significantly. It is dynamic variable and can but turned on / off online and we can add it in the my.cnf file as permanent change.

SET GLOBAL innodb_buffer_pool_dump_at_shutdown = 1 / ON

or we can add it in my.cnf as shown under

innodb_buffer_pool_dump_at_shutdown=ON

Now if we shutdown the server,  ib_buffer_pool dump file will be created under the MySQL datadir which can be configured to be created on different location and file name by using innodb_buffer_pool_filename. This file will store current state and data of innodb buffer pool.

Now we want server to use this dump file when it will start again. We can do it by using following entry in my.cnf file.

innodb_buffer_pool_load_at_startup=ON

After restarting the server we can check ib_buffer_pool file created in data directory and also we can view the contents as well as shown  below.

bash-4.2$ ls ib_buffer_pool
ib_buffer_pool

bash-4.2$ less ib_buffer_pool
0,278534
0,47
0,442406
0,278535
0,48
0,49
0,50
0,442407

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

Thanks and Regards

Raja M Naveed

Fine tune MySQL for Better performance

Few issues I came across when dealing with MySQL performance. Let us go through these problems one by one.

Swap usage:

MySQL loves memory as almost all databases do but MySQL tries to utilise all  the possible available memory. In order to restrict MySQL to use allocated memory resources we should implement following:-

  •  Swapiness

Swappiness is the kernel parameter that defines how much (and how often) your Linux kernel will copy RAM contents to swap. This parameter’s default value is “60” and it can take anything from “0” to “100”. The higher the value of the swappiness parameter, the more aggressively your kernel will swap.

Action: set swapiness to 1 as shown below

bash-4.2$ sysctl vm.swappiness=1

bash-4.2$  cat /proc/sys/vm/swappiness
1
  • innodb_buffer_pool_size

Allocate 60% of the total memory to MySQL instance. It is configured via configuration file my.cnf. For Example if we have 100G of RAM then we will allocate 60G to MySQL. We can reduce or increase percentage of memory allocation as per needs but increase in memory up to certain limit allocation can cause aggressive swap operations.

innodb_buffer_pool_size= 60G

  • Huge Pages

Hugepages is a mechanism that allows the Linux kernel to utilize the multiple page size capabilities of modern hardware architectures. Linux uses pages as the basic unit of memory, where physical memory is partitioned and accessed using the basic page unit. The default page size is 4096 Bytes in the x86 architecture. Hugepages allows large amounts of memory to be utilized with a reduced overhead. Linux uses “Translation Lookaside Buffers” (TLB) in the CPU architecture. These buffers contain mappings of virtual memory to actual physical memory addresses. So utilizing a huge amount of physical memory with the default page size consumes the TLB and adds processing overhead.

Applications that perform a lot of memory accesses may obtain performance improvements by using large pages due to reduced Translation Lookaside Buffer (TLB) misses.

Before large pages can be used on Linux, the kernel must be enabled to support them and it is necessary to configure the HugeTLB memory pool. For reference, the HugeTBL API is documented in the Documentation/vm/hugetlbpage.txt file of your Linux sources.

Please click here to see how to implement huge pages

  • Metadata states persistence

Metadata or database stats should be kept up to date by performing optimize operations and by keeping innodb_persistent_stats =1 / ON . Optimisation of tables can be difficult when table is very large therefor we should implement data partitions for large tables. Optimisation not only updates the stats but it re-organises the indexes as well and helps regain the free storage.

  • I/O capacity

If there are enough cpus and memory available then we should consider to increase number of  innodb_write_io_threads and innodb_read_io_threads from (4 default ) to 8 or 16. This can make huge impact on performance.

  • innodb_flush_log_at_trx_commit

Controls the balance between strict atomicity, consistency, isolation, and durability compliance for commit operations and higher performance that is possible when commit-related I/O operations are rearranged and done in batches. You can achieve better performance by changing the default value but then you can lose up to a second of transactions in a crash.

Keeping value to 2 means we can loose 1 sec worth of data as commits happens every sec regardless of size of transactions / redo but can improve i/o operations.

  • innodb_flush_method  

                      innodb_flush_method=O_DIRECT is recommended for OS caching for storage engine.  This will not use OS file cache.  

  • Partitions

Always use time range partitions to manage data deletions as it helps the big data to keep meta data stats up to date. It also helps indexing and data retrieval very fast.

 

Thanks for reading . Don’t forget to leave your feed backup.

Regards

Raja Naveed

Oracle VS MySQL file architecture

Oracle VS MySQL file architecture

For  Oracle database administrator, when it comes to learn new technology it always starts from comparison with respect to physical or logical architecture. Physical file architecture  comparison is given below , I hope it will be very useful for Oracle or MySQL database administrators who want to learn either of these databases.

Oracle database consist of following files

  • init. ora

Parameter file for database instance. It contains information about location of data, archive, redo and other log files. It also contains memory settings for the instance and remote locations.

  • Control file

It holds the information of each and every data file, redo log file locations and backup information starting time and ending time

  • Data file

It stores the data which includes user data and undo data. Extension of these files is “.dbf”. Names format and location of these files are defined in init.ora file.

  • Redo log file

It is part of an oracle database. It’s the main purpose is to recover the database. It’s extension “.log”

  • Archive log

It is a group of redo log files to one or more offline destinations, known collectively as the archived redo log. These logs contains all the transactions including redo statements which are applied to or can be applied to the database.

MySQL Database consists of following files:-

  • my.cnf

Parameter file for database instance. It contains information about location of data, archive, redo and other log files. It also contains memory settings for the instance and information about remote host for replication of data.

  • mysql-bin.index

This file contains information binary logs (archive logs in oracle called bin logs in MySQL). This information is stored in control file in oracle.

  • Redo log file

In MySQL ib_logfile0 and ib_logfile1 are called redo log files all transactions are stored before they are committed to disk. These transactions are translated into binary logs after commit. We can have more than 2 files in more than 1 groups as well.

  • Binary log ( archived logs in oracle)

These logs are same as archived logs in oracle. These files contains all the transactions or rollback statements committed to the database / disk. These files are translated into relay logs on remote destination for data replication but in oracle archived logs are created on remote destination to be applied on standby databases.

 

Thanks for visiting , dont forget to leave your feedback.
Regards
Raja Naveed

Connecting to MySQL using authenticated credentials login path using MySQL_config_editor

mysql_config_editor  (utility to configure authentication information for connecting to MySQL server)

Introduction:

Normally we connect to MySQL using username , password , socket  or host etc as shown below.

bash-4.2$  mysql -uroot -p  -h testmysql -S /var/log/mysql.sock 

Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 40974
Server version: 5.6.29-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql>

Every time when we would like to connect to mysql , we will have to use the string as shown above which is some time very frustrating. There is mysql_config_editor utility (available as of MySQL 5.6.6) enables you to store authentication credentials in an encrypted login path file named .mylogin.cnf . The file location is under mysql home dirctory unix or under %APPDATA%\MySQL on windows.

  • mysql_config_editor uses client login path defined to connect to mysql using  particular path name. for example –login-path=dba or –login-path=devuser or –login-path=root etc.
  • There is no default login-path for this utility.
  • Login path is always saved in .mylogin.cnf witch is encrypted and it gets decrypted when using login-path to connect to the instance.

we can use only following five  options with mysql_config_editor :-

  • set [command options] Sets user name/password/host name/socket/port for a given login path (section).
  • remove [command options] Remove a login path from the login file.
  • print [command options] Print all the options for a specified login path.
  • reset [command options] Deletes the contents of the login file.
  • help Display this usage/help information.

Setting up login-path:-

Lets create login path using  mysql_config_editor for local host  using set  option.

 

bash-4.2$ mysql_config_editor set --login-path=root --user=root --host=localhost --password --socket=/apps/mysql/mysql.sock
Enter password:

Now try to connect with mysql using log-path

bash-4.2$ mysql --login-path=root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 41218
Server version: 5.6.29-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql>

We can use print option to see how many login-paths have been set. We can use login-paths for other hosts as well .

 shell> mysql_config_editor print --all
[root]
user = root
password = *****
host = localhost
socket = /apps/mysql/mysql.sock
 [remote]
 user = remoteuser
 password = *****
 host = remote.example.com

if we want to remove a login path  we can using remove option as shown below

shell> mysql_config_editor remove --login-path=dba

finally the reset  option can be used to remove all the login-paths from .mylogin.cnf .

bash-4.2$ mysql_config_editor reset
bash-4.2$ mysql_config_editor print --all
bash-4.2$

 

Thanks for visiting , dont forget to leave your feedback.
Regards
Raja Naveed

Read files using arrays in Bash script

Read files using arrays in Bash script

I have following file with some numeric contents in it

bash-4.2$ cat /tmp/araytest.txt
1 2
3 4
5 6
7 8
9 10
11 12
13 14

If we would like save the file contents in Array. We can using following method.

 while read LINE
 do
 ARRAY+=("$LINE")
 done < <(cat /tmp/araytest.txt)

Now we can use another Loop to read it line by line for further manipulation as show below.

for i in "${ARRAY[@]}"; do
echo $i
done

Therefore full shell script should look like show below

#!/bin/bash

while read LINE
 do
 ARRAY+=("$LINE")
 done < <(cat /tmp/araytest.txt)

for i in "${ARRAY[@]}"; do
echo $i
done

Save it into file i-e testArray.sh and execute it as show below

bash-4.2$ ./testArray.sh
1 2
3 4
5 6
7 8
9 10
11 12
13 14

 

Thanks for visiting , dont forget to leave your feedback.
Regards
Raja Naveed

 

 

 

Fixing Broken GTID enabled replication ERROR 1858 (HY000):

Broken replication because of duplicate values MySQL 5.6 onward :

In non GTID enabled replication , we use to skip duplicate errors in case of Last_Errno: 1062 . But once we enable GTID we will have to deal it differently. The procedure is to follow the following simple steps

Let use assume two server  A as master B as slave.

  •  Check  slave status as shown below

mysql> show slave status\G
*************************** 1. row ***************************
 Slave_IO_State: Waiting for master to send event
 Master_Host: A
 Master_User: svc-mysql-repl
 Master_Port: 3306
 Connect_Retry: 60
 Master_Log_File: mysql-bin.000167
 Read_Master_Log_Pos: 72611417
 Relay_Log_File: relay-log.000364
 Relay_Log_Pos: 776
 Relay_Master_Log_File: mysql-bin.000161
 Slave_IO_Running: Yes
 Slave_SQL_Running: No
 Replicate_Do_DB:
 Replicate_Ignore_DB: admindb
 Replicate_Do_Table:
 Replicate_Ignore_Table: mysql.ibbackup_binlog_marker,mysql.backup_history,mysql.backup_progress,mysql.backup_sbt_history,mysql.inventory
 Replicate_Wild_Do_Table:
 Replicate_Wild_Ignore_Table:
 Last_Errno: 1062
 Last_Error: Error 'Duplicate entry '21555' for key 'PRIMARY'' on query. Default database: 'test'. Query: 'INSERT INTO auditlogs (text, category, datetime, username)
VALUES ('Starting application Web version 5.4', 'ACTION', '2017-07-26 09:49:41', '')'
 Skip_Counter: 0
 Exec_Master_Log_Pos: 611
 Relay_Log_Space: 5064799212
 Until_Condition: None
 Until_Log_File:
 Until_Log_Pos: 0
 Master_SSL_Allowed: No
 Master_SSL_CA_File:
 Master_SSL_CA_Path:
 Master_SSL_Cert:
 Master_SSL_Cipher:
 Master_SSL_Key:
 Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
 Last_IO_Errno: 0
 Last_IO_Error:
 Last_SQL_Errno: 1062
 Last_Error: Error 'Duplicate entry '21555' for key 'PRIMARY'' on query. Default database: 'test'. Query: 'INSERT INTO auditlogs (text, category, datetime, username)
VALUES ('Starting application Web version 5.4', 'ACTION', '2017-07-26 09:49:41', '')'
 Replicate_Ignore_Server_Ids:
 Master_Server_Id: 1
 Master_UUID: 4b7a0a70-146b-11e7-80f7-941882781532
 Master_Info_File: /opsrt12p/mysql/data/master.info
 SQL_Delay: 0
 SQL_Remaining_Delay: NULL
 Slave_SQL_Running_State:
 Master_Retry_Count: 86400
 Master_Bind:
 Last_IO_Error_Timestamp:
 Last_SQL_Error_Timestamp: 170727 08:59:35
 Master_SSL_Crl:
 Master_SSL_Crlpath:
 Retrieved_Gtid_Set: 4b7a0a70-146b-11e7-80f7-941882781532:1-87044138
 Executed_Gtid_Set: 4b7a0a70-146b-11e7-80f7-941882781532:1-82186600
 Auto_Position: 0
 Replicate_Rewrite_DB:
 Channel_Name:
 Master_TLS_Version:
1 row in set (0.00 sec)

In above example we can see two Gtid values which are

Executed_Gtid_Set : represents the last executed statement , The set of global transaction IDs written in the binary log. This is the same as the value for the global gtid_executed system variable on this server.

In above example we have following last executed Gtid value.

Executed_Gtid_Set: 4b7a0a70-146b-11e7-80f7-941882781532:1-82186600

and we have following master binlog file and position

 Master_Log_File: mysql-bin.000167 

 Read_Master_Log_Pos: 72611417

 

  •  If we try to skip the counter we will get following .
mysql> stop slave;

Query OK, 0 rows affected (0.00 sec) 

mysql> set global sql_slave_skip_counter=1;

ERROR 1858 (HY000): sql_slave_skip_counter can not be set when the server is running with @@GLOBAL.GTID_MODE = ON. 
Instead, for each transaction that you want to skip, generate an empty transaction with the same GTID as the transaction

 

  • Now we should check the entry int the mentioned binary log  at specific position  on server A (master server) and we need to check how many more steps needs to be executed from the perticular Gtid. using following commands.
    • create an temporary txt file using mysqlbinlog utility as shown
bash-4.2$ /apps/mysql/opsrt11p/install/bin/mysqlbinlog --base64-output=decode-rows --verbose mysql-bin.000167> /tmp/temp-binlog.txt
  •  Now view the temp-binlog.txt using less or any other utility  and find out set of statement executed under 4b7a0a70-146b-11e7-80f7-941882781532:1-82186600 Gtid at step number 82186600 there may be few more steps to be executed therefore we need to skip step number 82186600 and move to next step of the statement under particular Gtid. 
  • Skip to next step of Gtid using following statement.
mysql> set GTID_NEXT="4b7a0a70-146b-11e7-80f7-941882781532:82186601;

Query OK, 0 rows affected (0.00 sec)

mysql> start slave;
  • Now do show slave status again and you will see statement has been skipped and there are no duplicate errors any more.

 

Thanks for visiting , please dont forget to leave you feedback

Regards

Raja Naveed

 

 

Split file into multiple files using split command line utility on unix / linux

Split Command in Unix / Linux:

This utility / command in unix or linux helps to split the file into two or more files. We have multiple options to use the command as per our needs.

Syntax :

split [filename] [INPUT [PREFIX] ]

where
[filename] is the name of file we want to split.
[INPUT [PREFIX]]  is optional prefix for the splited files.
for example if we have file name called “abc” then usually we will keep Prefix as “abc” so that it can be divided into files are called abc0 , abc1, abc2 … abc[N]

OPTIONAL

-a, --suffix-length=N              use suffixes of length N (default 2)
-b, --bytes=SIZE              put SIZE bytes per output file
-C, --line-bytes=SIZE              put at most SIZE bytes of lines per output file
-d, --numeric-suffixes              use numeric suffixes instead of alphabetic
-l, --lines=NUMBER              put NUMBER lines per output file
 --verbose              print a diagnostic just before each output file is opened

Examples 1 :

Lets create a file which will contain 100 lines using following command

for i in {1..100} ; do echo "This is line number $i ">>test-split.txt; done
bash-4.1$ ls
test-split.txt

Now split the file into 10 files each will contain

split -l 10 test-split.txt test-split.txt -d

Note:  with -d option each file will have digit in suffix as shown below but if the number of files could go beyond 99 or more than 2 diget number we can us -a3 or -aN option for suffix length where N is length of suffix (default is -a2)

therefore file is split into 10 as shown below

bash-4.1$ ls
test-split.txt test-split.txt00 test-split.txt01 test-split.txt02 test-split.txt03 test-split.txt04 
test-split.txt05 test-split.txt06 test-split.txt07 test-split.txt08 test-split.txt09

Example 2.

Lets create a bigger file of 10M and split it with respect to size in MB.

bash-4.1$ ls -ltrh
total 10M  

-rw-r-----. 1 rmn users 10M Jul 20 15:13 test-split.txt

Now we split this 10M file into 10 equal size files of 1M appx using following command.

bash-4.1$ split -b 1M test-split.txt test-split.txt -d

File have been splited into 10 files as show below

bash-4.1$ ls -ltrh
total 20M
-rw-r-----. 1 rmn     users 10M Jul 20 15:13 test-split.txt
-rw-r-----. 1 rmn     users 1.0M Jul 20 15:17 test-split.txt00
-rw-r-----. 1 rmn     users 1.0M Jul 20 15:17 test-split.txt01
-rw-r-----. 1 rmn     users 1.0M Jul 20 15:17 test-split.txt02
-rw-r-----. 1 rmn      users 1.0M Jul 20 15:17 test-split.txt03
-rw-r-----. 1 rmn     users 1.0M Jul 20 15:17 test-split.txt04
-rw-r-----. 1 rmn     users 1.0M Jul 20 15:17 test-split.txt05
-rw-r-----. 1 rmn     users 1.0M Jul 20 15:17 test-split.txt06
-rw-r-----. 1 rmn     users 1.0M Jul 20 15:17 test-split.txt07
-rw-r-----. 1 rmn     users 1.0M Jul 20 15:17 test-split.txt08
-rw-r-----. 1 rmn     users 927K Jul 20 15:17 test-split.txt09

If you want to split the file in bytes then command will be as followed

bash-4.1$ split -b1000 test-split.txt test-split.txt -d

Finally how to join all back to make one single file ? it is very simple just use cat command line utility as shown belown

bash-4.1$ cat test-split.txt00 test-split.txt01 test-split.txt02 test-split.txt03 test-split.txt04 test-split.txt05 test-split.txt06 test-split.txt07 test-split.txt08 test-split.txt09 >> test-split.txt

 

Thanks for reading article I hope you liked it .. Keep visiting and don’t forget to give feedback