Bug #92224 Use "Environment" to set LD_PRELOAD in systemctl is not working
Submitted: 29 Aug 2018 10:46 Modified: 17 Dec 2019 10:00
Reporter: chen chen Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Installing Severity:S3 (Non-critical)
Version:8.0.12 OS:Any (7.4)
Assigned to: CPU Architecture:x86

[29 Aug 2018 10:46] chen chen
Description:
Here it is.

#### Basic info
[root@node2 ~]# cat /etc/redhat-release 
Red Hat Enterprise Linux Server release 7.4 (Maipo)
[root@node2 ~]# uname -a
Linux node2 3.10.0-693.el7.x86_64 #1 SMP Thu Jul 6 19:56:57 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux

[root@node2 ~]# yum install gperftools -y

[root@node2 ~]# rpm -ql gperftools-libs-2.6.1-1.el7.x86_64
/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

[root@node2 ~]# lsof -n | grep tcmalloc

### Set the LD_PRELOAD explicitly.
[root@node2 ~]# export LD_PRELOAD=/usr/lib64/libtcmalloc_minimal.so.4
[root@node2 ~]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service

check whether the tcmalloc come into effect
[root@node2 ~]# lsof -n | grep tcmalloc
lsof      2363         root  mem       REG              253,0    154776     339628 /usr/lib64/libtcmalloc_minimal.so.4.4.5
grep      2364         root  mem       REG              253,0    154776     339628 /usr/lib64/libtcmalloc_minimal.so.4.4.5
lsof      2365         root  mem       REG              253,0    154776     339628 /usr/lib64/libtcmalloc_minimal.so.4.4.5

As we can see,the tcmalloc come into effect.

Now,let's check the effect if we set LD_PRELOAD in systemd configuration file.

### Set LD_PRELOAD in systemd configuration file
https://dev.mysql.com/doc/refman/8.0/en/using-systemd.html#mysqld-safe-to-systemd-migratio...

Open another console

[root@node2 ~]# echo $LD_PRELOAD

[root@node2 ~]# systemctl edit mysqld
Environment="LD_PRELOAD=/usr/lib64/libtcmalloc_minimal.so.4"

[root@node2 ~]# cat /etc/systemd/system/mysqld.service.d/override.conf
Environment="LD_PRELOAD=/usr/lib64/libtcmalloc_minimal.so.4"

[root@node2 ~]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service

[root@node2 ~]#  lsof -n | grep tcmalloc

there is no output.

i guess the tcmalloc did't come into effect

How to repeat:
As stated above
[31 Aug 2018 11:05] Terje Røsten
Hi!

Thanks for your report!

You are right, indeed setting LD_PRELOAD=... environment variable don't
take effect. This is due to this scriptlet in the RPM spec file:

 /usr/sbin/setcap cap_sys_nice+ep /usr/sbin/mysqld

this has the side effect that the binary is suid as seen from
systemd, and systemd disable LD_PRELOAD for suid executables.

You can verify this by doing:

$ setcap -r /usr/sbin/mysqld

Note: removing CAP_SYS_NICE capability will disable the Resource Group feature:
 https://dev.mysql.com/doc/refman/8.0/en/resource-groups.html
[17 Dec 2019 10:00] Terje Røsten
Hi!

This issue was fixed by this commit:

https://github.com/mysql/mysql-server/commit/d10ff5ac64088119164572cbca729516dcb893de