MySQL/MariaDB Memory

In this blog post, I’m gonna talk about MySQL Memory Issues. What causes High memory usage and what is the impact on MySQL database instances.

But first, we can do a quick review on the following topics:

  • How MySQL uses memory? (Log files, buffers, InnoDB buffer pool size)
  • How MySQL allocates memory?
  • Swap memory
  • Troubleshooting
  • Best practices.
Globally shared memory is allocated upon the creation of an instance and is shared by all connections
Example: thread cache, query cache, buffer pool, and log buffer.

Private memory is used to allocate cache upon connection to MySQL server. It is allocated for each thread. For example; sort buffer, join buffer, and temporary table.

InnoDB maintains one or more buffer pools that cache frequently used data and indexes in the main memory.

So, when a read query is executed from the client program, InnoDB checks to see if the required data pages are in the buffer pool. If it is not in the buffer pool, InnoDB requests the data from the tablespace. Then it will put the data pages in the buffer pool. And then, MySQL will return the results to the client.

 
When transactions are performed, data changes are written to the log buffer in memory. then every time a transaction is committed, the transaction logs get flushed to the disk, but this behavior can be changed (InnoDB_flush_log_at_trx_commit). If a crash occurs (while the tables are being modified), the redo log files are used for recovery.
 
On the other hand, Dirty pages in the buffer pool get written to the disk at checkpoints. This is to ensure that the cache has enough free space.

innodb_buffer_pool_size is the most important tuning parameter. It caches table and index data. The buffer pool permits frequently used data to be accessed directly from memory, which speeds up processing.

Setting it too low can degrade the performance. Setting it too high can increase the memory consumption causing the DB to crash.

MySQL allocation is not only from innodb_buffer_pool_size, but also from other buffers in the database such sort_buffer_size, read_buffer_size, read_rnd_buffer, join_buffer_size, and tmp_table_size and this will require additional 5-10% extra memory

Below is a formula to calculate the approximate memory usage for your MySQL:

Maximum MySQL Memory Usage = innodb_buffer_pool_size + key_buffer_size + ((read_buffer_size + read_rnd_buffer_size + sort_buffer_size + join_buffer_size) X max_connections)

Swapping can happen when a system requires more memory than is allocated .A large buffer may lead to swapping in the operating system and make the performance slow.

To avoid your MySQL/MariaDB data being SWAP instead of RAM, you have to play with a kernel parameter called swappiness.

A swappiness value is used to change the balance between swapping out runtimememory and dropping pages from the system page cache. The higher the value, the more the system will swap. The lower the value, the less the system will swap. The maximum value is 100, the minimum is 0, and 60 is the default.

I performed a test in AWS RDS MySQL. I ran a stored procedure that consumed a lot of memory until the server crashed. We will notice that when memory gets low, Swap usage will increase. We will also see a spike in disk IO usage.

When MySQL crashed, we will lose connection to the database. In AWS RDS, we can see the logs and EVENTs to identify the cause of the crash. The log indicates that the database process was killed by the OS due to excessive memory consumption.

I also did the same test in Azure MySQL. The difference between Azure and AWS is that when the DB crash, in Azure, it will failover to the Standby replica, but in AWS, a mysqld process crash would not trigger a failover.

In Azure, under Private DNS Zone, if we see that the IP address changed, then it means that it failed over to the standby replica successfully.

Cheers!

Knowledge worth sharing...Share on linkedin
Linkedin
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter

MySQL/MariaDB – FLUSH TABLES WITH READ LOCK

To block all write access and to mark all tables as ‘properly closed’ on disk, we execute the FLUSH TABLES WITH READ LOCK. Note that the tables can still be used for read operations. Open tables will be closed and all tables will be locked for all databases with a global lock.

FLUSH TABLES and RELOAD privilege are required to execute this operation.

To release the lock, you may use UNLOCK TABLES, which implicitly commits any active transaction only if any tables currently have been locked with the LOCK TABLES. If the UNLOCK TABLES is executed after FLUSH TABLES WITH READ LOCK, then the commit will not occur because FLUSH TABLES WITH READ LOCK does not acquire table locks.

Cheers!

Knowledge worth sharing...Share on linkedin
Linkedin
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter

Redis – Predixy Proxy Auto Setup

