Setting up a database server on Ubuntu using MySQL can be a challenging task, especially for those who are new to Linux or database management. However, with the right guidance, this process can be made much easier. In this blog, we will guide you through the steps necessary to set up a MySQL database server on Ubuntu.
Step-1: Install MySQL Server
The first step is to install MySQL Server on your Ubuntu machine. This can be done by running the following command:
sudo apt-get update
sudo apt-get install mysql-server
During the installation process, you will be prompted to create a root password for MySQL. Be sure to choose a strong password and remember it, as you will need it to access the MySQL server.
Step-2: Secure MySQL Server
After installing MySQL, it is essential to secure it by running the following command:
sudo mysql_secure_installation
This command will prompt you to configure several security options, including changing the root password, removing anonymous users, disallowing root login remotely, and removing test databases. Follow the prompts to secure your MySQL server.
Step-3: Create a New Database
Once the MySQL server is installed and secured, you can create a new database by logging into the MySQL command-line interface using the following command:
mysql -u root -p
You will be prompted to enter your MySQL root password. After entering your password, you will be logged into the MySQL command-line interface.
To create a new database, run the following command:
CREATE DATABASE dbname;
Replace dbname with the name you want to give to your new database.
Step-4: Create a New MySQL User
After creating the new database, you will need to create a new MySQL user to access it. To create a new user, run the following command:
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
Replace username with the name you want to give to your new user and password with the password you want to set for the user.
Step-5: Grant Access to the New User
To grant the new user access to the new database, run the following command:
GRANT ALL PRIVILEGES ON dbname.* TO 'username'@'localhost';
Replace dbname with the name of the new database you created in Step 3, and username with the name of the new user you created in Step 4.
Step-6: Exit MySQL Command Line Interface
Once you have completed all of the necessary database configurations, you can exit the MySQL command-line interface by running the following command:
exit
Congratulations! You have successfully set up a MySQL database server on Ubuntu and created a new database with a new user. You can now use this database to store and manage data for your applications.