Install and Configure MySQL on Ubuntu for WordPress
MySQL is a common database choice for WordPress and many PHP applications. On Ubuntu, you can install it from the default package repositories and then apply a small performance configuration for local web hosting.
This guide uses generic placeholders for usernames and passwords. Do not hard-code real production passwords in public scripts, documentation, Git repositories, or screenshots.
What the Script Does
The deployment script this article is based on:
- Installs
mysql-server. - Enables and starts the MySQL service.
- Creates an admin user for local socket access.
- Backs up the MySQL configuration.
- Writes a custom performance config file.
- Validates the MySQL configuration.
- Restarts MySQL and tests login again.
Install MySQL
Run:
sudo apt update sudo DEBIAN_FRONTEND=noninteractive apt install -y mysql-server sudo systemctl enable mysql sudo systemctl start mysql sudo systemctl is-active --quiet mysql
You can open the MySQL shell as root with:
sudo mysql
Create an Admin User
Use your own username and a strong password:
MYSQL_ADMIN_USER="dbadmin" MYSQL_ADMIN_PASSWORD="<REPLACE_WITH_STRONG_PASSWORD>"
Create the user:
sudo mysql <<MYSQL_SCRIPT
CREATE USER IF NOT EXISTS '${MYSQL_ADMIN_USER}'@'localhost' IDENTIFIED BY '${MYSQL_ADMIN_PASSWORD}';
GRANT ALL PRIVILEGES ON *.* TO '${MYSQL_ADMIN_USER}'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
MYSQL_SCRIPT
Test the user:
mysql --protocol=socket -u "${MYSQL_ADMIN_USER}" -p -e "SELECT VERSION();"
Type the password when prompted.
Avoid putting the real password directly on the command line, because command-line arguments may be visible in shell history or process listings.
Back Up the MySQL Config
Create a backup folder:
sudo mkdir -p /opt/server-backups/mysql timestamp="$(date +%Y%m%d%H%M%S)"
Back up the main MySQL config if it exists:
if [ -f /etc/mysql/mysql.conf.d/mysqld.cnf ]; then
sudo cp /etc/mysql/mysql.conf.d/mysqld.cnf "/opt/server-backups/mysql/mysqld.cnf.before.${timestamp}.bak"
fi
Write a Custom Performance Config
Create a custom config file:
sudo tee /etc/mysql/mysql.conf.d/99-custom-performance.cnf >/dev/null <<EOF [mysqld] port = 3306 bind-address = 127.0.0.1 innodb_buffer_pool_size = 4G max_connections = 200 tmp_table_size = 256M max_heap_table_size = 256M innodb_flush_log_at_trx_commit = 1 innodb_file_per_table = 1 skip_name_resolve = ON EOF
The bind-address = 127.0.0.1 setting keeps MySQL listening locally. That is a good default when web applications connect from the same server.
For remote administration, use an SSH tunnel instead of exposing MySQL directly to the internet.
Validate and Restart MySQL
Validate the config:
sudo mysqld --validate-config
If validation succeeds, restart MySQL:
sudo systemctl restart mysql sudo systemctl is-active --quiet mysql
Test login again:
mysql --protocol=socket -u "${MYSQL_ADMIN_USER}" -p -e "SELECT VERSION();"
Back up the custom config:
sudo cp /etc/mysql/mysql.conf.d/99-custom-performance.cnf "/opt/server-backups/mysql/99-custom-performance.cnf.after.${timestamp}.bak"
Troubleshooting
If MySQL does not restart, check:
sudo journalctl -u mysql -xe sudo mysqld --validate-config
If login fails, confirm the user exists:
sudo mysql -e "SELECT user, host FROM mysql.user;"
Quick Reference
sudo apt install -y mysql-server sudo systemctl enable mysql sudo mysqld --validate-config sudo systemctl restart mysql sudo mysql
Keep database credentials private. Use placeholders in documentation, prompt for passwords when possible, and never publish real passwords in scripts.