MySQL my.cnf settings: connections, buffer pool and slow query log
A small MySQL or MariaDB override file for connections, memory, character set and the slow query log.
This file overrides a few MySQL settings without touching the main config. Set the number of connections, the memory for InnoDB and the logging you want, then restart MySQL.
Create your config file
Type your own values. The file below updates as you type, and nothing you type leaves your browser.
[mysqld]
max_connections = 200
innodb_buffer_pool_size = 1G
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1
bind-address = 127.0.0.1
[client]
default-character-set = utf8mb4How this setup works
Step-by-step setup
- Save the settings above as /etc/mysql/conf.d/custom.cnf. On some systems the folder is /etc/my.cnf.d/.
- If you turned on the slow log, create its folder: sudo mkdir -p /var/log/mysql && sudo chown mysql:mysql /var/log/mysql.
- Restart MySQL: sudo systemctl restart mysql (the service may be called mysqld or mariadb).
- Check a value: mysql -e "SHOW VARIABLES LIKE 'max_connections'".
- Read slow queries with sudo tail /var/log/mysql/slow.log.
What the important lines mean
- max_connections = 200 is how many clients may be connected at once.
- innodb_buffer_pool_size is the memory InnoDB uses to cache data and indexes. It is the most important memory setting.
- utf8mb4 stores all of Unicode, including emoji. The older utf8 does not.
- bind-address controls where connections may come from. 127.0.0.1 means this computer only.
Common errors and how to fix them
- ERROR 1040: Too many connections
- The limit was reached. Raise max_connections, but first find out why: a script that never closes connections, or too many app workers. SHOW PROCESSLIST shows who is connected.
- MySQL will not start after editing
- A wrong value or a typo. Read the log with sudo journalctl -u mysql -n 50. A buffer pool larger than the free RAM is a common cause.
- Slow log file is not created
- The folder does not exist or is not owned by the mysql user. Create it and fix the owner, then restart.
- Unknown variable
- The name is wrong or not supported by your version. MariaDB and MySQL differ in a few settings.
Frequently asked questions
- Where is my.cnf?
- Run mysql --help and look for "Default options are read from the following files". Files in conf.d are read too, which is the safe place for your changes.
- What should the buffer pool size be?
- On a server that only runs MySQL, about half to two thirds of the RAM. On a shared small server use much less, so other programs still have memory.
- Is it safe to set bind-address to 0.0.0.0?
- Only with a firewall and strong passwords. Prefer 127.0.0.1 and an SSH tunnel, or allow only specific IP addresses.
Free tools that help
- Common ports listSearch a list of common TCP and UDP port numbers: SSH 22, HTTP 80, HTTPS 443, MySQL 3306, PostgreSQL 5432 and more, with dev server ports.
Fix a problem with this setup
- MySQL too many connectionsFix MySQL ERROR 1040 "Too many connections": see who is connected, close leaking connections and raise max_connections.
- MySQL access deniedFix error 1045. Default passwords for XAMPP and MAMP, and how to log in on Linux where root uses socket login.
- phpMyAdmin #2002 cannot connectFix phpMyAdmin error #2002, "The server is not responding": start MySQL, use 127.0.0.1 and the right port, or fix the socket and Docker host.
More server configs
- nginx reverse proxy configSend visitors from port 80 to an app running on port 3000.
- nginx PHP-FPM configRun a PHP site (WordPress, Laravel or plain PHP) on nginx with PHP-FPM.
- Caddyfile reverse proxySend visitors to an app on port 3000 and get HTTPS certificates automatically.
- crontab examplesRun a script every minute, hour, day or week.
- systemd service fileRun your app in the background and start it automatically at boot.
- Apache virtual hostHost a website from a folder with Apache, with or without .htaccess.