MariaDB 数据库管理

MariaDB 数据库管理

文章目录

数据库介绍

**数据库,是一个存放计算机数据的仓库。**这个仓库是按照一定的数据结构来对数据进行组织和存储的,我们可以通过数据库提供的多种方法来管理其中的数据。

数据库种类

  • 关系型数据库,把复杂的数据结构归结为简单的二维表形式。在关系型数据库中,对数据的操作几乎全部建立在一个或多个关系表格上,通过对这些关联的表格分类、合并、连接或选取等运算来实现数据库的管理。例如Oracle、MySql。
  • 非关系型数据库 ,也被称为 NoSQL 数据库,本意是"Not Only SQL",而非"NO SQL"的意思,因此,NoSQL的产生并不是要彻底否定关系型数据库,而是作为传统数据库的一个有效补充。例如Redis、Mongodb、Memcached。

MariaDB 介绍

MariaDB数据库管理系统是MySQL数据库的一个分支,主要由开源社区维护,采用GPL授权许可。

  • 表中的一行即为一个元组 ,或称为一条记录
  • 数据表中的每一列称为一个字段(属性),表是由其包含的各种字段定义,每个字段描述了它所含有的数据意义,为每个字段分配一个数据类型,定义它们的数据长度和其他属性。
  • 行和列的交叉位置表示某个属性值

SQL语句

  • 数据查询语言(DQL:Data Query Language):也称为"数据检索语句",用以从表中获得数据,保留字SELECT WHERE,ORDER BY,GROUP BY和HAVING。
  • 数据操作语言(DML:Data Manipulation Language):其语句包括动词INSERT,UPDATE和DELETE。
  • 数据定义语言(DDL):其语句包括动词CREATE和DROP。例如,在数据库中创建新表(CREATE TABLE)、删除表(DROP TABLE),为表添加索引等。
  • 事务处理语言(TPL):它的语句能确保被DML语句影响的表的所有行及时得以更新。TPL语句包括BEGIN TRANSACTION,COMMIT和ROLLBACK。
  • 数据控制语言(DCL):它的语句通过GRANT或REVOKE获得许可,确定单个用户和用户组对数据库对象的访问。

查询数据库列表

  • mysql 数据库,是一个系统数据库,保存数据库用户及其访问权限等信息。
  • INFORMATION_SCHEMA 数据库,保存关于数据库或者数据表的元数据信息。
  • **PERFORMANCE_SCHEMA **数据库,保存数据库服务器性能信息。

server端

bash 复制代码
#安装 MariaDB

[root@server ~ 09:04:18]# yum install -y mariadb-server.x86_64 

#启动服务

[root@server ~ 10:11:12]# systemctl enable mariadb --now

#加固 MariaDB
[root@server ~ 10:12:59]# mysql_secure_installation

[root@server ~ 10:19:00]# mysql -u root -p

#数据库服务器本机创建连接用户
[root@server ~ 10:19:44]# mysql -u root -p123

[root@server ~ 10:48:12]# vim /etc/my.cnf.d/client.cnf

[root@server ~ 10:48:12]# cat /etc/my.cnf.d/client.cnf
#
# These two groups are read by the client library
# Use it for options that affect all clients, but not the server
#


[client]

# This group is not read by mysql client library,
# If you use the same .cnf file for MySQL and MariaDB,
# use it for MariaDB-only client options
[client-mariadb]
user=root
password=123


[root@server ~ 10:48:49]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 23
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

[root@server ~ 10:48:12]# cat /etc/my.cnf.d/client.cnf
#
# These two groups are read by the client library
# Use it for options that affect all clients, but not the server
#


[client]

# This group is not read by mysql client library,
# If you use the same .cnf file for MySQL and MariaDB,
# use it for MariaDB-only client options
[client-mariadb]
user=root
password=123

[root@server ~ 10:48:49]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 23
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

#提权
MariaDB [(none)]> grant all privileges on *.* to demo01;
Query OK, 0 rows affected (0.00 sec)

#刷新
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

#SQL语句不区分大小写,但数据库名称区分大小写。
#创建数据库

MariaDB [(none)]> create database webapp ;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| webapp             |
+--------------------+
4 rows in set (0.00 sec)

#删除数据库
MariaDB [(none)]> drop database webapp;
Query OK, 0 rows affected (0.00 sec)


MariaDB [(none)]> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> create database inventory;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> exit
Bye

[root@server ~ 11:10:04]# mysql -e 'drop database inventory;'

[root@server ~ 11:10:27]# mysql -e 'show databases;'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+

#数据准备

[root@server ~ 11:10:39]# mysql -e 'create database inventory;'

[root@server ~ 11:11:00]# rz -E
rz waiting to receive.

