How to Install MariaDB on Ubuntu
MariaDB is a popular open-source relational database management system, designed as a replacement for MySQL. This guide will walk you through the steps to install and configure MariaDB on Ubuntu, ensuring a secure and efficient setup. Let’s dive into the details.
mariadb install on ubuntu
Step 1: Update the System
Before installing MariaDB, ensure your system’s package index is up-to-date to avoid conflicts and issues during the installation process.
sudo apt update
sudo apt upgrade -y
Keeping your system updated is crucial for both security and performance. Learn more about Ubuntu package management on Wikipedia.
Step 2: Install MariaDB Server
MariaDB is available in the default Ubuntu repository, making installation straightforward. Use the following command to install MariaDB:
sudo apt install mariadb-server mariadb-client -y
After installation, verify that MariaDB is correctly installed by checking its version:
mariadb --version
This confirms the successful installation of MariaDB on your Ubuntu system. Read more about MariaDB on Wikipedia.
Step 3: Start and Enable MariaDB
To use MariaDB, start the service and enable it to run on system startup:
sudo systemctl start mariadb
sudo systemctl enable mariadb
You can verify that MariaDB is running correctly by checking its status:
sudo systemctl status mariadb
This ensures MariaDB is active and ready to process database queries.
Step 4: Secure the Installation
Securing your MariaDB installation is vital to protect your data. Run the built-in security script:
sudo mysql_secure_installation
Key Prompts:
- Set a root password.
- Remove anonymous users.
- Disallow root login remotely.
- Remove the test database.
Follow the on-screen instructions to complete the security setup. For more details, check MariaDB’s official security documentation.
Step 5: Connect to MariaDB
To verify the installation and start using MariaDB, log in as the root user:
sudo mariadb
You should see the MariaDB shell prompt:
MariaDB [(none)]>
This confirms a successful connection to the MariaDB server.
Step 6: Basic Configuration
Create a New Database
To create a new database, run:
CREATE DATABASE example_db;
Create a User and Grant Privileges
Next, create a user and assign necessary privileges:
CREATE USER 'example_user'@'localhost' IDENTIFIED BY 'secure_password';
GRANT ALL PRIVILEGES ON example_db.* TO 'example_user'@'localhost';
FLUSH PRIVILEGES;
Exit the MariaDB shell:
EXIT;
Step 7: Test the Connection
Test the new user’s access to ensure everything is configured correctly:
mariadb -u example_user -p
Once logged in, switch to the new database and check its structure:
USE example_db;
SHOW TABLES;
Step 8: Firewall Configuration (Optional)
If you plan to access MariaDB remotely, ensure the server allows traffic on port 3306:
sudo ufw allow 3306
sudo ufw reload
Configuring the firewall secures remote access while keeping your server protected.
Step 9: Verify Remote Access (Optional)
To enable remote access, edit the MariaDB configuration file:
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
Find the line:
bind-address = 127.0.0.1
Change it to:
bind-address = 0.0.0.0
Restart the MariaDB service:
sudo systemctl restart mariadb
This allows connections from remote hosts. Use it cautiously and ensure strong security measures are in place. Explore MariaDB’s networking documentation.
Additional Database Commands for Beginners
Here are some basic commands to help you get started with MariaDB:
Show Existing Databases
SHOW DATABASES;
Select a Database
USE database_name;
Create a Table
CREATE TABLE example_table (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
age INT
);
Insert Data into a Table
INSERT INTO example_table (name, age) VALUES ('John Doe', 30);
View Data in a Table
SELECT * FROM example_table;
Update Data in a Table
UPDATE example_table SET age = 31 WHERE name = 'John Doe';
Delete Data from a Table
DELETE FROM example_table WHERE name = 'John Doe';
Advanced Commands for Experienced Users
For more advanced users, here are some commands and tips:
Create a User with Remote Access
CREATE USER 'remote_user'@'%' IDENTIFIED BY 'remote_password';
GRANT ALL PRIVILEGES ON *.* TO 'remote_user'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Backup a Database
mysqldump -u root -p example_db > example_db_backup.sql
Restore a Database from Backup
mysql -u root -p example_db < example_db_backup.sql
Optimize a Table
OPTIMIZE TABLE example_table;
Monitor Server Status
SHOW STATUS;
SHOW VARIABLES;
Conclusion
Congratulations! You now have a fully functional MariaDB server installed and configured on your Ubuntu system. With proper security measures and user management, MariaDB offers robust performance for your database needs.
Related Resources:
Stay secure and happy coding!
