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

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)		#创建成功
  • 便于理解,以上创建的表结构如下:

字段名数据类型主键自增
idint
namevarchar(10)
birthdaydatetime
sexchar(5)
passwordchar(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				#更新防火墙

 

欢迎交流学习

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拾柒SHY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值