Here is a shell script I wrote to automate the setup of Predixy Proxy in Centos 7.

This script will work if you have a single endpoint (i.e. AWS Elasticache cluster configuration endpoint or a standalone Elasticache endpoint).

#!/bin/bash

# Created By  : Enrique Valencia 
# Script Name : predixy_auto_setup.sh
# Description : Predixy Automatic Setup

################################################################################################################
# Usage	          : ./auto_predixy_setup.sh <configuration_end_point>"
# Example         : ./auto_predixy_setup.sh dba-redis-test01.bbssxp.clustercfg.apse1.cache.amazonaws.com:6379"
# To Check Status : systemctl status predixy 
# To Start	      : systemctl start predixy
# To Stop		  : systemctl stop predixy
# To Restart	  : systemctl restart predixy
################################################################################################################

################################################################################################################
##### Validate Usage
################################################################################################################

if [ $# -ne 1 ]
then
  echo ""
  echo "************************************************************************************************************"
  echo "Usage:   ./predixy_auto_setup.sh <configuration_end_point>"
  echo ""
  echo "Example: ./predixy_auto_setup.sh dba-redis-test01.bbssxp.clustercfg.apse1.cache.amazonaws.com:6379"
  echo "************************************************************************************************************"
  echo ""
  exit 1
fi

################################################################################################################
##### Variable
################################################################################################################

ENDPOINT=$1

################################################################################################################
##### Install Predixy
################################################################################################################

InstallPredixy(){
	mkdir /predixy_data
	cd /predixy_data
	sudo yum install git -y
	sudo yum install libstdc++-static -y
	yum -y install gcc
	yum -y install gcc-c++ 
	sudo git clone https://github.com/joyieldInc/predixy.git
	cd predixy
	sudo make
	mkdir -p /predixy_data/predixy/log
}

################################################################################################################
##### Cluster
################################################################################################################

cluster(){

cat >> /predixy_data/predixy/conf/cluster.conf << EOF
ClusterServerPool {
	MasterReadPriority 100  # 100 
	# Password sjwkk123456 # redis 
	StaticSlaveReadPriority 0  #  redis slave redis 0
	DynamicSlaveReadPriority 0 #  redis sentinel 0
	RefreshInterval 1 # predixy redis sentinel 1 
	ServerTimeout 1 #  predixy/redis predixy redis blpop 0 redis 0
	ServerFailureLimit 10 #  redis 10
	ServerRetryTimeout 1 #  redis 1 
	KeepAlive 120 #predixy redis tcp keepalive 0 0
	Servers {
	##  
		+ $ENDPOINT
	}
}
EOF

	sed -i 's/Auth "#a complex password#" {/Auth "123456" {/' auth.conf
	sed -i 's/WorkerThreads 1/WorkerThreads 4/' /predixy_data/predixy/conf/predixy.conf
	sed -i 's/# Log .\/predixy.log/Log \/predixy_data\/predixy\/log\/predixy.log/' /predixy_data/predixy/conf/predixy.conf
	sed -i '/# LogRotate 1d 2G/d' /predixy_data/predixy/conf/predixy.conf
	sed -i 's/# LogRotate 1d/LogRotate 1d/' /predixy_data/predixy/conf/predixy.conf
	sed -i 's/Include try.conf/# Include try.conf/' /predixy_data/predixy/conf/predixy.conf
	sed -i 's/# Include cluster.conf/Include cluster.conf/' /predixy_data/predixy/conf/predixy.conf
}


################################################################################################################
##### Standalone
################################################################################################################

standalone(){
	
cat >> /predixy_data/predixy/conf/standalone.conf << EOF

StandaloneServerPool {
	RefreshMethod fixed
	Group shard001 {
		+ $ENDPOINT
	}
}
EOF

	sed -i 's/Auth "#a complex password#" {/Auth "123456" {/' auth.conf
	sed -i 's/WorkerThreads 1/WorkerThreads 4/' /predixy_data/predixy/conf/predixy.conf
	sed -i 's/# Log .\/predixy.log/Log \/predixy_data\/predixy\/log\/predixy.log/' /predixy_data/predixy/conf/predixy.conf
	sed -i '/# LogRotate 1d 2G/d' /predixy_data/predixy/conf/predixy.conf
	sed -i 's/# LogRotate 1d/LogRotate 1d/' /predixy_data/predixy/conf/predixy.conf
	sed -i 's/Include try.conf/# Include try.conf/' /predixy_data/predixy/conf/predixy.conf
	sed -i 's/# Include sentinel.conf/Include standalone.conf/' /predixy_data/predixy/conf/predixy.conf
}

################################################################################################################
##### Setup Predixy as a Systemd Service
################################################################################################################

systemd(){
	echo "/predixy_data/predixy/src/predixy /predixy_data/predixy/conf/predixy.conf" > /usr/sbin/predixy_start.sh

	cat > /etc/systemd/system/predixy.service <<-EOF
	[Unit]
	Description=Predixy

	[Service]
	ExecStart=/bin/bash /usr/sbin/predixy_start.sh


	[Install]
	WantedBy=multi-user.target
	EOF

	sudo chmod 640 /etc/systemd/system/predixy.service
	sudo systemctl daemon-reload
	sudo systemctl enable predixy
}


################################################################################################################
##### Check Predixy Status
################################################################################################################

CheckStatus() {
	if [ `ps -ef | grep predixy.conf | grep -v grep | wc -l` -gt 0 ]; then
		echo ""
		echo "************************************************************************"
		echo "Predixy Setup Completed."
		echo "************************************************************************"
		echo ""
	else
		echo ""
		echo "************************************************************************"
		echo "Predixy Proxy is unable to start. Please check."
		echo "************************************************************************"
		echo ""
		exit 1
	fi
}

################################################################################################################
##### MAIN
################################################################################################################

echo -ne "
Menu: 
=====

1) cluster
2) standalone
3) Exit

