MySQL is a widely used open-source relational database management system. In this article, we will guide you through the steps to install MySQL database on a Linux system with the main focus on the Ubuntu operating system.
Prerequisites
To do the installation, ensure you have access to the following:
- An Ubuntu system (preferably an updated version).
- Access to a terminal or command line.
- Sudo privileges or access to the root user.
Step 1: Update Package Lists
Update the package lists for upgrades and new package installations:
sudo apt update
Step 2: Install MySQL Server
Install the MySQL server package using the following command. During the installation, the system may prompt you to set a root password for MySQL. Set it as required or have it set when setting the security primitives of mysql database.
sudo apt install mysql-server
Step 3: Configure MySQL
To secure your MySQL installation and improve its security, run the MySQL secure installation script:
sudo mysql_secure_installation #If this returns an error, proceed to step 4; then back to step 3
Follow the prompts to complete the security configurations.
Step 4: Start and Enable MySQL Service
Start the MySQL service and enable it to start on boot:
sudo systemctl start mysql #Start mysql service
sudo systemctl enable mysql #enable the service to start on reboot
OR
#If systemctl is not configured
service mysql start #Start mysql service
service mysql enable #enable the service to start on reboot
Step 5: Verify MySQL Installation
To verify that MySQL has been successfully installed and is running, execute the following command to access the MySQL shell:
mysql -u root -p #then enter the root password , set on previous steps.
How to retrieve the default MySQL password
In some cases, you may not be prompted to enter a password for the root user. When this happens, you need to be aware of how to access the database so that you can create other users. Below are a few tricks you can use to get access to the MySQL database. Once in, you can create your preferred user.
Option 1:
#Run the following command to access mysql database. Once prompted, enter your sudo password:
> sudo mysql --defaults-file=/etc/mysql/debian.cnf
Option 2:
> sudo cat /etc/mysql/debian.cnf
#This file has some default username and password. Use that to access the database, see example.
mysql -u debian-sys-maint -p #use the password provided.
Option 3:
#Run the mysql as sudo, this doesnt ask for password
> sudo mysql -u root
Option 4:
#Run the following:
> sudo -i
> mysql
Once logged in, we can now proceed and run the database queries.
Create a database and switch to it
CREATE DATABASE mydatabase;
USE mydatabase;
Create a User and Set a Password:
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
Grant Privileges to the User on the Database:
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
Flush Privileges:
FLUSH PRIVILEGES;
Create a Table and Insert Data:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
age INT
);
INSERT INTO users (name, age) VALUES ('John Doe', 30);
INSERT INTO users (name, age) VALUES ('Alice Smith', 25);
select * from users #returns all data from users table
Make a donation to support us
Web Hosting and email hosting Packages
Related content:
- A Practical Tutorial for Dockerizing Software Applications
- How to Configure a Docker App to a Domain Name
- Getting Started with Docker | Docker commands
- How To Run Scripts in Linux
- Deploy a Django Application on EC2 Instance with Nginx
- How to configure a domain to a docker container and install an SSL certificate on AWS