[root@server ~ 11:12:04]# ls
anaconda-ks.cfg  inventory.dump

[root@server ~ 11:12:08]# mysql inventory < inventory.dump

# 查询表列表
MariaDB [(none)]> USE inventory;
MariaDB [inventory]> SHOW TABLES;
+---------------------+
| Tables_in_inventory |
+---------------------+
| category            |
| manufacturer        |
| product             |
+---------------------+
3 rows in set (0.001 sec)

#查询表结构
MariaDB [inventory]> DESCRIBE product;

[root@server ~ 11:12:53]# tail /etc/my.cnf.d/client.cnf 

[client]

# This group is not read by mysql client library,
# If you use the same .cnf file for MySQL and MariaDB,
# use it for MariaDB-only client options
[client-mariadb]
user=root
password=123

#创建表并插入更新记录

MariaDB [inventory]> CREATE TABLE staff(
id INT(11) NOT NULL,
name VARCHAR(100) NOT NULL,
age INT(11)  DEFAULT 10,
id_department INT(11) 
);
Query OK, 0 rows affected (0.017 sec)
MariaDB [inventory]> SHOW TABLES;

MariaDB [inventory]> INSERT INTO staff (id,name,age,id_department)
VALUES (1,'demo01',20,10);

MariaDB [inventory]> UPDATE staff SET age=30 WHERE id=1;

#删除记录和表

MariaDB [inventory]> DELETE FROM staff WHERE id=1 ;

MariaDB [inventory]> DROP TABLE staff ;

client端

bash 复制代码
[root@client ~ 10:11:52]# yum install -y mariadb

[root@client ~ 10:24:04]# mysql -u demo01 -p123 -h 10.1.8.10

[root@client ~ 10:37:35]# vim /etc/my.cnf.d/client.cnf 
[root@client ~ 10:38:49]# cat /etc/my.cnf.d/client.cnf
#
# These two groups are read by the client library
# Use it for options that affect all clients, but not the server
#


[client]

# This group is not read by mysql client library,
# If you use the same .cnf file for MySQL and MariaDB,
# use it for MariaDB-only client options
[client-mariadb]
host=10.1.8.10
port=3306
user=demo01
password=123


[root@client ~ 10:49:06]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 24
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

[root@client ~ 10:49:06]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 24
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

# 查询表列表

MariaDB [mysql]> show tables;

MariaDB [mysql]> use inventory
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [inventory]> show tables;
+---------------------+
| Tables_in_inventory |
+---------------------+
| category            |
| manufacturer        |
| product             |
+---------------------+
3 rows in set (0.00 sec)

MariaDB [inventory]> desc category;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(100) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

#查询表中所有记录所有字段

MariaDB [inventory]> select * from category;
+----+------------+
| id | name       |
+----+------------+
|  1 | Networking |
|  2 | Servers    |
|  3 | Ssd        |
+----+------------+
3 rows in set (0.00 sec)

MariaDB [inventory]> select * from product;
+----+-------------------+---------+-------+-------------+-----------------+
| id | name              | price   | stock | id_category | id_manufacturer |
+----+-------------------+---------+-------+-------------+-----------------+
|  1 | ThinkServer TS140 |  539.88 |    20 |           2 |               4 |
|  2 | ThinkServer RD630 | 2379.14 |    20 |           2 |               4 |
|  3 | RT-AC68U          |  219.99 |    10 |           1 |               3 |
|  4 | X110 64GB         |   73.84 |   100 |           3 |               1 |
+----+-------------------+---------+-------+-------------+-----------------+
4 rows in set (0.01 sec)

#查询表中所有记录特定字段

MariaDB [inventory]> select name,price from product;
+-------------------+---------+
| name              | price   |
+-------------------+---------+
| ThinkServer TS140 |  539.88 |
| ThinkServer RD630 | 2379.14 |
| RT-AC68U          |  219.99 |
| X110 64GB         |   73.84 |
+-------------------+---------+
4 rows in set (0.00 sec)

MariaDB [inventory]> select user,host,password from mysql.user;
+--------+-----------+-------------------------------------------+
| user   | host      | password                                  |
+--------+-----------+-------------------------------------------+
| root   | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root   | 127.0.0.1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root   | ::1       | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| demo01 | %         | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+--------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)

MariaDB [inventory]> select * from product;

#WHERE子句

MariaDB [inventory]> select * from product where stock>=20;
+----+-------------------+---------+-------+-------------+-----------------+
| id | name              | price   | stock | id_category | id_manufacturer |
+----+-------------------+---------+-------+-------------+-----------------+
|  1 | ThinkServer TS140 |  539.88 |    20 |           2 |               4 |
|  2 | ThinkServer RD630 | 2379.14 |    20 |           2 |               4 |
|  4 | X110 64GB         |   73.84 |   100 |           3 |               1 |
+----+-------------------+---------+-------+-------------+-----------------+
3 rows in set (0.00 sec)

