系统运维-Linux Mariadb数据库基本命令教程

2023-10-31

Mariadb:是MySQL数据库的一个分支 它与MySQL比较有更优的存储引擎 运行速度快等优势

实验环境

  • Rocky 9.0
  • 本地yum

安装Mariadb

yum install mariadb* -y

开启服务并设置一下自启动

systemctl start mariadb		#启动mariadb
systemctl enable mariadb	#设置自启动

初始化Mariadb

mysql_secure_installation	#初始化mariadb

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 						#初安装回车
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n							#是否切换unix_socket身份验证
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] y										#是否更改root登录密码
New password: 						#新密码
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] n									#是否禁止root远程登录
 ... skipping.

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							#是否删除测试数据库并访问
 - 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									#是否重新加载权限表
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!												#初始化完成

用户与权限

mysql -u root -p123456 #使用root用户登录数据库(-p指定密码)
MariaDB [(none)]> select user();					#查询当前登录的用户

+----------------+
| user()         |
+----------------+
| root@localhost |			#当前登录的用户为root
+----------------+
1 row in set (0.001 sec)
MariaDB [(none)]> select user from mysql.user;		#查询数据库中所有用户

+-------------+
| User        |
+-------------+
| mariadb.sys |			#用户1
| mysql       |			#用户2
| root        |			#用户3
+-------------+
3 rows in set (0.002 sec)		#总共3个用户
MariaDB [(none)]> create user test@'%' identified by '123456';		#创建数据库用户
#test为用户名 @为指定在哪个主机上登录 '%'为所有(可设为ip) inentified by指定密码 '密码';
Query OK, 0 rows affected (0.002 sec)		#创建成功
MariaDB [(none)]> drop user 'test';				#删除用户test

Query OK, 0 rows affected (0.001 sec)		#删除成功
grant all on *.* to jack@'%' identified by 'john';				#赋予用户权限
#all为用户操作所有权限 *.*表示对所有数据库(可改为数据库名.表名) to为指定对象 jack为用户 @为指定在哪个主机上登录 '%'为所有(可设为ip) identified by为指定密码'密码'; (根据实际需要删减配置)
Query OK, 0 rows affected (0.002 sec)		#赋权成功
MariaDB [(none)]> use mysql;				#切换数据库
MariaDB [mysql]> select user,host from user;				#查看用户权限分配

+-------------+-----------+
| User        | Host      |
+-------------+-----------+
| jack        | %         |
| mariadb.sys | localhost |
| mysql       | localhost |
| root        | localhost |
+-------------+-----------+
4 rows in set (0.001 sec)
MariaDB [mysql]> grant all privileges on *.* to root@'%' identified by '123456';		#允许root用户远程登录

Query OK, 0 rows affected (0.001 sec)		#设置成功(记得刷新权限)

登录数据库

#本机登录
mysql -u root -p123456			#-u指定用户 -p密码

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.5.13-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)]>				#登录成功
#其他主机登录
mysql -h 192.168.100.200 -u root -p123456			#-h指定数据库服务器 -u指定用户 -p密码

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.5.13-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 |		#数据库1
| mysql              |		#数据库2
| performance_schema |		#数据库3
+--------------------+
3 rows in set (0.001 sec)	#共3个数据库
MariaDB [(none)]> create database userdb;		#创建名为userdb的数据库

Query OK, 1 row affected (0.001 sec)	#创建成功
MariaDB [(none)]> drop database userdb;		#删除名为userdb的数据库

Query OK, 0 rows affected (0.002 sec)	#创建成功
MariaDB [(none)]> use userdb;				#使用名为userdb的数据库(切换)

Database changed
MariaDB [userdb]>			#切换成功

数据表操作

MariaDB [userdb]> create table userinfo(id int primary key auto_increment,name varchar(10),birthday datetime,sex char(5),password char(200));
			#创建名为userinfo的表		primary key为主键	auto_increment为自增
Query OK, 0 rows affected (0.008 sec)		#创建成功
  • 便于理解,以上创建的表结构如下:

字段名 数据类型 主键 自增
id int
name varchar(10)
birthday datetime
sex char(5)
password char(200)
MariaDB [userdb]> show tables;				#查询数据库所有表

+------------------+
| Tables_in_userdb |
+------------------+
| userinfo         |		#表1
+------------------+
1 row in set (0.001 sec)		#共1个表

插入数据

MariaDB [userdb]> insert into userinfo values('1','user1','1995-7-1','nan',password('user1'));
		#insert into为选择数据表 userinfo为数据表名 values为数据(1为id,user1为name,1995-7-1为birthday,nan为sex,password('user1')为password函数加密密码为user1)
Query OK, 1 row affected (0.004 sec)		#插入数据成功
#---------------------------------------------------------------------
MariaDB [userdb]> insert into userinfo values('2','user2','1995-9-1','nv',password('user2'));

