Maria DB

MariaDB/MySQL Overview

  • Objectives

    • Learn about MariaDB and MySQL

    • Create users and databases in MariaDB

    • Review SQL basics

    • Backup a database

MySQL & MariaDB

  • What is MySQL?

    • Open-source relational database management system (RDBMS).

    • Uses a multithreaded design.

    • Owned by Oracle; forked to create MariaDB due to trademarking issues.

  • Laboratory Focus:

    • Will be using MariaDB in practical applications.

Advantages of RDBMS

  • Historical Context:

    • Flat files (text-based) used to store data previously.

    • Flat files are inefficient, requiring full scans for data processing.

  • Benefits of RDBMS:

    • Enhanced speed and efficiency.

    • Data organization through tables indexed by primary keys.

    • Capability to link tables with foreign keys for relational integrity.

Reasons to Use MySQL/MariaDB

  • Superior performance compared to PostgreSQL and SQLite.

  • Strong community support due to its open-source nature.

  • Cost-effective (no licensing fees unlike Oracle or MSSQL).

  • Lightweight architecture yet robust feature set.

Introduction to SQL

  • Structured Query Language (SQL):

    • Used to manage and manipulate database information.

    • Syntax is key; databases may include proprietary features (e.g., MySQL uses REPLACE).

  • Characteristics of SQL:

    • Declarative: Describes desired outcomes, allowing database to determine execution.

    • Interactive: Direct command input displays results.

    • Embeddable: Can be integrated within scripts (BASH, PHP, Java).

    • Standardized: Defined by an international standards organization.

Components of an RDBMS

  • Database Composition:

    • Comprises multiple schemas; schemas contain multiple tables.

    • In MySQL, databases and schemas are synonymous.

  • Tables:

    • Consist of columns and rows (records).

    • Columns:

    • Define attributes of the table's entity type.

    • Each has a unique name, constraints on data types, uniqueness, etc.

    • Rows (Records):

    • Describe entity instances; must be uniquely identified by a primary key.

    • Can frequently change as data is updated.

Understanding Primary Keys

  • Primary Key:

    • Each table should have exactly one primary key.

    • Must be unique and not null.

    • Can consist of a single or combination of columns.

Installing and Configuring MariaDB

  • Installation Command:

    • apt install mariadb-client mariadb-server

  • Service Management:

    • Service called mysqld, client is accessed via mysql.

    • Ensure database is behind firewall for security.

Securing Your MySQL Installation

  • Securing Command:

    • Run mysql_secure_installation.

    • Actions include:

    • Set root password

    • Remove anonymous users

    • Disallow remote root login

    • Remove test database

MySQL Shell Interactions

  • Common Actions:

    • Log into MySQL shell for operations such as creating databases, users, granting/revoking privileges, etc.

Creating Databases & Users

  • Creating a Database:

    • Command: CREATE DATABASE mit416;

  • Creating Users:

    • Syntax examples:

    • CREATE USER 'student'@localhost IDENTIFIED BY 'test1';

    • Wildcard host example: CREATE USER 'student'@'%' IDENTIFIED BY 'test1';

Granting and Viewing Privileges

  • Grant Privileges:

    • To grant all privileges:

    • GRANT ALL PRIVILEGES ON mit416.* TO 'student'@'localhost';

    • To view users:

    • SELECT user, host, password FROM mysql.user;

Managing Privileges

  • Revoking Privileges:

    • Syntax includes:

    • REVOKE ALL ON exampledb.* FROM 'student'@'localhost';

Table Operations

  • Creating Tables:

    • Use command:

    • CREATE TABLE employee (id INT NOT NULL AUTO_INCREMENT, firstname VARCHAR(20), lastname VARCHAR(20), department VARCHAR(20), PRIMARY KEY (id));

  • Deleting Tables:

    • Command: DROP TABLE employee;

Data Manipulation

  • Inserting Records:

    • Command:

    • INSERT INTO employee VALUES (DEFAULT, 'Franco', 'Iacobacci', 'Networking');

  • Updating Records:

    • Command:

    • UPDATE employee SET department='IT' WHERE id=1;

  • Deleting Records:

    • Command:

    • DELETE FROM employee WHERE id=1;

Viewing Data

  • Select Commands:

    • View all records:

    • SELECT * FROM employee;

    • Filter records based on criteria:

    • SELECT * FROM employee WHERE firstname='Bob';

Exporting a Database

  • Database Export Command:

    • Use:

    • mysqldump -u username -p"password" database_name > File.sql

  • Purpose:

    • Backs up the specified database into a SQL file.

Resetting the Root Password

  • Password Reset Steps:

    1. Stop mysqld service.

    2. Run mysqld_safe --skip-grant-tables &.

    3. Log into MySQL shell using mysql.

    4. Update password:

    • ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';

    1. Execute FLUSH PRIVILEGES; to apply changes.