Introduction:
Embarking on your journey into database management on Ubuntu? This beginner-friendly tutorial will walk you through the step-by-step process of creating a database, setting up a database user, and establishing a connection with ease.

Prerequisites:
Make sure you have Ubuntu installed on your system, and you have administrative privileges.

Step 1: Install MySQL (if not already installed):
Open a terminal and run the following commands to install MySQL:

sudo apt update
sudo apt install mysql-server

During the installation, you’ll be prompted to set a root password for MySQL.

Step 2: Access MySQL:
Log in to MySQL with the root user:

sudo mysql -u root -p

Enter the root password when prompted.

Step 3: Create a Database:
Let’s create a database. Replace [database_name] with your desired database name:

CREATE DATABASE [database_name];

Example:

CREATE DATABASE mydatabase;

Step 4: Create a Database User:
Now, create a user for your database. Replace [username] and [password] with your preferred username and password:

CREATE USER '[username]'@'localhost' IDENTIFIED BY '[password]';

Example:

CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';

Step 5: Grant Privileges:
Grant the user the necessary privileges on the database. Replace [database_name] and [username] with your database name and username:

GRANT ALL PRIVILEGES ON [database_name].* TO '[username]'@'localhost';
FLUSH PRIVILEGES;

Example:

GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;

Step 6: Exit MySQL:
Exit the MySQL prompt:

EXIT;

Step 7: Test the Connection:
Now, let’s test the connection using the created user. Log in again but this time with the new user:

mysql -u [username] -p

Example:

mysql -u myuser -p

Enter the password when prompted.

Congratulations! You’ve Successfully Created a Database, User, and Connected:
You’ve just completed a fundamental task in database management on Ubuntu. This lays the groundwork for your future database endeavors. Remember, this is just the beginning—explore, experiment, and build upon this foundation as you delve deeper into the world of databases on Ubuntu. Happy coding!

Last Update: December 10, 2023