MariaDB Tutorial
MariaDB is an open-source relational database management system (RDBMS) that is a popular alternative to MySQL. It is fast, scalable, and suitable for various types of applications, from small websites to large data warehouses.
Prerequisitesβ
-
MariaDB Installation: Ensure that MariaDB is installed on your system.
mysql --versionIf not installed, you can install it via your package manager, for example:
sudo apt update
sudo apt install mariadb-server -
Basic Knowledge of SQL: Familiarity with SQL basics will be helpful.
1. Starting and Stopping MariaDBβ
To start the MariaDB service:
sudo systemctl start mariadb
To stop the MariaDB service:
sudo systemctl stop mariadb
To enable MariaDB to start at boot:
sudo systemctl enable mariadb
2. Logging into MariaDBβ
To log in to the MariaDB client as the root user:
sudo mysql -u root -p
Enter your password when prompted.
3. Basic SQL Commands in MariaDBβ
Creating a Databaseβ
To create a new database:
CREATE DATABASE example_db;
Selecting a Databaseβ
To use a specific database:
USE example_db;
Creating a Tableβ
To create a table named users with some basic fields:
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
email VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Inserting Dataβ
To insert data into the users table:
INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com');
Querying Dataβ
To retrieve all records from the users table:
SELECT * FROM users;
Updating Dataβ
To update a userβs name in the users table:
UPDATE users SET name = 'Jane Doe' WHERE id = 1;
Deleting Dataβ
To delete a user from the users table:
DELETE FROM users WHERE id = 1;
4. User Management in MariaDBβ
Creating a New Userβ
To create a new user with a password:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
Granting Privilegesβ
To grant all privileges on a specific database:
GRANT ALL PRIVILEGES ON example_db.* TO 'newuser'@'localhost';
Revoking Privilegesβ
To revoke privileges from a user:
REVOKE ALL PRIVILEGES ON example_db.* FROM 'newuser'@'localhost';
Deleting a Userβ
To delete a user:
DROP USER 'newuser'@'localhost';
5. Backing Up and Restoring Databasesβ
Backing Up a Databaseβ
To back up a MariaDB database using mysqldump:
mysqldump -u root -p example_db > example_db_backup.sql
Restoring a Databaseβ
To restore a MariaDB database from a backup file:
mysql -u root -p example_db < example_db_backup.sql
6. Useful MariaDB Commandsβ
Show Databasesβ
To list all databases:
SHOW DATABASES;
Show Tablesβ
To list all tables in the selected database:
SHOW TABLES;
Describe Table Structureβ
To show the structure of a table:
DESCRIBE users;
Exiting the MariaDB Clientβ
To exit the MariaDB client:
EXIT;
Summaryβ
This tutorial introduced the basics of using MariaDB, including:
- Starting and stopping the MariaDB service.
- Creating and managing databases and tables.
- Performing CRUD (Create, Read, Update, Delete) operations.
- Managing users and privileges.
- Backing up and restoring databases.
MariaDB is a powerful and versatile database system, well-suited for both small and large-scale applications. Experiment with SQL commands to get comfortable with database operations in MariaDB.
Content Reviewβ
The content in this repository has been reviewed by chevp. Chevp is dedicated to ensuring that the information provided is accurate, relevant, and up-to-date, helping users to learn and implement programming skills effectively.
About the Reviewerβ
For more insights and contributions, visit chevp's GitHub profile: chevp's GitHub Profile.