Please choose: "

read -r choice

case $choice in

1)
	echo ""
	echo "************************************************************************"
	echo "You chose 1 - Cluster. Starting Predixy Setup."
	echo "************************************************************************"
	echo ""
	sleep 3
	InstallPredixy
	cluster
	systemd
	systemctl start predixy
	sleep 3
	CheckStatus
	;;

2)
	echo ""
	echo "************************************************************************"
	echo "You chose 2 - Standalone. Starting Predixy Setup."
	echo "************************************************************************"
	echo ""
	sleep 3
	InstallPredixy
	standalone
	systemd
	systemctl start predixy
	sleep 3
	CheckStatus
	;;
	
3)
	echo ""
	echo "*********"
	echo "再见"
	echo "*********"
	echo ""
	exit 1
	;;
*)
	echo ""
	echo "************************************************************************"
	echo "Invalid Option. Please try again."
	echo "************************************************************************"
	echo ""
	exit 1
esac

Cheers!

Knowledge worth sharing...Share on linkedin
Linkedin
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter

Redis – Monitor Redis Using Grafana

In this blog post, I will outline the steps to set up Grafana to monitor your Redis database.

Install Grafana

Go to Grafana website download page.

Example: For Red Hat, CentOS, RHEL, and Fedora(64 Bit).

Execute the following commands to make Grafana start automatically when the server is booted up.

sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable grafana-server.service

Install Redis Plugin

The Redis Data Source for Grafana is a plugin that allows users to connect to any Redis database On-Premises and in the Cloud. It provides out-of-the-box predefined dashboards and lets you build customized dashboards to monitor Redis and application data.

grafana-cli plugins install redis-datasource

Start Grafana

Start Grafana.

sudo systemctl start grafana-server.service

Check Grafana Status.

sudo systemctl status grafana-server.service

Access Grafana Monitor Dashboard

Paste the server’s IP, plus port 3000 on your web browser to access Grafana Dashboar<Ipd.

<ServerIPaddress>:3000

The initial username and password is admin/admin.

Configure Data source

Go to Configuration > Data sources.

Add data source.

Search for “Redis“, then select the data source below.

Enter a name and address in the following format.

redis://your-redis-endpoint.ng.0001.sae1.cache.amazonaws.com:6379

Click Save and test and verify that the data source is working as expected.

Import Dashboard

This step will be performed one time only.

Go to Dashboards. Then click Import for both Redis and Redis Streaming.

View Dashboard

Go to Dashboard > Manage.

Choose Redis or Redis Streaming.

Knowledge worth sharing...Share on linkedin
Linkedin
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter