Installing MySQL on Ubuntu
Get your database up and running in minutes
Need a database for your project? MySQL is probably the most popular choice out there. It's reliable, fast, and pretty straightforward to set up on Ubuntu.
Let's get it installed and running.
What You Need
- Ubuntu 20.04 or later
- Sudo access
- A couple of minutes
The Installation
Step 1: Update Package List
Start by updating your system:
sudo apt update
Step 2: Install MySQL Server
Now install MySQL:
sudo apt install mysql-server -y
This downloads and installs MySQL along with all its dependencies. Takes a minute or two.
Step 3: Start MySQL Service
MySQL usually starts automatically, but let's make sure:
sudo systemctl start mysql.service
Check If It's Running
Verify MySQL is up:
sudo systemctl status mysql
Look for "active (running)" in green. That's what you want to see.
Secure Your Installation
MySQL comes with a security script that fixes a bunch of default settings. Run it:
sudo mysql_secure_installation
It'll ask you a few questions:
- Set up password validation? Up to you, but recommended for production.
- Set a root password? Yes, definitely.
- Remove anonymous users? Yes.
- Disallow root login remotely? Yes, unless you specifically need it.
- Remove test database? Yes.
- Reload privileges? Yes.
Important: Don't skip the security script. It takes 30 seconds and prevents a bunch of potential issues down the road.
Log Into MySQL
Test that everything works:
sudo mysql
You should see the MySQL prompt:
mysql>
Type exit to get out.
Create a Database User
Using root for everything is bad practice. Create a regular user:
sudo mysql
Then in the MySQL prompt:
CREATE USER 'youruser'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON *.* TO 'youruser'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
exit
Replace youruser and yourpassword with actual values. Now you can log in without sudo:
mysql -u youruser -p
All Commands Together
Here's the basic installation:
sudo apt update
sudo apt install mysql-server -y
sudo systemctl start mysql.service
sudo mysql_secure_installation
Auto-start on boot: MySQL is usually set to start automatically when your system boots. If not, enable it with sudo systemctl enable mysql
Quick Test
Create a test database to make sure everything works:
sudo mysql
Then:
CREATE DATABASE testdb;
SHOW DATABASES;
DROP DATABASE testdb;
exit
If those commands work, MySQL is fully operational.
Pro tip: Install a GUI tool like MySQL Workbench or phpMyAdmin if you prefer a visual interface over the command line.
That's It
MySQL is installed, secured, and running. You can now create databases, tables, and start building your application. Check out the MySQL docs for more advanced configuration and optimization tips.
Happy querying! 🗄️
Comments
Post a Comment