Welcome to my website

Setup a database

Guide: Installing MySQL Server



This guide will walk you through installing MySQL, a popular open-source relational database management system. MySQL is widely used for web applications and various data storage needs.

Prerequisites:
  • A Linux server (e.g., Ubuntu, Debian)
  • Root access or sudo privileges


Step 1: Install MySQL Server
On most Debian/Ubuntu-based systems, you can install the MySQL server with the following command. The `mysql-server` package will pull in all necessary components.


sudo apt update
sudo apt install mysql-server mysql-client


After installation, the MySQL service should start automatically. You can verify its status:

sudo systemctl status mysql

You should see "active (running)".

Step 2: Run the Security Script
A fresh installation of MySQL often has some insecure default settings. The `mysql_secure_installation` script helps you harden your database server by prompting you to make key security choices.


sudo mysql_secure_installation


You will be prompted with a series of questions. Here’s a breakdown of common prompts and recommended answers:

Enter current password for root (enter for none):
Press Enter here, as there's no root password set by default right after installation.

Switch to unix_socket authentication? [Y/n]
This feature allows the MySQL root user to log in via the Unix socket without a password, as long as the OS user is also root. This is generally recommended for security, as it prevents remote root logins. Type Y and press Enter.

Change the root password? [Y/n]
Recommended: Y. Type Y and press Enter. Then, enter a strong password for your database root user twice. This is the root password for your MySQL database, not your system root password.

Remove anonymous users? [Y/n]
Recommended: Y. Anonymous users can log in without credentials. Removing them enhances security.

Disallow root login remotely? [Y/n]
Recommended: Y. This prevents the database root user from logging in from anywhere other than localhost (the server itself). This is a critical security measure.

Remove test database and access to it? [Y/n]
Recommended: Y. The 'test' database is insecure and not needed for production.

Reload privilege tables now? [Y/n]
Recommended: Y. This applies the changes you just made to the privilege tables.

Step 3: Test MySQL Root Login
Now that you've secured your installation, test logging in as the database root user.


sudo mysql -u root -p

You will be prompted for the database root password you set in Step 2. Enter it and press Enter.

If successful, you will see the MySQL command prompt (mysql>). You can type exit; and press Enter to quit the prompt.

Step 4: Create a Database and User (Optional, but Recommended for Applications)
For applications like phpMyAdmin (or any web application), it's best practice to create a dedicated database and a dedicated user with specific privileges for that database, instead of using the database root user directly.

1. Log in to your MySQL server as root:

sudo mysql -u root -p

(Enter your database root password)

2. Create a new database (replace your_database_name):

CREATE DATABASE your_database_name;


3. Create a new user and grant them privileges on that database (replace your_username and your_password):

CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost';
FLUSH PRIVILEGES;

  • 'localhost' means the user can only connect from the server itself. Use '%' if you need remote access (less secure).
  • GRANT ALL PRIVILEGES is for full control; you can specify more limited privileges if needed.


4. Exit the database prompt:

EXIT;


Conclusion
You have successfully installed and secured your MySQL database server. You're now ready to use it for your web applications or other data storage needs.

Back to Knowledge Base