MySQL - Administration
Modern MySQL Administration Fundamentals
Section titled “Modern MySQL Administration Fundamentals”MySQL administration involves managing the health, security, and performance of your database server. This has evolved from manual server management to using modern command-line tools, containerization, and cloud services. This tutorial covers the fundamental administrative tasks for a modern MySQL setup.
Server Lifecycle Management
Section titled “Server Lifecycle Management”How you manage the MySQL server process depends on your operating system and environment.
Linux (using systemd)
Section titled “Linux (using systemd)”On most modern Linux distributions, you’ll use systemctl to manage the MySQL service (the service name might be mysql or mysqld).
# Start the MySQL serversudo systemctl start mysqld
# Stop the MySQL serversudo systemctl stop mysqld
# Restart the serversudo systemctl restart mysqld
# Check the server's status (very useful for debugging)sudo systemctl status mysqldmacOS (using Homebrew)
Section titled “macOS (using Homebrew)”If you installed MySQL via Homebrew, you can use brew services.
# Start the MySQL server and have it launch at loginbrew services start mysql
# Stop the MySQL serverbrew services stop mysql
# Restart the serverbrew services restart mysqlDocker (The Modern, Portable Approach)
Section titled “Docker (The Modern, Portable Approach)”Using Docker provides a consistent, isolated environment for development and deployment. You manage the server through the Docker container lifecycle.
# Run a new MySQL container in the backgrounddocker run --name my-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d -p 3306:3306 mysql:latest
# Stop the containerdocker stop my-mysql
# Start the container againdocker start my-mysql
# View logs for debuggingdocker logs my-mysqlSecure User and Access Control Management
Section titled “Secure User and Access Control Management”Proper user management is critical for security. The guiding principle is the principle of least privilege: only grant users the permissions they absolutely need.
Creating Users and Granting Privileges
Section titled “Creating Users and Granting Privileges”Always create dedicated users for your applications instead of using the root user. The process is a two-step: create the user, then grant privileges.
-- Step 1: Create a new user. 'app_user' can connect only from localhost.CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'a-very-strong-password';
-- Step 2: Grant specific privileges on a specific database.-- This user can only perform CRUD operations on tables in the 'my_app_db' database.GRANT SELECT, INSERT, UPDATE, DELETE ON my_app_db.* TO 'app_user'@'localhost';
-- It's a good practice to reload privileges after manual grant table edits, though-- GRANT/CREATE USER statements usually handle this automatically.FLUSH PRIVILEGES;Updating Passwords
Section titled “Updating Passwords”The modern way to change a user’s password is with ALTER USER.
ALTER USER 'app_user'@'localhost' IDENTIFIED BY 'new-even-stronger-password';Using Roles (MySQL 8.0+)
Section titled “Using Roles (MySQL 8.0+)”Roles simplify permission management. You can group permissions into a role and then assign that role to multiple users.
-- Create roles for read-only and read-write accessCREATE ROLE 'app_readonly', 'app_readwrite';
-- Grant permissions to the rolesGRANT SELECT ON my_app_db.* TO 'app_readonly';GRANT SELECT, INSERT, UPDATE, DELETE ON my_app_db.* TO 'app_readwrite';
-- Create a user and grant them the readonly roleCREATE USER 'analyst'@'localhost' IDENTIFIED BY 'secure_password';GRANT 'app_readonly' TO 'analyst'@'localhost';
-- Activate the role for the user's sessionSET DEFAULT ROLE 'app_readonly' TO 'analyst'@'localhost';Essential Administrative Commands
Section titled “Essential Administrative Commands”These commands are your day-to-day tools for inspecting the state of your database.
USE database_name;— Select a database to work with.SHOW DATABASES;— List all databases you have permission to see.SHOW TABLES;— List all tables in the currently selected database.DESCRIBE table_name;— Show the structure of a table (columns, types, etc.).SHOW PROCESSLIST;— Display all currently running queries and connections. Crucial for diagnosing performance issues.SHOW VARIABLES LIKE 'variable_name%';— Check the value of server configuration variables.SHOW ENGINE INNODB STATUS;— Provides detailed information about the InnoDB storage engine, including deadlocks and transaction history.
Understanding Configuration Files
Section titled “Understanding Configuration Files”MySQL’s behavior is controlled by a configuration file, typically named my.cnf. Its location varies by OS. You can find its location by running mysql --help | grep 'Default options'.
A minimal, modern configuration file might look like this:
[mysqld]# General settingsdatadir = /var/lib/mysqlsocket = /var/lib/mysql/mysql.sock
# Important for security and consistencysql_mode = "ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
# InnoDB performance tuninginnodb_buffer_pool_size = 1G # Adjust based on your server's RAM (e.g., 70-80% of available RAM)innodb_log_file_size = 256M
# Logginglog-error = /var/log/mysql/error.logslow_query_log = 1slow_query_log_file = /var/log/mysql/slow-query.loglong_query_time = 2Always back up your my.cnf file before making changes, and only change one setting at a time to observe its effect. Restart the MySQL server for changes to take effect.