MariaDB [inventory]> 
MariaDB [inventory]> 
MariaDB [inventory]> select * from product where id_category in (1,2);
+----+-------------------+---------+-------+-------------+-----------------+
| id | name              | price   | stock | id_category | id_manufacturer |
+----+-------------------+---------+-------+-------------+-----------------+
|  1 | ThinkServer TS140 |  539.88 |    20 |           2 |               4 |
|  2 | ThinkServer RD630 | 2379.14 |    20 |           2 |               4 |
|  3 | RT-AC68U          |  219.99 |    10 |           1 |               3 |
+----+-------------------+---------+-------+-------------+-----------------+
3 rows in set (0.00 sec)

MariaDB [inventory]> select * from manufacturer;

MariaDB [inventory]> select * from product;

MariaDB [inventory]> select * from product where price between 100 and 600;
+----+-------------------+--------+-------+-------------+-----------------+
| id | name              | price  | stock | id_category | id_manufacturer |
+----+-------------------+--------+-------+-------------+-----------------+
|  1 | ThinkServer TS140 | 539.88 |    20 |           2 |               4 |
|  3 | RT-AC68U          | 219.99 |    10 |           1 |               3 |
+----+-------------------+--------+-------+-------------+-----------------+
2 rows in set (0.00 sec)


MariaDB [inventory]> select * from product where id_category=(select id from category where name='Servers');
+----+-------------------+---------+-------+-------------+-----------------+
| id | name              | price   | stock | id_category | id_manufacturer |
+----+-------------------+---------+-------+-------------+-----------------+
|  1 | ThinkServer TS140 |  539.88 |    20 |           2 |               4 |
|  2 | ThinkServer RD630 | 2379.14 |    20 |           2 |               4 |
+----+-------------------+---------+-------+-------------+-----------------+
2 rows in set (0.00 sec)

MariaDB [inventory]> select stock from product;
+-------+
| stock |
+-------+
|    20 |
|    20 |
|    10 |
|   100 |
+-------+
4 rows in set (0.00 sec)

#函数

MariaDB [inventory]> select sum(stock) from product;
+------------+
| sum(stock) |
+------------+
|        150 |
+------------+
1 row in set (0.00 sec)

MariaDB [inventory]> select max(stock) from product;
+------------+
| max(stock) |
+------------+
|        100 |
+------------+
1 row in set (0.00 sec)

MariaDB [inventory]> select avg(stock) from product;
+------------+
| avg(stock) |
+------------+
|    37.5000 |
+------------+
1 row in set (0.00 sec)

MariaDB [inventory]> select count(id) from product;
+-----------+
| count(id) |
+-----------+
|         4 |
+-----------+
1 row in set (0.00 sec)

MariaDB [inventory]> select min(price) from product;
+------------+
| min(price) |
+------------+
|      73.84 |
+------------+
1 row in set (0.00 sec)

MariaDB [inventory]> select * from product where price=(select min(price) from product);
+----+-----------+-------+-------+-------------+-----------------+
| id | name      | price | stock | id_category | id_manufacturer |
+----+-----------+-------+-------+-------------+-----------------+
|  4 | X110 64GB | 73.84 |   100 |           3 |               1 |
+----+-----------+-------+-------+-------------+-----------------+
1 row in set (0.00 sec)

#多表查询

MariaDB [inventory]> SELECT count(product.name)
    -> FROM product,manufacturer 
    -> WHERE product.id_manufacturer = manufacturer.id
    -> AND manufacturer.name='Lenovo';
+---------------------+
| count(product.name) |
+---------------------+
|                   2 |
+---------------------+
1 row in set (0.00 sec)

MariaDB [inventory]> select user,host from mysql.user
    -> ;
+--------+-----------+
| user   | host      |
+--------+-----------+
| demo01 | %         |
| root   | 127.0.0.1 |
| root   | ::1       |
| root   | localhost |
+--------+-----------+
4 rows in set (0.00 sec)

MariaDB [inventory]> update mysql.user set host='%' where host='localhost';

Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [inventory]> select user,host from mysql.user
    -> ;
+--------+-----------+
| user   | host      |
+--------+-----------+
| demo01 | %         |
| root   | %         |
| root   | 127.0.0.1 |
| root   | ::1       |
+--------+-----------+
4 rows in set (0.00 sec)

MariaDB [inventory]> flush privileges;
Query OK, 0 rows affected (0.01 sec)

[root@client ~ 14:22:13]# mysql -u root -p123 -h 10.1.8.10

#远程禁止登陆改回来

MariaDB [mysql]> update mysql.user set host='localhost' where host='%' and user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

