The max_connections in MySQL 5.7 - mysql

I met a problem, the value of max_connction in MySQL is 214 after I set it 1000 via edit the my.cnf, just like below:
hadoop#node1:~$ mysql -V
mysql Ver 14.14 Distrib 5.7.15, for Linux (x86_64) using EditLine wrapper
MySQL version: 5.7
OS version : ubuntu 16.04LTS
mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set (0.00 sec)
As we can see, the variable value of max_connections is 151. Then , I edit the configuration file of MySQL.
yang2#node1:~$ sudo vi /etc/mysql/my.cnf
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
max_connections=1000
Restart MySQL service after save the configraion.
yang2#node1:~$ service mysql restart
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'mysql.service'.
Multiple identities can be used for authentication:
1. yangqiang,,, (yang2)
2. ,,, (hadoop)
Choose identity to authenticate as (1-2): 1
Password:
==== AUTHENTICATION COMPLETE ===
yang2#node1:~$
Now, we guess the max_connection is 1000, really?
mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 214 |
+-----------------+-------+
1 row in set (0.00 sec)
It is 214. I do not really understand this result, who can help me? thx!

You may set the value manually, e.g.
set global max_connections=500;
however, after a restart of MySQL the value is reset to 214.
The solution depends on the (version of) OS and the MySQL version. With Ubuntu 16.04 and MySQL >= 5.7.7 following works:
systemctl edit mysql
Enter
[Service]
LimitNOFILE=8000
save, this will create a new file
/etc/systemd/system/mysql.service.d/override.conf
and restart the server:
systemctl daemon-reload
systemctl restart mysql
For other environments: Can not increase max_open_files for Mysql max-connections in Ubuntu 15

As MySQL documentation on max_connections setting says:
Increasing this value increases the number of file descriptors that
mysqld requires. If the required number of descriptors are not
available, the server reduces the value of max_connections.
This means that probably your MySQL server does not have enough resources to maintain the required number of descriptors.
MySQL documentation on How MySQL Opens and Closes Tables makes it clear that:
The table_open_cache and max_connections system variables affect the
maximum number of files the server keeps open. If you increase one or
both of these values, you may run up against a limit imposed by your
operating system on the per-process number of open file descriptors.
Many operating systems permit you to increase the open-files limit,
although the method varies widely from system to system. Consult your
operating system documentation to determine whether it is possible to
increase the limit and how to do so.

Follow the following steps:
cp /lib/systemd/system/mysql.service /etc/systemd/system/
echo -e "\r\nLimitNOFILE=infinity" >> /etc/systemd/system/mysql.service
echo "LimitMEMLOCK=infinity" >> /etc/systemd/system/mysql.service
sudo systemctl daemon-reload
sudo service mysql restart
And change or add he following line into file /etc/mysql/mysql.conf.d/mysqld.cnf :
[mysqld]
max_connections=110
Just this!
#Mahdi_Mohammadi

1.Edit mysql service file
#sudo cat /etc/systemd/system/multi-user.target.wants/mysql.service
# MySQL systemd service file
[Unit]
Description=MySQL Community Server
After=network.target
[Install]
WantedBy=multi-user.target
[Service]
Type=forking
User=mysql
Group=mysql
PIDFile=/run/mysqld/mysqld.pid
PermissionsStartOnly=true
ExecStartPre=/usr/share/mysql/mysql-systemd-start pre
ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid
TimeoutSec=600
Restart=on-failure
RuntimeDirectory=mysqld
RuntimeDirectoryMode=755
##this bellow for tuneup
LimitNOFILE=infinity
LimitMEMLOCK=infinity
2.edit /etc/mysql/mysql.conf.d/mysqld.cnf
max_connections = 99999

Add session required pam_limits.so in /etc/pam.d/common-session (usually is not present by default).
The in /etc/security/limits.conf you can add some limits:
* hard nofile 8192
* soft nofile 4096
Also check using ulimit -a the open files limit.
This you can increase with ulimit -n 4096
Make sure you reboot at the end.

