Installing MySQL in Linux Ubuntu: A Step-by-Step Guide
Introduction
MySQL is a popular open-source relational database management system widely used in various applications, including web servers, email servers, and more. In this article, we will guide you through the process of installing MySQL in Linux Ubuntu.
Prerequisites
Before installing MySQL, ensure that your Linux Ubuntu system meets the following requirements:
- CPU: 64-bit CPU or AMD64 processor
- Memory: 4 GB RAM or more
- Storage: 10 GB available disk space
- Package manager: Ubuntu 18.04 or later
- Internet connection: For downloading MySQL packages
Step 1: Update and Upgrade Your System
Before installing MySQL, update your system to ensure you have the latest packages:
- Open a terminal and run the following command to update your system:
sudo apt update - If you’re using a newer version of Ubuntu, you might need to upgrade to the latest version. Run the following command:
sudo apt full-upgrade
Step 2: Install MySQL Server
Install the MySQL server package:
- Run the following command to install MySQL server:
sudo apt install mysql-server - If you’re using a newer version of Ubuntu, you might need to install the MySQL server package using the following command:
sudo apt install mysql-server-8.0
Step 3: Configure MySQL Server
Configure the MySQL server to start automatically on boot:
- Edit the
/etc/mysql/mysql.conf.d/mysqld.cnffile using the following command:sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf - Add the following lines to the file:
[mysqld]
default-pool-size = 128
default-timeout = 60
default-connect-timeout = 60
log-error = /var/log/mysql/error.log - Save and close the file.
- Restart the MySQL server to apply the changes:
sudo service mysql restart
Step 4: Create a Database and User
Create a new database and user:
- Run the following command to create a new database:
sudo mysql -u root -p - Create a new user with a strong password:
sudo mysql -u root -p -e "CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypassword';" - Grant privileges to the new user:
sudo mysql -u root -p -e "GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%';"
Step 5: Configure MySQL to Use the New Database
Configure MySQL to use the new database:
- Edit the
/etc/mysql/mysql.conf.d/mysqld.cnffile using the following command:sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf - Add the following lines to the file:
[mysqld]
default-pool-size = 128
default-timeout = 60
default-connect-timeout = 60
log-error = /var/log/mysql/error.log - Save and close the file.
- Restart the MySQL server to apply the changes:
sudo service mysql restart
Step 6: Test MySQL
Test MySQL to ensure it’s working correctly:
- Run the following command to test MySQL:
mysql -u myuser -p - Create a new user and database:
CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypassword';
CREATE DATABASE mydb; - Grant privileges to the new user:
GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'%'; - Test the database connection:
SELECT * FROM mydb;
Troubleshooting
- If you encounter issues with MySQL, check the MySQL error log for more information.
- If you’re using a newer version of Ubuntu, you might need to update the MySQL server package using the following command:
sudo apt update - If you’re using a different package manager, you might need to update the MySQL server package using the following command:
sudo apt update
Conclusion
Installing MySQL in Linux Ubuntu is a straightforward process that requires minimal technical expertise. By following these steps, you can install MySQL and start using it to manage your database. Remember to regularly update your MySQL server to ensure you have the latest security patches and features.
