Install MySQL on Ubuntu 24.04
Step 1 – Install MySQL
apt update && apt upgrade -y
apt install -y mysql-server
systemctl enable --now mysql
Step 2 – Secure the installation
mysql_secure_installation
Follow the prompts to set the root password, remove anonymous users, disallow remote root login, and remove the test database.
Step 3 – Connect
mysql -u root -p
Step 4 – Create a database and user
CREATE DATABASE appdb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'StrongPass123!';
GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Step 5 – Configuration file
Main config: /etc/mysql/mysql.conf.d/mysqld.cnf
Important defaults to tune:
[mysqld]
# InnoDB buffer pool – set to 70–80% of available RAM
innodb_buffer_pool_size = 1G
# Slow query logging
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1
# Binary logging (required for replication)
log_bin = /var/log/mysql/mysql-bin
binlog_expire_logs_seconds = 604800