How to Install MySQL on Ubuntu (Xorg) – Step-by-Step Guide
If you're setting up a development or production server on Ubuntu, installing MySQL with a web interface like phpMyAdmin is one of the first steps for managing databases efficiently.
Whether you're using Ubuntu with Xorg (desktop environment) or a server edition, the installation process remains almost the same. In this complete guide, you'll learn:
Required packages
Step-by-step MySQL installation
Securing MySQL
Installing and configuring phpMyAdmin
Common mistakes to avoid
Frequently asked questions
We’ll be installing:
MySQL
phpMyAdmin
Apache HTTP Server
PHP
Let’s get started.
Requirements:
Before installing MySQL and phpMyAdmin on Ubuntu, make sure you have:
Ubuntu 20.04 / 22.04 / 24.04 installed
Sudo privileges
Internet connection
Terminal access
Update your system first:
sudo apt updateThen:
sudo apt upgrade -yRequired Packages
To run MySQL with phpMyAdmin, you need:
Package | Purpose |
|---|---|
mysql-server | Database engine |
apache2 | Web server |
php | Server-side scripting |
php-mysql | PHP-MySQL connector |
phpmyadmin | Web UI for MySQL |
libapache2-mod-php | Apache PHP module |
How to Install MySQL on Ubuntu
Step 1: Install MySQL Server
sudo apt install mysql-server -yAfter installation, check status:
sudo systemctl status mysqlIf running, you’ll see active (running).
Enable auto-start:
sudo systemctl enable mysqlStep 2: Secure MySQL Installation
Run:
sudo mysql_secure_installationYou’ll be asked:
Set root password
Remove anonymous users
Disable remote root login
Remove test database
Reload privilege tables
Choose Yes for all recommended options.
Step 3: Login to MySQL
sudo mysql -u root -pInside MySQL:
SHOW DATABASES;Exit:
EXIT;Install Apache and PHP
Step 4: Install Apache
sudo apt install apache2 -yCheck in browser:
http://localhostYou should see Apache default page.
Step 5: Install PHP and Required Extensions
sudo apt install php libapache2-mod-php php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip -yCheck PHP version:
php -vRestart Apache:
sudo systemctl restart apache2Install phpMyAdmin
Step 6: Install phpMyAdmin
sudo apt install phpmyadmin -yDuring installation:
Choose apache2
Select Yes for dbconfig-common
Enter MySQL root password
Set phpMyAdmin password
Step 7: Enable phpMyAdmin in Apache
If not automatically enabled:
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadminRestart Apache:
sudo systemctl restart apache2Step 8: Access phpMyAdmin Web Interface
Open browser:
http://localhost/phpmyadminLogin using:
Username: root
Password: (your MySQL password)
Create a Test Database (Practical Example)
Let’s create a database for a sample project.
Create Database via MySQL CLI
sudo mysql -u root -pThen:
CREATE DATABASE company_db;Verify:
SHOW DATABASES;Create Database via phpMyAdmin
Login
Click New
Enter 'company_db'
Click Create
Create a User and Grant Privileges (Best Practice)
Avoid using root in production.
Step 1:
CREATE USER 'company_user'@'localhost' IDENTIFIED BY 'StrongPassword123!';Step 2:
GRANT ALL PRIVILEGES ON company_db.* TO 'company_user'@'localhost';Step 3:
FLUSH PRIVILEGES;Now login using:
mysql -u company_user -pCommon Mistakes to Avoid
1. Not Running mysql_secure_installation
This leaves:
Anonymous users
Weak root access
Test databases
Always secure installation.
2. Using Root for Applications
Root has full privileges - risky for production.
3. Forgetting Apache Restart
After installing PHP or phpMyAdmin:
sudo systemctl restart apache24. Port Conflict (3306)
Check if MySQL is using port:
sudo netstat -tlnp | grep 33065. phpMyAdmin Not Found
If "/phpmyadmin" gives 404:
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadminBenefits of Installing MySQL with phpMyAdmin
1. Easy Database Management
phpMyAdmin provides GUI access.
2. Faster Development
Quickly create tables and run queries.
3. Ideal for Local Development
Perfect for testing Laravel, WordPress, or custom PHP apps.
4. Secure and Stable
MySQL is production-ready and widely used.
How to Uninstall (Optional)
Remove phpMyAdmin:
sudo apt remove phpmyadmin -yRemove MySQL:
sudo apt remove mysql-server -y
sudo apt purge mysql-server -yConclusion
Installing MySQL on Ubuntu Xorg with phpMyAdmin is straightforward when done step by step:
Update system
Install MySQL
Secure installation
Install Apache and PHP
Install phpMyAdmin
Create database and user
This setup is perfect for:
Local development
Learning SQL
Hosting PHP applications
Running small production apps
If you’re building web applications or backend systems, this stack provides a solid foundation.