Hey there, database enthusiasts! 🛢️ Today, we're diving into setting up MySQL, the powerhouse relational database that's been a backbone of web applications for years. Don't worry – I'll walk you through this step-by-step!

⏱️ Estimated setup time: 10-15 minutes

First Things First: System Update 💫

Let's get your system refreshed and ready:

sudo apt update  

Installing MySQL Server 🔧

Time to bring MySQL into your world:

sudo apt install mysql-server  

Starting and Enabling MySQL Service 🚦

Let's get MySQL up and running:

sudo systemctl start mysql.service  
sudo systemctl enable mysql.service  
💡
Pro tip: The 'enable' command ensures MySQL starts automatically after server reboots.

Initial MySQL Configuration 🔐

Access MySQL as the root user:

sudo mysql  

Now, we'll set a secure root password:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';  
exit
⚠️
Important - Replace 'password' with a strong, unique password!

Securing Your MySQL Installation 🛡️

The mysql_secure_installation wizard is your first line of defense. Let's break down each step:

1. Password Validation Component 🔐

When prompted:

Would you like to setup VALIDATE PASSWORD COMPONENT?  
Press y|Y for Yes, any other key for No:
  • Choose 'y' to enable enhanced password security
  • You'll be presented with three password strength levels:
    • LOW: Length >= 8
    • MEDIUM: Length >= 8, includes numeric, mixed case, and special characters
    • STRONG: Length >= 8, includes numeric, mixed case, special characters, and dictionary check

Password Strength Selection

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:  
  • Recommended: Choose 1 (MEDIUM) or 2 (STRONG)
  • Provides robust protection against weak passwords

2. Root Password Configuration 🔑

  • The wizard will assess your existing root password strength
  • You'll be given the option to change the root password
  • If your current password is weak, strongly consider changing it

3. Anonymous User Removal 👥

Remove anonymous users?  
  • Always choose 'y' (Yes)
  • Prevents unauthorized access to your MySQL server
  • Anonymous users can log in without a specific account
  • Critical for preventing potential security vulnerabilities

4. Remote Root Login Restriction 🌐

Disallow root login remotely?  
  • Always choose 'y' (Yes)
  • Prevents potential brute-force attacks
  • Ensures root access is only possible from localhost
  • Best practice for server security

5. Test Database Removal 🧪

Remove test database and access to it?  
  • Choose 'y' (Yes)
  • Removes the default test database
  • Eliminates unnecessary access points
  • Follows the principle of least privilege

6. Privilege Table Reload 🔄

  • Automatically reloads privilege tables
  • Ensures all security changes take effect immediately

🚨 Security Best Practices

  • Use a strong, unique password
  • Enable password validation
  • Disable remote root login
  • Remove anonymous users
  • Remove test database

Helpful Things to Know 📌

  • Configuration file: /etc/mysql/mysql.conf.d/mysqld.cnf
  • Default port: 3306
  • Data storage: /var/lib/mysql
  • Log files: /var/log/mysql

Verifying Installation ✅

Check your MySQL version:

mysql --version  

First-Time Connection 🔌

Connect to MySQL:

mysql -u root -p  

Enter the password you set earlier.

Security Note 🚨

Always use strong, unique passwords and follow best practices:

  • Use complex passwords
  • Limit root access
  • Create specific user accounts for applications
  • Regularly update and patch your MySQL server

Troubleshooting 🛠️

If you encounter issues, check the log file:

sudo tail -f /var/log/mysql/error.log  

That's it! 🎉 Your MySQL server is now up and running.

⏱️ Total setup time: Approximately 15 minutes

Got questions? Are you running into issues? Drop a comment below, and I'll help you out!

Share this post