• Home
  • 🐬 How to Install MySQL Server and Set Up a Database via Command Line (Linux)

🐬 How to Install MySQL Server and Set Up a Database via Command Line (Linux)

by:admin January 13, 2025 0 Comments

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:

  1. Installing MySQL server
  2. Securing the installation and creating a MySQL user
  3. Creating a database
  4. 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

Categories:

Leave Comment