Related

How do I correctly restart mysql so that changes in the `*.cnf`-files get active?

I try to configure mysql as mentioned here: https://websiteforstudents.com/install-erpnext-erp-platform-on-ubuntu-16-04-18-04-lts-with-nginx-mariadb-support/ on a Linux Mint 18.2 system.
I added innodb-file-format=barracuda to the [mysqld] section in /etc/mysql/mariadb.conf.d/50-server.cnf and restarted the mysql service. But these settings are taking no effect.
If I run following commands
sudo mysql -u root -p
SHOW VARIABLES LIKE 'innodb_file_format';
then I get following output.
+--------------------+----------+
| Variable_name | Value |
+--------------------+----------+
| innodb_file_format | Antelope |
+--------------------+----------+
The settings file ~/.my.cnf is not existing.
Why are my settings taking no effect?
++++++++++++ UPDATE 2019-06-03 ++++++++++++
I think I found the issue now. /etc/mysql/mariadb.conf.d/50-server.cnf is the correct logfile and the settings were not overwritten by another *.cnf-file. After a restart of the PC, the changed settings were used. The problem is, that restarting the mysql service (sudo service mysql restart or sudo /etc/init.d/mysql restart) is not enough.
So the question should be:
How do I correctly restart mysql so that changes in the *.cnf-files get active?
First, you should try to find my.cnf in other locations like
How do I find the MySQL my.cnf location
If no luck, scan all *.cnf files in /etc/mysql/mariadb.conf.d/ because Mariadb may read all *.cnf as config in that folder
Therefore you may add this value on 50-server.cnf, but it is overwritten in another files
Hope this helps
Don't do this:
systemctl restart mysqld
Instead, do:
systemctl stop mysqld
systemctl start mysqld
Not sure exactly what the syntax is for Debian's 'service' command but I believe that's just a wrapper around systemctl. Syntax should be easily found. Change the service name if your system uses something different, of course. I have found that for some services, doing a restart doesn't seem to reread the config files, while a stop and then start forces a complete reload and config file read.

Changes to my.cnf don't take effect (Ubuntu 16.04, mysql 5.6)

