Linux巩固篇018-Linux MariaDB 数据库管理系统

简介: 纸上得来终觉浅,绝知此事要躬行

前言

身为一个三年的运维工程师,从开发转测开再转运维,都是不断学习的过程,未必开发才是最优秀的,找到适合自己的职业不断深耕,你也会在自己的行业大放光彩,本系列依照《Linux就该这么学》系列随书学习练习操作,将一些课本上不顺畅的地方,全部以最简方式免费开源展示给大家,资源大家可以自行百度,也希望大家多关注刘遄老师的第二版关于centos8的丛书,学习最前沿的Linux相关技术。

常用命令汇总

数据库管理系统

数据库是指按照某些特定结构来存储数据资料的数据仓库

初始化 MariaDB 服务

[root@mail ~]#  yum install mariadb mariadb-server -y

Loaded plugins: fastestmirror, langpacks

Existing lock /var/run/yum.pid: another copy is running as pid 9523.

Another app is currently holding the yum lock; waiting for it to exit...

 The other application is: PackageKit

   Memory :  37 M RSS (394 MB VSZ)

   Started: Mon Jul 31 18:43:07 2023 - 00:01 ago

   State  : Running, pid: 9523

报这个错说明yum被别的占用了,所以把yum相关运行的文件删一下就行

[root@mail ~]# rm -f /var/run/yum.pid

[root@mail ~]#  yum install mariadb mariadb-server -y

初始化操作涉及下面 5 个步骤。

1.设置 root 管理员在数据库中的密码值(注意,该密码并非 root 管理员在系统中的密码, 这里的密码值默认应该为空,可直接按回车键)。

2.设置 root 管理员在数据库中的专有密码。

3.随后删除匿名账户,并使用 root 管理员从远程登录数据库,以确保数据库上运行的业 务的安全性。

4.删除默认的测试数据库,取消测试数据库的一系列访问权限。

5.刷新授权列表,让初始化的设定立即生效

启动

[root@mail ~]# systemctl start mariadb

配置

[root@mail ~]# mysql_secure_installation

Enter current password for root (enter for none):  当前数据库密码为空,直接按回车键

OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB

root user without the proper authorisation.

Set root password? [Y/n] y(设置密码)

New password:  输入要为 root 管理员设置的数据库密码

Re-enter new password: 再次输入密码

Password updated successfully!

Reloading privilege tables..

... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone

to log into MariaDB without having to have a user account created for

them.  This is intended only for testing, and to make the installation

go a bit smoother.  You should remove them before moving into a

production environment.

Remove anonymous users? [Y/n] y(删除匿名账户)

... Success!

Normally, root should only be allowed to connect from 'localhost'.  This

ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y(禁止 root 管理员从远程登录)

... Success!

By default, MariaDB comes with a database named 'test' that anyone can

access.  This is also intended only for testing, and should be removed

before moving into a production environment.

Remove test database and access to it? [Y/n] y(删除 test 数据库并取消对它的访问权限)

- Dropping test database...

... Success!

- Removing privileges on test database...

... Success!

Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.

Reload privilege tables now? [Y/n] y(刷新授权表,让初始化后的设定立即生效)

生产环境中都需要使用站库分离的技术,如果需要让 root 管理员远程访问数据库,可在上面的初始化操作中设置策略,以允许 root 管 理员从远程访问。还需要设置防火墙,使其放行对数据库服务程序的访问请求,数据库 服务程序默认会占用 3306 端口,在防火墙策略中服务名称统一叫作 mysql:

[root@mail ~]# firewall-cmd --permanent --add-service=mysql

success

[root@mail ~]# firewall-cmd --reload

success

登录

[root@mail ~]# mysql -u root -p  

Enter password:  此处输入 root 管理员在数据库中的密码

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 10

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)]>  

MariaDB [(none)]> show databases;(展示库)

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

+--------------------+

3 rows in set (0.00 sec)

MariaDB [(none)]> set password = password('linux');(修改密码)

Query OK, 0 rows affected (0.00 sec)

管理账户以及授权

创建数据库管理账户

数据库大小写不敏感,且以分号结尾

MariaDB [(none)]> create user weihongbin@localhost identified by 'linux';

Query OK, 0 rows affected (0.01 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]> select host,user,password from user where user='weihongbin';

+-----------+------------+-------------------------------------------+

| host      | user       | password                                  |

+-----------+------------+-------------------------------------------+

| localhost | weihongbin | *6F3CAE7C3BBB2A5B5D933738682953BC21AEBEE7 |

+-----------+------------+-------------------------------------------+

1 row in set (0.00 sec)

使用grant命令授权并查看权限

image.png

MariaDB [mysql]> grant select,update,delete,insert on mysql.user to weihongbin@localhost;

Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> show grants for weihongbin@localhost;

+-------------------------------------------------------------------------------------------------------------------+

| Grants for weihongbin@localhost                                                                                   |

+-------------------------------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'weihongbin'@'localhost' IDENTIFIED BY PASSWORD '*6F3CAE7C3BBB2A5B5D933738682953BC21AEBEE7' |

| GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.`user` TO 'weihongbin'@'localhost'                                |

+-------------------------------------------------------------------------------------------------------------------+

2 rows in set (0.00 sec)

移除授权的命令(revoke)与授权命令(grant)不同之外,其余部分都 是一致的

MariaDB [(none)]> revoke select,update,delete,insert on mysql.user from weihongbin@localhost;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show grants for weihongbin@localhost;

+-------------------------------------------------------------------------------------------------------------------+

| Grants for weihongbin@localhost                                                                                   |

+-------------------------------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'weihongbin'@'localhost' IDENTIFIED BY PASSWORD '*6F3CAE7C3BBB2A5B5D933738682953BC21AEBEE7' |

+-------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

创建数据库与表单

一个数据库可以存放多个数据表,数据表单是数据库中 最重要最核心的内容

image.png

创建库

MariaDB [(none)]> create database linux;

Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| linux              |

| mysql              |

| performance_schema |

+--------------------+

4 rows in set (0.00 sec)

创建表(定义存储数据内容的结构:名字、价格、页)

MariaDB [(none)]> use linux;

Database changed

MariaDB [linux]> create table test (name char(15),price int,pages int);

Query OK, 0 rows affected (0.00 sec)

MariaDB [linux]> describe test;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| name  | char(15) | YES  |     | NULL    |       |

| price | int(11)  | YES  |     | NULL    |       |

| pages | int(11)  | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

3 rows in set (0.02 sec)

管理表单及数据

插入数据

MariaDB [(none)]> use linux

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 [linux]> insert into test(name,price,pages) values('weihongbin','100','1000');

Query OK, 1 row affected (0.01 sec)

MariaDB [linux]> select * from test;

+------------+-------+-------+

| name       | price | pages |

+------------+-------+-------+

| weihongbin |   100 |  1000 |

+------------+-------+-------+

1 row in set (0.00 sec)

修改

MariaDB [linux]> update test set price=55;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [linux]> select price from test;

+-------+

| price |

+-------+

|    55 |

+-------+

1 row in set (0.00 sec)

删除

MariaDB [linux]> delete from test;

Query OK, 1 row affected (0.00 sec)

MariaDB [linux]> select * from test;

Empty set (0.00 sec)

条件查询

1.做数据

MariaDB [linux]> insert into test(name,price,pages) values('weihongbin1','100','100');

Query OK, 1 row affected (0.00 sec)

MariaDB [linux]> insert into test(name,price,pages) values('weihongbin2','200','200');

Query OK, 1 row affected (0.00 sec)

MariaDB [linux]> insert into test(name,price,pages) values('weihongbin3','300','300');

Query OK, 1 row affected (0.00 sec)

MariaDB [linux]> insert into test(name,price,pages) values('weihongbin4','400','400');

Query OK, 1 row affected (0.00 sec)

where命令

image.pngMariaDB [linux]> select * from test where price>99;

+-------------+-------+-------+

| name        | price | pages |

+-------------+-------+-------+

| weihongbin1 |   100 |   100 |

| weihongbin2 |   200 |   200 |

| weihongbin3 |   300 |   300 |

| weihongbin4 |   400 |   400 |

+-------------+-------+-------+

4 rows in set (0.00 sec)

数据库的备份及恢复

mysqldump 命令用于备份数据库数据,格式为“mysqldump [参数] [数据库名称]”

[root@mail ~]# mysqldump -u root -p linux > /root/linux.dump

Enter password:  

[root@mail ~]# mysql -u root -p  

Enter password:  

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 4

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)]> drop database linux;

Query OK, 1 row affected (0.03 sec)

MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

+--------------------+

3 rows in set (0.00 sec)

MariaDB [(none)]> create database linux;

Query OK, 1 row affected (0.00 sec)

必须先把库建起来

接下来是数据导入

[root@mail ~]# mysql -u root -p linux < /root/linux.dump  

Enter password:  

[root@mail ~]# mysql -u root -p  

Enter password:  

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 6

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)]> use linux

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 [linux]> show tables;

+-----------------+

| Tables_in_linux |

+-----------------+

| test            |

+-----------------+

1 row in set (0.00 sec)

MariaDB [linux]> describe test;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| name  | char(15) | YES  |     | NULL    |       |

| price | int(11)  | YES  |     | NULL    |       |

| pages | int(11)  | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

3 rows in set (0.00 sec)

结语

简问简答

初始化 MariaDB 或 MySQL 数据库管理系统的命令是什么?

答:是 mysql_secure_installation 命令,建议每次安装 MariaDB 或 MySQL 数据库管理系统 后都执行这条命令


用来查看已有数据库或数据表单的命令是什么?

答:要查看当前已有的数据库列表,需执行 SHOW databases;命令;要查看已有的数据表单 列表,则需执行 SHOW tables;命令。


切换至某个指定数据库的命令是什么?

答:执行“use 数据库名称”命令即可切换成功。


若想针对某个账户进行授权或取消授权操作,应该执行什么命令?

