Skip to Content

How to Install MySQL on Ubuntu 24


Ubuntu 24 is a popular platform for hosting databases due to its stability and wide adoption. This article will guide you step by step to install MySQL on Ubuntu 24, configure a database, and create a user with the appropriate permissions.

1. Préparer l’Environnement

Before installing MySQL, make sure your system is up to date. Open a terminal and run the following commands:

sudo apt update && sudo apt upgrade -y

This ensures that all system packages are up to date.

2. Install MySQL

Ubuntu 24 includes MySQL in its official repositories. Here is how to install the MySQL server:

Step 1: Install MySQL

Run the following command to install MySQL:

sudo apt install mysql-server -y

Step 2: Check the installation

To check if MySQL is correctly installed and running, use:

sudo systemctl status mysql

If the service is active (indicated by "active (running)"), it means that the installation was successful.

Step 3: Configure MySQL for the first time

MySQL includes a security script to improve the default configuration. Run it with:

sudo mysql_secure_installation

During this configuration:

  • Set a secure root password.
  • Answer the questions to enable or disable certain features (such as removing anonymous users, disabling remote root logins, etc..).

3. Se Connecter à MySQL

To manage your database, connect to the MySQL shell with:

sudo mysql

This will connect you as the root user without asking for a password (if you are using socket authentication).

4. Créer une Base de Données

Once connected to the MySQL shell, you can create a database. Suppose you want to create a database named blog_db. Execute:

CREATE DATABASE blog_db;

You can check that the database has been created with:

SHOW DATABASES;

5. Créer un Utilisateur MySQL

For security reasons, it is recommended not to use the root account to access your database. Create a new user with specific permissions.

Step 1: Create a user

Replace user_blog with your username and password123 with a strong password:

CREATE USER 'user_blog'@'localhost' IDENTIFIED BY 'password123';

Step 2: Grant Permissions

Assurez-vous que cet utilisateur dispose des droits nécessaires pour interagir avec la base de données blog_db. Par exemple :

GRANT ALL PRIVILEGES ON blog_db.* TO 'user_blog'@'localhost';

Step 3: Apply the changes

To apply the permissions immediately, execute:

FLUSH PRIVILEGES;

6. Tester la Connexion avec le Nouvel Utilisateur

Disconnect from the MySQL shell by typing:

EXIT;

Then, reconnect with the new user:

mysql -u user_blog -p

Enter the password you set. If everything works, you are successfully logged in.

7. (Optionnel) Configurer un Accès à Distance

If you need the user to connect from another machine, replace the % with localhost when creating the user:

CREATE USER 'user_blog'@'%' IDENTIFIED BY 'password123'; GRANT ALL PRIVILEGES ON blog_db.* TO 'user_blog'@'%';

Then, modify the configuration file to allow remote connections. Open the file /etc/mysql/mysql.conf.d/mysqld.cnf :

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Search for the following line:

bind-address = 127.0.0.1

Replace it with:

bind-address = 0.0.0.0

Restart MySQL to apply the changes:

sudo systemctl restart mysql

Conclusion

You have now installed MySQL on Ubuntu 24, created a database, and configured a user with the necessary permissions. Whether for developing an application or hosting a website, you are ready to use MySQL securely and efficiently.

N’hésitez pas à commenter ci-dessous pour partager vos expériences ou poser vos questions ! 🚀



Rating
0 0

There are no comments for now.

to be the first to leave a comment.