Changes I make to my.cnf don't seem to have any effect on the mysql environment. Here's a summary of what's happened...
I installed mysql 5.7 on Ubuntu 16.04 but then realized I needed to downgrade to mysql 5.6 due to incompatibility issues.
I apt purged the related applications and then removed any remaining directories such at /etc/mysql and /var/lib/mysql
I then installed mysql-5.6 (server and client) and related packages.
I was able to load one database from a dump from a server also running mysql 5.6 but when I tried to load a second database from a second dump from that same server, I got this error:
ERROR 2006 (HY000) at line 1721: MySQL server has gone away
When I Googled that, I saw results saying to set various options via the my.cnf file.
When I run...
updatedb && locate my.cnf
...I only see four results which are all links back to the same file: /etc/mysql/my.cnf.fallback. E.g. /etc/mysql/my.cnf.fallback == /etc/mysql/my.cnf
There are no .my.cnf files in either the root home directory or my user's home directory. I put a typo into the my.cnf file and reloaded mysql just to see the expected error and know the file was being loaded. I then removed the erroneous code and added the following:
[mysqld]
max_allowed_packet=1073741824
I then reloaded mysql by running in various ways:
service mysql restart
or
service mysql stop
service mysql start
or
/etc/init.d/mysql stop
/etc/init.d/mysql start
I then kept getting this default value indicating that it was not getting set from my.cnf:
mysql> SHOW VARIABLES LIKE 'max_allowed_packet';
+--------------------+---------+
| Variable_name | Value |
+--------------------+---------+
| max_allowed_packet | 4194304 |
+--------------------+---------+
If I do this:
mysql> SET GLOBAL max_allowed_packet=1073741824;
and log out and back into the mysql client, I see the correct value:
mysql> SHOW VARIABLES LIKE 'max_allowed_packet';
+--------------------+------------+
| Variable_name | Value |
+--------------------+------------+
| max_allowed_packet | 1073741824 |
+--------------------+------------+
But of course, if I restart the mysql server, the value reverts.
I've exhausted my search ability. What can I possibly be doing wrong?
The config files are fine. The root cause is a bug in the MySQL 5.6 packaging for Ubuntu 16.04.
If you check your /var/log/syslog you'll probably see a line like this:
Sep 15 18:56:09 ip-172-31-18-162 kernel: [ 383.840275] audit: type=1400 audit(1505501769.234:50): apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/etc/mysql/my.cnf.fallback" pid=25701 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=0 ouid=0
A security tool called AppArmor is denying access to a symlinked file (/etc/mysql/my.cnf.fallback).
Try this workaround, which will allow symlinks to be read by mysqld.
echo '/etc/mysql/** lr,' >> /etc/apparmor.d/local/usr.sbin.mysqld
systemctl reload apparmor
Now mysqld should see your custom config.
This bug appears to be fixed in the MySQL 5.7 Ubuntu package.
Obviously my.cnf.fallback is not the correct configuration file.
If you try this commands you can get output for possible my.cnf locations:
$ which mysqld
/usr/sbin/mysqld
$ /usr/sbin/mysqld --verbose --help | grep -A 1 "Default options"
Default options are read from the following files in the given order:
/etc/mysql/my.cnf ~/.my.cnf /usr/etc/my.cnf
It means mysql will check those locations for my.cnf file. Simply rename /etc/mysql/my.cnf.fallback as /etc/mysql/my.cnf:
mv /etc/mysql/my.cnf.fallback /etc/mysql/my.cnf

cannot increase open-files-limit in mariadb 10 on centos7