答:针对账户进行授权,需执行 GRANT 命令;取消授权则需执行 REVOKE 命令。


若只想查看 mybook 表单中的 name 字段,应该执行什么命令?

答:应执行 SELECT name FROM mybook 命令。


要想把 linuxprobe 数据库中的内容导出为一个文件(保存到 root 管理员的家目录中),应该执 行什么命令?

答:应执行 mysqldump -u root -p linuxprobe > /root/linuxprobeDB.dump 命令

如果想根据教程实践的朋友们可以通过阿里云ecs服务器免费试用和低价购买,入口如下

入口一:新人免费试用

入口二:大学生免费试用

入口三:低价服务器购买

入口四:低价服务器购买2

入口五:建站特惠购买

目录
相关文章
|
1月前
|
Ubuntu Linux Anolis
Linux系统禁用swap
本文介绍了在新版本Linux系统(如Ubuntu 20.04+、CentOS Stream、openEuler等)中禁用swap的两种方法。传统通过注释/etc/fstab中swap行的方式已失效,现需使用systemd管理swap.target服务或在/etc/fstab中添加noauto参数实现禁用。方法1通过屏蔽swap.target适用于新版系统,方法2通过修改fstab挂载选项更通用,兼容所有系统。
144 3
Linux系统禁用swap
|
1月前
|
Linux
Linux系统修改网卡名为eth0、eth1
在Linux系统中,可通过修改GRUB配置和创建Udev规则或使用systemd链接文件,将网卡名改为`eth0`、`eth1`等传统命名方式,适用于多种发行版并支持多网卡配置。
190 3
|
1月前
|
Ubuntu 安全 关系型数据库
安装MariaDB服务器流程介绍在Ubuntu 22.04系统上
至此, 您已经在 Ubuntu 22.04 系统上成功地完成了 MariadB 的标准部署流程,并且对其进行基础但重要地初步配置加固工作。通过以上简洁明快且实用性强大地操作流程, 您现在拥有一个待定制与使用地强大 SQL 数据库管理系统。
143 18
|
1月前
|
Ubuntu 安全 关系型数据库
安装MariaDB服务器流程介绍在Ubuntu 22.04系统上
至此, 您已经在 Ubuntu 22.04 系统上成功地完成了 MariadB 的标准部署流程,并且对其进行基础但重要地初步配置加固工作。通过以上简洁明快且实用性强大地操作流程, 您现在拥有一个待定制与使用地强大 SQL 数据库管理系统。
137 15
|
Ubuntu Linux 网络安全
Linux系统初始化脚本
一款支持Rocky、CentOS、Ubuntu、Debian、openEuler等主流Linux发行版的系统初始化Shell脚本,涵盖网络配置、主机名设置、镜像源更换、安全加固等多项功能,适配单/双网卡环境,支持UEFI引导,提供多版本下载与持续更新。
165 0
Linux系统初始化脚本
|
2月前
|
运维 Linux 开发者
Linux系统中使用Python的ping3库进行网络连通性测试
以上步骤展示了如何利用 Python 的 `ping3` 库来检测网络连通性,并且提供了基本错误处理方法以确保程序能够优雅地处理各种意外情形。通过简洁明快、易读易懂、实操性强等特点使得该方法非常适合开发者或系统管理员快速集成至自动化工具链之内进行日常运维任务之需求满足。
155 18
|
1月前
|
安全 Linux Shell
Linux系统提权方式全面总结:从基础到高级攻防技术
本文全面总结Linux系统提权技术,涵盖权限体系、配置错误、漏洞利用、密码攻击等方法,帮助安全研究人员掌握攻防技术,提升系统防护能力。
156 1
|
1月前
|
监控 安全 Linux
Linux系统提权之计划任务(Cron Jobs)提权
在Linux系统中,计划任务(Cron Jobs)常用于定时执行脚本或命令。若配置不当,攻击者可利用其提权至root权限。常见漏洞包括可写的Cron脚本、目录、通配符注入及PATH变量劫持。攻击者通过修改脚本、创建恶意任务或注入命令实现提权。系统管理员应遵循最小权限原则、使用绝对路径、避免通配符、设置安全PATH并定期审计,以防范此类攻击。
627 1
|
1月前
|
安全 关系型数据库 数据管理
阿里云数据库:构建高性能与安全的数据管理系统
阿里云数据库提供RDS、PolarDB、Tair等核心产品,具备高可用、弹性扩展、安全合规及智能运维等技术优势,广泛应用于电商、游戏、金融等行业,助力企业高效管理数据,提升业务连续性与竞争力。
|
2月前
|
缓存 监控 Linux
Linux系统清理缓存(buff/cache)的有效方法。
总结而言,在大多数情形下你不必担心Linux中buffer与cache占用过多内存在影响到其他程序运行;因为当程序请求更多内存在没有足够可用资源时,Linux会自行调整其占有量。只有当你明确知道当前环境与需求并希望立即回收这部分资源给即将运行重负载任务之前才考虑上述方法去主动干预。
1023 10

热门文章

最新文章