Query OK, 1 row affected (0.001 sec)
MariaDB [userdb]> select * from userinfo;		#查询userinfo表数据

+----+-------+---------------------+------+-------------------------------------------+
| id | name  | birthday            | sex  | password                                  |
+----+-------+---------------------+------+-------------------------------------------+
|  1 | user1 | 1995-07-01 00:00:00 | nan  | *34D3B87A652E7F0D1D371C3DBF28E291705468C4 |
|  2 | user2 | 1995-09-01 00:00:00 | nv   | *12A20BE57AF67CBF230D55FD33FBAF5230CFDBC4 |
+----+-------+---------------------+------+-------------------------------------------+
2 rows in set (0.001 sec)		#查询成功

表结构

MariaDB [userdb]> desc userinfo;			#查询userinfo表的结构
#	字段		类型				   主键			  额外
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| name     | varchar(10) | YES  |     | NULL    |                |
| birthday | datetime    | YES  |     | NULL    |                |
| sex      | char(5)     | YES  |     | NULL    |                |
| password | char(200)   | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
5 rows in set (0.003 sec)
alter table userinfo add height float after name;	#在userinfo表中name字段后面添加新字段height数据类型为float

Query OK, 0 rows affected (0.007 sec)
Records: 0  Duplicates: 0  Warnings: 0		#添加成功
MariaDB [userdb]> desc userinfo;			#再次查询userinfo表的结构

+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| name     | varchar(10) | YES  |     | NULL    |                |
| height   | float       | YES  |     | NULL    |                |
| birthday | datetime    | YES  |     | NULL    |                |
| sex      | char(5)     | YES  |     | NULL    |                |
| password | char(200)   | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
6 rows in set (0.002 sec)
MariaDB [userdb]> update userinfo set height='1.61' where id='1';
	#更新表结构 在height字段中id=1添加数据为1.61
Query OK, 1 row affected (0.003 sec)
Rows matched: 1  Changed: 1  Warnings: 0		#添加成功
#---------------------------------------------------------------------
MariaDB [userdb]> update userinfo set height='1.62' where id='2';

Query OK, 1 row affected (0.001 sec)
Rows matched: 1  Changed: 1  Warnings: 0
MariaDB [userdb]> select * from userinfo;		#再次查询userinfo表中的数据

+----+-------+--------+---------------------+------+-------------------------------------------+
| id | name  | height | birthday            | sex  | password                                  |
+----+-------+--------+---------------------+------+-------------------------------------------+
|  1 | user1 |   1.61 | 1995-07-01 00:00:00 | nan  | *34D3B87A652E7F0D1D371C3DBF28E291705468C4 |
|  2 | user2 |   1.62 | 1995-09-01 00:00:00 | nv   | *12A20BE57AF67CBF230D55FD33FBAF5230CFDBC4 |
+----+-------+--------+---------------------+------+-------------------------------------------+
2 rows in set (0.001 sec)

导出数据表

#在本机上导出
mysqldump -u root -p123456 userdb userinfo > /mysql/mysql.sql
# -u指定用户名 -p密码 userdb为导出数据库名 userinfo为数据表名 /mysql/mysql.sql为导出路径
#在其他主机上导出
mysqldump -h 192.168.100.200 -u root -p123456 userdb userinfo > /mysql/mysql.sql
# -h指定数据库服务器ip -u指定用户名 -p密码 userdb为导出数据库名 userinfo为数据表名 /mysql/mysql.sql为导出路径

防火墙规则

firewall-cmd --zone=public --add-port=3306/tcp --permanent		#防火墙放行mariadb
#--------------------
firewall-cmd --reload				#更新防火墙

 

欢迎交流学习

 

 

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

系统运维-Linux Mariadb数据库基本命令教程 的相关文章