#删除表 drop table inventory;

管理 MariaDB 用户

bash 复制代码
#创建用户账户
MariaDB [(none)]> CREATE USER demo01 IDENTIFIED BY '123';

#查看
MariaDB [mysql]> SELECT host,user,password FROM user WHERE user = 'demo01';

#查询用户权限
MariaDB [(none)]> SHOW GRANTS FOR root@localhost;

#授予用户权限
MariaDB [(none)]> GRANT SELECT, UPDATE, DELETE, INSERT
    -> ON inventory.category
    -> TO demo01;

#回收用户权限
MariaDB [(none)]> REVOKE SELECT, UPDATE, DELETE, INSERT
    -> ON inventory.category FROM demo01;
    
#删除用户
MariaDB [(none)]> DROP USER demo01@localhost;

#修改用户密码

# root用户修改普通用户账户密码
MariaDB [(none)]> USE mysql;
MariaDB [(mysql)]> UPDATE user SET password=PASSWORD('123') WHERE user='demo01';

# 普通用户修改自己账户密码
MariaDB [(none)]> SET PASSWORD = PASSWORD('123');
MariaDB [(none)]> FLUSH PRIVILEGES;

数据库连接失败原因

  1. 网络问题
    网络配置、防火墙(ip、ping、mtr)
  2. 数据库问题
    a、数据库服务
    b、用户是否创建
    c、用户权限
    d、数据库是否允许远程登录
    e、客户端连接时候,参数是否正确

忘记 root 用户密码

bash 复制代码
#编辑/etc/my.cnf.d/server.cnf配置文件
[root@server ~ 16:13:42]# vim /etc/my.cnf.d/server.cnf 

#mysqld下添加skip-grant-tables=1

#重启服务
[root@server ~ 16:14:08]# systemctl restart mariadb

[root@server ~ 16:14:25]# mysql -uroot -p123

MariaDB [(none)]> UPDATE mysql.user SET password=PASSWORD('新密码') where USER='root';

MariaDB [(none)]> exit

#注释掉修改的信息

备份和恢复

  • 逻辑备份

    bash 复制代码
    [root@server ~]# mysqldump -u root -p inventory > /backup/inventory.dump
    
    [root@server ~]# mysqldump -u root -p --all-databases > /backup/mariadb.dump
  • 物理备份

    bash 复制代码
    #cp 命令即可
    
    # 停止mariadb服务
    [root@server ~]# systemctl stop mariadb
    
    # 确保数据库数据目录是空
    [root@server ~]# grep '^datadir' /etc/my.cnf.d/mariadb-server.cnf
    datadir=/var/lib/mysql
    
    [root@server ~]# rm -rf /var/lib/mysql/*
    
    # 启动mariadb服务
    
    systemctl start mariadb

MariaDB数据库日志

bash 复制代码
[root@server ~ 16:18:57]# tail /var/log/mariadb/mariadb.log

ne)]> exit

#注释掉修改的信息

复制代码
## 备份和恢复

- 逻辑备份

  ```bash
  [root@server ~]# mysqldump -u root -p inventory > /backup/inventory.dump
  
  [root@server ~]# mysqldump -u root -p --all-databases > /backup/mariadb.dump
  • 物理备份

    bash 复制代码
    #cp 命令即可
    
    # 停止mariadb服务
    [root@server ~]# systemctl stop mariadb
    
    # 确保数据库数据目录是空
    [root@server ~]# grep '^datadir' /etc/my.cnf.d/mariadb-server.cnf
    datadir=/var/lib/mysql
    
    [root@server ~]# rm -rf /var/lib/mysql/*
    
    # 启动mariadb服务
    
    systemctl start mariadb

MariaDB数据库日志

bash 复制代码
[root@server ~ 16:18:57]# tail /var/log/mariadb/mariadb.log
相关推荐
蒋士峰DBA修行之路2 小时前
实验二十六 GaussDB参数调优
数据库·oracle·gaussdb
q***3752 小时前
MySQL输入密码后闪退?
数据库·mysql·adb
张人玉2 小时前
SQLite 快速入门 Cheat Sheet
数据库·sql·sqlite
杨DaB2 小时前
【MySQL】03 数据库的CRUD
数据库·mysql·adb
生信大表哥2 小时前
Python单细胞分析-基于leiden算法的降维聚类
linux·python·算法·生信·数信院生信服务器·生信云服务器
DarkAthena2 小时前
【DuckDB】活用marco以兼容GaussDB的SQL执行
数据库·sql·duckdb
q***3752 小时前
QoS质量配置
开发语言·智能路由器·php
沐浴露z2 小时前
一张思维导图理清【Redis】
数据库·redis·缓存
q***33372 小时前
mysql查看binlog日志
数据库·mysql