MySQL is a powerful, open-source relational database system widely used for websites, applications, and hosting control panels. If you’re running a VPS or dedicated server, installing MySQL from the terminal is quick and efficient.
This guide walks you through:
- Installing MySQL server
- Securing the installation and creating a MySQL user
- Creating a database
- Creating tables via the MySQL CLI
β Step 1: Install MySQL Server
π¦ For Ubuntu/Debian:
bash
sudo apt update
sudo apt install mysql-server -y
π¦ For CentOS/AlmaLinux/Rocky:
bash
sudo dnf install @mysql -y
Once installed, verify that MySQL is running:
bash
sudo systemctl start mysqld
sudo systemctl enable mysqld
π Step 2: Secure MySQL Installation
Run the built-in security script:
bash
sudo mysql_secure_installation
You’ll be prompted to:
- Set the root password
- Remove anonymous users
- Disallow remote root login
- Remove test database
- Reload privilege tables
π‘ Tip: Choose βYβ for all prompts to improve security.
π€ Step 3: Log in to MySQL as Root
bash
sudo mysql -u root -p
You’ll be prompted to enter the root password you just created.
π§βπ» Step 4: Create a New MySQL User (Optional)
Itβs a good practice not to use the root user for applications. Create a new user with access to your database.
sql
CREATE USER 'youruser'@'localhost' IDENTIFIED BY 'strongpassword';
GRANT ALL PRIVILEGES ON *.* TO 'youruser'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
β
Replace youruser
and strongpassword
with your own values.
ποΈ Step 5: Create a New Database
From within the MySQL shell:
sql
CREATE DATABASE your_database_name;
β Example:
sql
CREATE DATABASE website_data;
π§± Step 6: Create a Table in Your Database
First, use the new database:
sql
USE website_data;
Then create a table. For example, to store user data:
sql
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
β You can view all tables with:
sql
SHOW TABLES;
β To describe a tableβs structure:
sql
DESCRIBE users;
π Step 7: Insert and View Data (Bonus)
Insert sample data:
sql
INSERT INTO users (username, email, password)
VALUES ('admin', 'admin@example.com', 'securepass123');
View data:
sql
SELECT * FROM users;
πͺ Step 8: Exit MySQL Shell
sql
EXIT;
β Summary of Commands
Hereβs a quick summary of key CLI commands:
bash
# Install MySQL (Debian/Ubuntu)
sudo apt install mysql-server -y
# Install MySQL (RHEL/CentOS)
sudo dnf install @mysql -y
# Secure installation
sudo mysql_secure_installation
# Login to MySQL
mysql -u root -p
Inside MySQL:
sql
CREATE USER 'youruser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'youruser'@'localhost';
CREATE DATABASE your_db;
USE your_db;
CREATE TABLE your_table (...);
EXIT;
π Final Thoughts
Managing MySQL from the command line gives you powerful control over your databases. Whether you’re hosting websites or applications, understanding these fundamentals helps with performance, security, and flexibility.
Need help managing users, backups, or migrations? Contact us at below for support
π§ Email:Β support@anytimeserver.support
π Website:Β https://anytimeserver.support