随机推荐

  • 数据指标应用场景说明

    科技飞速发展的时代 企业信息化建设会越来越完善 越来越体系化 当今数据时代背景下更加强调 重视数据的价值 以数据说话 通过数据为企业提升渠道转化率 改善企业产品 实现精准运营 为企业打造自助模式的数据分析成果 以数据驱动决策 数据指标体系是
  • uniapp掉完接口后刷新当前页面方法

    uniapp掉完接口后刷新当前页面方法 掉完接口 里面加下面这个方法uni redirectTo setTimeout gt uni redirectTo 当前页面路由 url pages property mutualrotation m
  • python3 pyinstaller打包exe运行无限创建进程问题 multiprocessing windows

    mulitprocessing模块在window环境下使用有诸多限制 需要注意 其中之一就是在main py下使用multiprocessing启动子进程问题 该问题在pycharm中没有 pycharm环境下运行调试都是正常的 当使用py
  • 插入排序算法笔记

    插入排序 1 最简单的排序算法 2 在增量排序中有很高的效率 比如已经存在成绩排序 要插入一个新的成绩并且排序 3 不需要额外的存储空间 属于内部排序 4 时间复杂度为O n 2 首先 定义数组的形式为 num MAX 1 MAX是已经定义
  • 【计算机组成原理笔记】1.1计算机系统简介

    1 1计算机系统简介 计算机系统由硬件 软件两部分组成 硬件是看得到摸得到的实体 如硬盘 显卡 主板等 软件程序通常寄寓于各种媒体 如RAM ROM 磁带 光盘等 RAM 随机存取存储器 英语 Random Access Memory 缩写
  • 【信号与系统】1、初步认识信号与系统

    文章目录 1 信号的概念 1 1 信号的定义 1 2 因果 逆因果信号的概念 1 2 1 因果信号 1 2 2 逆因果信号 1 2 3 时限信号 1 2 4 右边信号 1 2 5 左边信号 1 2 6 双边信号 2 信号的分类 2 1 确定
  • 浅显易懂 SQLite3 笔记(04)— SQL数据更新(增加、删除、修改)

    文章目录 前言 一 插入数据 二 修改数据 三 删除数据 总结 前言 我们在上一篇博文 浅显易懂 SQLite3 笔记 03 SQL数据查询 超级详细 学习了如何使用SQL语句进行数据查询 本篇 我们将继续学习如何使用SQL语句进行数据更新
  • ubuntu14.04 64bit 安装 && 破解quartus13.0 记录

    安装文件 Quartus 13 0 0 156 linux iso Quartus 13 0 0 156 devices 1 iso 1 挂载 sudo mount o loop Quartus 13 0 0 156 linux iso m
  • CocoaPods:“Use the `$(inherited)` flag” or “Remove the build settings from the target”

    问题背景 解决方法 总结 阅读之前注意 本文阅读建议用时 5min 问题背景 CocoaPods导库的时候 出现了如下情况 The project Debug target overrides the PODS CONFIGURATION
  • docker 复制镜像和复制容器

    复制镜像和复制容器都是通过保存为新镜像而进行的 具体为 保存镜像 docker save ID gt xxx tar docker load lt xxx tar 保存容器 docker export ID gt xxx tar docke
  • C#之Yield Return语法的作用和好处

    还是和以前一样 我先上代码 请大家先拿到我的代码或者你跟着敲 运行看效果 以及理解每行带代码的作用 我们要带着为什么要用Yield这个关键字 不用可以吗这个目的去学知识 我相信会更加的有意思 首先我贴出平时正常输出偶数集合的办法 学习Yie
  • 编译内核函数copy_from_user()和copy_to_user()

    文章目录 一 定义 二 例子 三 问题 1 隐式声明函数 copy from user 2 copy from user 缓冲区爆炸 一 定义 copy from user rwbuf buf count 作用 从用户空间复制到内核空间 c
  • 用switch语句实现简单的取款机程序

    大家好 今天给大家分享一个简单的取款机程序 相关函数 switch语句 分支语句 case语句 分支条件 break语句 结束循环 default语句 其他分支 演示效果 试试存款 存款成功 再试试取款 取款也成功 代码部分 源文件名 ma
  • Anaconda安装和激活

    一 Anaconda下载地址 https mirrors tuna tsinghua edu cn anaconda archive C M O D 说明 使用paddlepaddle需要先安装python环境 这里我们选择python集成
  • java判断平衡二叉树 - Kaiqisan

    大家好 都吃晚饭了吗 我是Kaiqisan 是一个已经走出社恐的一般生徒 今天还是二叉树的内容 首先明确一个概念 何为平衡二叉树 答 左子树和右子树高度差小于等于1 所以判断一个数是否为平衡二叉树的时候需要遍历所有的子树 因为概念的限制 即
  • Kafka Stream

    文章目录 一 Kafka Stream背景 1 Kafka Stream是什么 2 什么是流式计算 3 为什么要有Kafka Stream 二 Kafka Stream架构 1 Kafka Stream整体架构 2 Processor To
  • js判断对象数组中的元素是否存在重复

    1 使用 Array some 方法和自定义比较函数 使用 Array some 方法遍历数组 对每个元素执行自定义的比较函数 比较函数使用 Array findIndex 方法来查找与当前元素相等且索引不同的元素 如果找到则表示存在重复元
  • Java笔试题

    精选30道Java笔试题解答 都是一些非常非常基础的题 是我最近参加各大IT公司笔试后靠记忆记下来的 经过整理献给与我一样参加各大IT校园招聘的同学们 纯考Java基础功底 老手们就不用进来了 免得笑话我们这些未出校门的孩纸们 但是IT公司
  • EasyExcel——多sheet、有合并单元格的excel导入

    EasyExcel 读Excel官方文档链接 maven依赖
  • 系统运维-Linux Mariadb数据库基本命令教程

    Mariadb 是MySQL数据库的一个分支 它与MySQL比较有更优的存储引擎 运行速度快等优势 实验环境 Rocky 9 0 本地yum 安装Mariadb yum install mariadb y 开启服务并设置一下自启动 syst