I searched about the topic subject and tested options, but I still cant increase the open-files-limit on my mariadb server that is used as remote database server for cpanel/whm server.
here is s good reference
http://duntuk.com/how-raise-ulimit-open-files-and-mysql-openfileslimit
I increased it in
/etc/my.cnf
open-files-limit=65550
here is some results
#ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 63471
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 65535
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 65535
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
#ulimit -Hn -Sn
open files (-n) 65535
open files (-n) 65535
cat /etc/systemd/system/mariadb.service.d/limits.conf
[Service]
LimitNOFILE=65500
cat /usr/lib/systemd/system/mariadb.service
[Unit]
Description=MariaDB database server
After=syslog.target
After=network.target
[Service]
Type=simple
User=mysql
Group=mysql
LimitNOFILE=infinity
LimitMEMLOCK=infinity
and still in mysql I get
show global variables like 'open%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| open_files_limit | 1024 |
+------------------+-------+
1 row in set (0.00 sec)
and im getting following error over and over after restart
60108 16:30:02 [ERROR] mysqld: Can't open file: './username_wp/wp_users.frm' (errno: 24)
160108 16:30:02 [ERROR] Error in accept: Too many open files
160108 16:30:04 [ERROR] Error in accept: Too many open files
160108 16:30:06 [ERROR] Error in accept: Too many open files
160108 16:30:11 [ERROR] mysqld: Can't open file: './username_db/strikes.frm' (errno: 24)
and here is the startup log of mariadb
[Warning] Could not increase number of max_open_files to more than 1024 (request: 132107)
RH/CentOS mariadb integration is quiet confusing, using in some places mariadb as its name, mysqld elsewhere...
You should edit(or create) a .conf file in
/etc/systemd/system/mariadb.service.d/
For eg: /etc/systemd/system/mariadb.service.d/centreon.conf
Then edit it as kujiy suggested
[Service]
LimitNOFILE=320000
Don't forget to reload systemd services files:
# systemctl daemon-reload
Cedric
This is an OS problem.
I think you have to increase the "hard limit" in /etc/security/limits.conf, something like this:
* hard nofile 65536
* soft nofile 16384
then use ulimit to increase the "soft limit".
finally i find solution but without any reasonable cause !
i was running MariaDB 5.5 and all settings where fine but the soft limit did not goes more than 1024 !
i was thinking my mariadb is 10.0 as i freshly installed it
after i find out that its 5.5 i tried to upgrade it to v10.0 ! and then BOOM ! problem solved without any extra action or setting ( kernel setting allows maximum files limit as described in reference link )
i hope it help other peoples have same problem ( but after try all other required settings )
You can see the official instruction in the mariadb.service file;
[root#db1 system]# cat /usr/lib/systemd/system/mariadb.service | grep exam -A 5
# For example, if you want to increase mariadb's open-files-limit to 10000,
# you need to increase systemd's LimitNOFILE setting, so create a file named
# "/etc/systemd/system/mariadb.service.d/limits.conf" containing:
# [Service]
# LimitNOFILE=10000
Though I think this should be written in the official manual...
I have CentOS 7.3 with MariaDB 10.0.29 installed from official repo.
For some reason, systemd unit is named mysql and I had to create /etc/systemd/system/mysql.service.d/oioki.conf:
[Service]
LimitNOFILE=500000
Don't forget to run systemctl daemon-reload after that.
open_files_limit is dynamic value that depends on next params:
#my.cnf file
# max connections
max_connections = 64
# table_open_cache = max_connections * tables used in one transaction + 5
table_open_cache = 800
# table_definition_cache = all tables(50) + max_connections + 5
table_definition_cache = 400
# open_files_limit = table_open_cache * 1.4
open_files_limit = 1120
To check the value of open_files_limit run this command from mysql:
mysql> select ##open_files_limit;
...it will output:
+--------------------+
| ##open_files_limit |
+--------------------+
| 12903 |
+--------------------+
1 row in set (0.000 sec)
I had this problem running RedHat Enterprise Linux 7.2 with Software Collections (SCL) and SystemD
rh-mariadb100-mariadb-server-10.0.20-1.el7.x86_64
In my case the hard limit was ok, but the soft limit was stuck on 1024
Summary of the old and new
old : RHEL6.x : MySQL : init.d : mysql_safe : 'root', mysqld : 'mysql'
new : RHEL7.x : MariaDB : SystemD : mysql_safe : 'mysql', mysqld 'mysql'
Note that the old initl.d wrapper script runs as root, but the new SystemD wrapper script runs as 'mysql'. This can cause mysql_safe to not make the call to ulimit. In my case setting LimitNOFILE in SystemD didn't help - all it did was increase the file limit for the wrapper, but NOT for the daemon itself.
I found a two step process to get it working
Step 1 - re-instate the mysqld_safe section to the conf file so that mysql_safe will read it
sudo vi /etc/opt/rh/rh-mariadb100/my.cnf.d/mariadb-server.cnf
[mysqld_safe]
open_files_limit=2048
Step 2 - create a SystemD drop-in to start the wrapper as root (the daemon will still run as 'mysql'). Note that LimitNOFILE is commented out.
/etc/systemd/system
mkdir rh-mariadb100-mariadb.service.d
cd rh-mariadb100-mariadb.service.d
vi limits.conf
[Service]
#LimitNOFILE=2048
User=
Group=
Reload SystemD service files
systemctl daemon-reload
now restart the service, check value of open_files_limit in mysql client
it should now say 2048 (new soft limit)
The proper solution would probably be to get rid of the wrapper and use native SystemD.
UPDATE
it looks like this is fixed in the more recent version, which no longer uses the mysql_safe wrapper
rh-mariadb101-mariadb-10.1.19-6.el7.x86_64
To increase open-files-limit in MySQL 5.6 on Centos 7 you must:
nano /usr/lib/systemd/system/mysqld.service
add at the end of the file:
LimitNOFILE=65535
LimitNPROC=65535
then:
systemctl daemon-reload
systemctl restart mysqld
Done! , I assume that for MariaDB is the same thing
Ensure that no other files exist in the:
/etc/systemd/system/mariadb.service.d
folder that might be limiting the number of files.
That was my case, there was another file that was reducing the limit I set :)

