Quantcast
Channel: Planet MySQL
Viewing all articles
Browse latest Browse all 18800

Enabling jemalloc on Percona Server for MySQL 5.7 and 8.0 Series

$
0
0
jemalloc on Percona Server for MySQL

jemalloc on Percona Server for MySQLThe benefits of jemalloc versus glibc memory allocator for use with MySQL have been widely discussed. With jemalloc (along with Transparent Huge Pages disabled) there is less memory fragmentation, and thus more efficient resource management of the server memory. For MySQL 5.6, installing jemalloc is enough to enable it when starting the MySQL process. However, for MySQL 5.7 and 8.0.X series, you will need to take a few extra steps.

Enabling jemalloc on Percona Server for MySQL

Installing the jemalloc package is simple for Percona. This is because the library is available on the Percona repository, which is available for both apt and yum package management:

Once you have the repo, just run the install command (according to your OS) to install it:

Now that the package is installed, we need to make MySQL working with it. And now, it depends on how you are starting MySQL:

yum install jemalloc / apt-get install libjemalloc1

Using systemd Services (systemctl Command)

If you are using systemd services (using systemctl commands), you need to execute the following steps:

1-) The unit file provided with Percona Server (/usr/lib/systemd/system/mysqld.service) has the following lines in it:

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

The /etc/sysconfig/mysql  file should contain new-line-separated variable assignments. See systemd docs https://www.freedesktop.org/software/systemd/man/systemd.exec.html#EnvironmentFile= for the explanation. So the final result will be a file like this:

# cat /etc/sysconfig/mysql
LD_PRELOAD=/usr/lib64/libjemalloc.so.1

2-) Start MySQL

systemctl start mysql

Starting Manually

If you are starting it manually, in the command line, you need to specify the library in the LD_PRELOAD. So the command to initialize will be similar to this one:

LD_PRELOAD=/usr/lib64/libjemalloc.so.1 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid --user mysql

Check if MySQL is Using jemalloc

[root@localhost ~]# pt-mysql-summary | grep -A5 -i "memory management"
# Memory management library ##################################
jemalloc enabled in mysql config for process with id 14490
Using jemalloc from /usr/lib64/libjemalloc.so.1
# The End ####################################################

And you can also do it manually:
pid=$(pidof mysqld)
cat /proc/${pid}/environ

Or:

lsof -Pn -p $(pidof mysqld) | grep jemalloc

Conclusion

This blog post was written to update the guidelines of jemalloc. Important to mention that using the recommendations above regarding systemd, the changes will persist across MySQL upgrades.

Finally, jemalloc still is a good memory allocator and I consider it part of the best practices that should be implemented before the deployment in production.

Useful Resources

Finally, you can reach us through the social networks, our forum, or access our material using the links presented below:


Viewing all articles
Browse latest Browse all 18800

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>