mysqld - the MariaDB server
mysql - the MariaDB command-line tool
database -> table -> attribute
Install in CentOS
$ cat /etc/yum.repos.d/MariaDB.repo
# MariaDB 10.4 CentOS repository list - created 2019-12-09 10:58 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
sudo yum install MariaDB-server MariaDB-client
sudo systemctl enable mariadb
sudo systemctl start mariadb
sudo mysql_secure_installation
mysql -u root -p
CREATE DATABASE vsa;
CREATE DATABASE IF NOT EXISTS database_name;
# or
mysqladmin -u root -p create database_name
USE vsa;
create a new user account.
A user account in MySQL consists of two parts: a user name and hostname.
CREATE USER 'vsa'@'%' IDENTIFIED BY '123';
CREATE USER 'database_user'@'localhost' IDENTIFIED BY 'user_password';
change a user account password
ALTER USER 'vsa'@'%' IDENTIFIED BY '456';
ALTER USER 'database_user'@'localhost' IDENTIFIED BY 'new_password';
List all user accounts
SELECT user, host FROM mysql.user;
desc mysql.user;
Delete user account
DROP USER 'vsa'@'%';
DROP USER 'database_user'@'localhost';
Grant all privileges to a user account over a specific database
GRANT ALL PRIVILEGES ON vsa.* TO 'vsa'@'%';
GRANT ALL PRIVILEGES ON database_name.* TO 'database_user'@'localhost';
Grant all privileges to a user account over all database
GRANT ALL PRIVILEGES ON *.* TO 'database_user'@'localhost';
Grant all privileges to a user account over a specific table from a database
GRANT ALL PRIVILEGES ON database_name.table_name TO 'database_user'@'localhost';
Grant only specific privileges to a user account over a specific database type
GRANT SELECT, INSERT, DELETE ON database_name.* TO database_user@'localhost';
Revoke all privileges from a user account over a specific database
REVOKE ALL PRIVILEGES ON database_name.* TO 'database_user'@'localhost';
SHOW GRANTS FOR 'database_user'@'localhost';
SQL - Structured Query Language
is a domain-specific language used in programming and designed for managing data held in
RDBMS - relational database management system
SHOW databases;
CREATE DATABASE IF NOT EXISTS test;
DROP DATABASE IF EXISTS test;
USE test;
CREATE TABLE books (
isbn CHAR(20) PRIMARY KEY,
title VARCHAR(50),
author_id INT,
publisher_id INT,
year_pub CHAR(4),
description TEXT );
DESCRIBE books;
INSERT INTO books
(title, author_id, isbn, year_pub)
VALUES('The Castle', '1', '0805211063', '1998');
CREATE TABLE authors
(author_id INT AUTO_INCREMENT PRIMARY KEY,
name_last VARCHAR(50),
name_first VARCHAR(50),
country VARCHAR(50) );
INSERT INTO authors
(name_last, name_first, country)
VALUES('Kafka', 'Franz', 'Czech Republic');
INSERT INTO books
(title, author_id, isbn, year_pub)
VALUES('The Trial', '1', '0805210407', '1995'),
('The Metamorphosis', '2', '0553213695', '1995'),
('America', '1', '0805210644', '1995');
SHOW TABLES;
SELECT title
FROM books
LIMIT 5;
SELECT title, name_last
FROM books
JOIN authors USING (author_id);
SELECT title AS 'Kafka Books'
FROM books
JOIN authors USING (author_id)
WHERE name_last = 'Kafka';
UPDATE books
SET title = 'Amerika'
WHERE isbn = '0805210644';
DELETE FROM books
WHERE author_id = '1';
Use the SELECT statement
SELECT isbn, title
FROM books
WHERE author_id = 1
ORDER BY title ASC
LIMIT 5;
SELECT isbn, title,
CONCAT(name_first, ' ', name_last) AS author
FROM books
JOIN authors USING (author_id)
WHERE name_last = 'Dostoevsky'
ORDER BY title ASC
LIMIT 5;
# Use LIKE operator
SELECT isbn, title,
CONCAT(name_first, ' ', name_last) AS author
FROM books
JOIN authors USING (author_id)
WHERE name_last LIKE 'Dostoevsk%'
ORDER BY title ASC
LIMIT 5;
# Some Flags
SELECT DISTINCT HIGH_PRIORITY isbn, title
FROM books
JOIN authors USING (author_id)
WHERE name_last = 'Dostoevsky'
ORDER BY title;
Adding and Changing Data
Install in Arch
sudo pacman -S mariadb
# run the following command as su before start mariadb.service
mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
# Now the mariadb.service can be started and/or enabled with systemd.
systemctl start mariadb
# config recommended security measures
mysql_secure_installation
MariaDB Service's Unit File
/usr/lib/systemd/system/
config files
mysqld --print-defaults
mysql --print-defaults
mysqld --help --verbose | less
Default options are read from the following files in the given order:
/etc/my.cnf ~/.my.cnf
mysql -u //user_name// -p -h //ip_address// -P //port// //db_name//
SELECT User, Host FROM mysql.user;
SELECT User, Host FROM mysql.user WHERE Host <> 'localhost';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'0.0.0.0'
IDENTIFIED BY '1' WITH GRANT OPTION;
SHOW DATABASES;
CREATE DATABASE IF NOT EXISTS test;
USE test;
CREATE TABLE IF NOT EXISTS books (
BookID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Title VARCHAR(100) NOT NULL,
SeriesID INT, AuthorID INT);
CREATE TABLE IF NOT EXISTS authors
(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT);
CREATE TABLE IF NOT EXISTS series
(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT);
INSERT INTO books (Title,SeriesID,AuthorID)
VALUES('The Fellowship of the Ring',1,1),
('The Two Towers',1,1), ('The Return of the King',1,1),
('The Sum of All Men',2,2), ('Brotherhood of the Wolf',2,2),
('Wizardborn',2,2), ('The Hobbbit',0,1);
SHOW TABLES;
+----------------+
| Tables_in_test |
+----------------+
| authors |
| books |
| series |
+----------------+
3 rows in set (0.001 sec)
DESCRIBE books;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| BookID | int(11) | NO | PRI | NULL | auto_increment |
| Title | varchar(100) | NO | | NULL | |
| SeriesID | int(11) | YES | | NULL | |
| AuthorID | int(11) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
4 rows in set (0.002 sec)
SELECT * FROM books;
+--------+----------------------------+----------+----------+
| BookID | Title | SeriesID | AuthorID |
+--------+----------------------------+----------+----------+
| 1 | The Fellowship of the Ring | 1 | 1 |
| 2 | The Two Towers | 1 | 1 |
| 3 | The Return of the King | 1 | 1 |
| 4 | The Sum of All Men | 2 | 2 |
| 5 | Brotherhood of the Wolf | 2 | 2 |
| 6 | Wizardborn | 2 | 2 |
| 7 | The Hobbbit | 0 | 1 |
+--------+----------------------------+----------+----------+
7 rows in set (0.007 sec)
Inserting Data
INSERT INTO books (Title, SeriesID, AuthorID)
VALUES ("Lair of Bones", 2, 2);
Modifying Data
UPDATE books
SET Title = "The Hobbit"
WHERE BookID = 7;
Install the DBMS.
Tune the setup variables according to the hardware, software and usage conditions.
Create the database and tables.
Load the data.
Set up the users and security.
Implement the backup regime.