Docker container with MySQL not writing in the log file

I am working on an existing Dockerfile that I have been asked to modify as less as possible. The docker image is based on a CentOS Linux image and is supposed to contain a MySQL service.
I want to enable the verbose logging for all the queries (i.e. general_log and general_log_file variables on the /etc/my.cnf file).
The MySQL service needs to be run in the mysqld_safe mode and I've checked that the configuration lines I am adding (see below the printf) are after the [mysqld_safe] line in the /etc/my.cnf file, so I am assuming this setting should be fine.
What I've done so far is adding to the Dockerfile the following statements:
RUN groupadd -r mysql && useradd -r -g mysql mysql
# [...] Lots of Mysql stuff regarding importing DBs etc.
# Adding some more configuration details to the database service
RUN printf '\n%s\n%s\n%s\n\n' '# Set General Log to log all the queries' 'general_log=1' 'general_log_file=/var/log/mysql_general.log' >> /etc/my.cnf
# Getting the new log file prepared to get written by the MySQL service
RUN touch /var/log/mysql_general.log
RUN chown mysql.mysql /var/log/mysql_general.log
# MySQL Port
EXPOSE 3306
ENTRYPOINT ["mysqld_safe"]
After building the docker image and running the docker container I see this in the /var/log folder:
-rw-r-----. 1 mysql mysql 5108 Nov 20 17:07 mysqld.log
-rw-r--r--. 1 mysql mysql 3880 Nov 20 17:44 mysql_general.log
If I grep the mysqld.log for keywords like ERROR or general I can not find anything interesting. The mysql_general.log file is empty.
I see this also this:
mysql> show variables like '%general_log%';
+------------------+----------------------------+
| Variable_name | Value |
+------------------+----------------------------+
| general_log | OFF |
| general_log_file | /var/run/mysqld/mysqld.log |
+------------------+----------------------------+
2 rows in set (0.00 sec)
mysql>
I am not able to get the SQL queries written in the log file, why?

Cannot set limit of MySQL open-files-limit from 1024 to 65535

I have mysql ver. 5.1.49-3, I am working on linux debian. I am trying to set open-files-limit to 65535. so I edited te my.cnf in /etc/mysql/
[mysqld]
open_files_limit = 65535
[mysqld_safe]
open_files_limit = 65535
then in /etc/security/limit.conf
* soft nofile 100000
* hard nofile 200000
After restarting mysql service, when I run this command in linux
ps -ef|grep mysql
I got 65535. when I log into mysql as root and fetch the value of open-files-limit
show global variables like "%open_files_limit%";
I got 1024. Please help.
If mysql is started with systemd, this setting is important:
In the file /lib/systemd/system/mysql.service you have to add this 2 lines in the [Service] section at the end:
LimitNOFILE = infinity
LimitMEMLOCK = infinity
After this restart systemctl and mysql:
systemctl daemon-reload
/etc/init.d/mysql restart
To check if the configuration is effective, you can get the parameter from the running mysql process like this:
cat /proc/$(pgrep mysqld$)/limits | grep files
All I need is to add this line to /etc/pam.d/common-session:
session required pam_limits.so
then restart apache
An issue with older versions of MySQL require you to use use open-files-limit (dashes not underbars) in my.cnf. See http://bugs.mysql.com/bug.php?id=40368
do ulimit -a for show. ulimit -n NUMBER can change to YOUR_NUMEBR open files
Take a look at the official documentation:
"The value of this variable at runtime is the real value permitted by the system and might be different from the value you specify at server startup."
On Unbuntu 14.04 this worked
vim /etc/mysql/my.cnf
[mysqld]
open-files-limit=16000
After this, just restart mysql
/etc/init.d/mysql restart