MariaDB数据库

MariaDB数据库

MariaDB数据库

​ 开发MariaDB数据库的开发人员以前是从事开发MySQL,但是MySQL被Oracle公司收购了,从开源软件转变为闭源软件,之后MySQL的开发者们就离开了公司,再次创建了一个名为MariaDB的数据库,可以说,MariaDB和MySQL在性能上基本保持一致,两者的操作命令也十分相似。从务实的角度来讲,在掌握了MariaDB数据库的命令和基本操作之后,在今后的工作中即使遇到MySQL数据库,也可以快速上手。

MariaDB-百度百科

MariaDB

​ 当前,谷歌、维基百科等技术领域决定将MySQL数据库上的业务转移到MariaDB数据库,Linux开源系统的领袖红帽公司也决定在RHEL 7、CentOS 7以及最新的Fedora系统中,将MariaDB作为默认的数据库管理系统,而且红帽公司更是首次将数据库知识加入到了RHCE认证的考试内容中。随后,还有数十个常见的Linux系统(如openSUSE、Slackware等)也作出了同样的表态。

[外链图片转存失败(img-pb8Vr5oC-1563461033240)(https://s2.ax1x.com/2019/07/01/Z819C8.png)]

初始化MariaDB服务

​ 相较于MySQL,MariaDB数据库管理系统有了很多新鲜的扩展特性,例如对微秒级别的支持、线程池、子查询优化、进程报告等。在配置妥当Yum软件仓库后,即可安装部署MariaDB数据库主程序及服务端程序了。

[root@server222 ~]# tail -n 1 /etc/fstab
/dev/cdrom      /mnt/cdrom      iso9660                 defaults        0 0
[root@server222 ~]# mount -a
[root@server222 ~]# yum install -y mariadb mariadb-server

安装完成后加入到开机启动项

[root@server222 ~]# systemctl restart mariadb
[root@server222 ~]# systemctl enable mariadb

初始化5步骤

  1. 设置root管理员在数据库中的密码值(注意,该密码并非root管理员在系统中的密码,这里的密码值默认应该为空,可直接按回车键)。
  2. 设置root管理员在数据库中的专有密码。
  3. 随后删除匿名账户,并使用root管理员从远程登录数据库,以确保数据库上运行的业务的安全性。
  4. 删除默认的测试数据库,取消测试数据库的一系列访问权限。
  5. 刷新授权列表,让初始化的设定立即生效。
[root@server222 ~]# mysql_secure_installation

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
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

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: # 设置创建数据库管理员密码
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 #删除text数据库并取消对它的访问权限
 - 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!
[root@server222 ~]#

之后我们还要设置防火墙,使其放行对数据库服务程序的访问请求,数据库服务程序默认会占用3306端口,在防火墙策略中服务名称统一叫作mysql

[root@server222 ~]# firewall-cmd --permanent --add-service=mysql
success
[root@server222 ~]# firewall-cmd --reload
success

最后,我们可以开始首次登陆MariaDB数据库

-u 指定哪一位用户登陆

-p 验证该用户在数据库中的密码值

[root@server222 ~]# mysql -u root -p
Enter password: #此处输入您刚才设置的密码
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.56-MariaDB MariaDB Server

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

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

MariaDB [(none)]>

在登陆MariaDB数据库执行命令时候,每一次都要在命令结尾以分号(;)结尾

MariaDB [(none)]> SHOW databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
使用数据库命令更改密码
MariaDB [(none)]> SET password = PASSWORD('abccba');
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
[root@server222 ~]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@server222 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.

管理用户以及授权

创建专用的数据库管理账户
MariaDB [mysql]> CREATE USER abc@localhost IDENTIFIED BY '123321';
Query OK, 0 rows affected (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

MariaDB [mysql]> SELECT HOST,USER,PASSWORD FROM user WHERE USER="abc";
+-----------+------+-------------------------------------------+
| HOST      | USER | PASSWORD                                  |
+-----------+------+-------------------------------------------+
| localhost | abc  | *437F1809645E0A92DAB553503D2FE21DB91270FD |
+-----------+------+-------------------------------------------+
1 row in set (0.00 sec)
授权:grant
命令作用
GRANT 权限 ON 数据库.表单名称 TO 用户名@主机名对某个特定数据库中的特定表单给予授权
GRANT 权限 ON 数据库.* TO 用户名@主机名对某个特定数据库中的所有表单给予授权
GRANT 权限 ON . TO 用户名@主机名对所有数据库及所有表单给予授权
GRANT 权限1,权限2 ON 数据库.* TO 用户名@主机名对某个数据库中的所有表单给予多个授权
GRANT ALL PRIVILEGES ON . TO 用户名@主机名对所有数据库及所有表单给予全部授权(需谨慎操作)
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]> GRANT SELECT,UPDATE,DELETE,INSERT ON mysql.user TO abc@localhost;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> SHOW GRANTS FOR abc@localhost;                                 +------------------------------------------------------------------------------------------------------------+
| Grants for abc@localhost                                                                                   |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'abc'@'localhost' IDENTIFIED BY PASSWORD '*437F1809645E0A92DAB553503D2FE21DB91270FD' |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.`user` TO 'abc'@'localhost'                                |
+------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

经过一系列的操作,我们再切入到abc用户

[root@server222 ~]# mysql -u abc -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 22
Server version: 5.5.56-MariaDB MariaDB Server

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

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

MariaDB [(none)]> show database;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'database' at line 1
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
+--------------------+
2 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;
+-----------------+
| Tables_in_mysql |
+-----------------+
| user            |
+-----------------+
1 row in set (0.00 sec)
收回权限命令:revoke
[root@server222 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 24
Server version: 5.5.56-MariaDB MariaDB Server

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

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

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]> REVOKE SE
SECOND              SELECT              SERIALIZABLE
SECOND_MICROSECOND  SENSITIVE           SESSION
SECURITY            SEPARATOR           SESSION_USER
SEC_TO_TIME         SERIAL              SET
MariaDB [mysql]> REVOKE SELECT
Display all 1116 possibilities? (y or n)
MariaDB [mysql]> REVOKE SELECT,UPDATE,DELETE,INSERT ON mysql.user FROM abc@localhost;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> SHOW GRANTS FOR abc@localhost;
+------------------------------------------------------------------------------------------------------------+
| Grants for abc@localhost                                                                                   |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'abc'@'localhost' IDENTIFIED BY PASSWORD '*437F1809645E0A92DAB553503D2FE21DB91270FD' |
+------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

创建数据库与表单

自定义数据库表结构
用法作用
CREATE database 数据库名称。创建新的数据库
DESCRIBE 表单名称;描述表单
UPDATE 表单名称 SET attribute=新值 WHERE attribute > 原始值;更新表单中的数据
USE 数据库名称;指定使用的数据库
SHOW databases;显示当前已有的数据库
SHOW tables;显示当前数据库中的表单
SELECT * FROM 表单名称;从表单中选中某个记录值
DELETE FROM 表单名 WHERE attribute=值;从表单中删除某个记录值

现在我们创建一个名为tom的数据库

MariaDB [(none)]> CREATE DATABASE tom;
Query OK, 1 row affected (0.00 sec)

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

想要创建数据表单,首先需要切换到指定的数据库中,我们现在在tom数据库中创建表单mybook。然后进行表单的初始化,即定义存储数据内容的结构。我们分别定义3个字段项,其中,长度为15个字符的字符型字段name用来存放图书名称,整型字段price和pages分别存储图书的价格和页数。

MariaDB [(none)]> use tom;
Database changed
MariaDB [tom]> CREATE TABLE mybook (name char(15),price int,pages int);
Query OK, 0 rows affected (0.00 sec)

MariaDB [tom]> DESCRIBE mybook;
+-------+----------+------+-----+---------+-------+
| 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 [tom]>

管理表单及数据

​ 接下来向mybook数据表单中插一条图书信息。为此需要使用INSERT命令,并在命令中写清表单名称以及对应的字段项。执行该命令之后即可完成图书写入信息。下面我们使用该命令插入一条图书信息,其中书名为gelintonghua,价格和页数分别是60元和518页。在命令执行后也就意味着图书信息已经成功写入到数据表单中,然后就可以查询表单中的内容了。我们在使用select命令查询表单内容时,需要加上想要查询的字段。

MariaDB [tom]> INSERT INTO mybook(name,price,pages) VALUES('gelintonghua','60', '518');
Query OK, 1 row affected (0.00 sec)

如果想查看表单中的所有内容,则可以使用星号(*)通配符来显示

MariaDB [tom]> select * from mybook;
+--------------+-------+-------+
| name         | price | pages |
+--------------+-------+-------+
| gelintonghua |    60 |   518 |
+--------------+-------+-------+
1 row in set (0.00 sec)

更改图书的价格

MariaDB [tom]> UPDATE mybook SET price=55 ;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [tom]> SELECT name,price FROM mybook;
+--------------+-------+
| name         | price |
+--------------+-------+
| gelintonghua |    55 |
+--------------+-------+
1 row in set (0.00 sec)

MariaDB [tom]>
删除命令:delete
MariaDB [tom]> DELETE FROM mybook;
Query OK, 1 row affected (0.00 sec)

MariaDB [tom]> SELECT * FROM mybook;
Empty set (0.00 sec)
查询大于某一个数值的信息

首先我们插入四条数据

MariaDB [tom]> INSERT INTO mybook(name,price,pages) VALUES('123','30','518');   Query OK, 1 row affected (0.00 sec)

MariaDB [tom]> INSERT INTO mybook(name,price,pages) VALUES('456','50','518');
Query OK, 1 row affected (0.00 sec)

MariaDB [tom]> INSERT INTO mybook(name,price,pages) VALUES('789','80','518');
Query OK, 1 row affected (0.00 sec)

MariaDB [tom]> INSERT INTO mybook(name,price,pages) VALUES('101','100','518');
Query OK, 1 row affected (0.00 sec)
命令where
参数作用
=相等
<>或!=不相等
>大于
<小于
>=大于或等于
<=小于或等于
BETWEEN在某个范围内
LIKE搜索一个例子
IN在列中搜索多个值

在mybook表单中查找出价格大于75元或价格不等于80元的图书

MariaDB [tom]> SELECT * FROM mybook WHERE price>75;
+------+-------+-------+
| name | price | pages |
+------+-------+-------+
| 789  |    80 |   518 |
| 101  |   100 |   518 |
+------+-------+-------+
2 rows in set (0.00 sec)

MariaDB [tom]>  SELECT * FROM mybook WHERE price!=80;
+------+-------+-------+
| name | price | pages |
+------+-------+-------+
| 123  |    30 |   518 |
| 456  |    50 |   518 |
| 101  |   100 |   518 |
+------+-------+-------+
3 rows in set (0.00 sec)

数据库的备份及恢复

命令mysqldump

​ mysqldump命令用于备份数据库数据,格式为“mysqldump [参数] [数据库名称]”。其中参数与mysql命令大致相同,-u参数用于定义登录数据库的账户名称,-p参数代表密码提示符。

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

MariaDB [(none)]> exit
Bye
[root@server222 ~]# mysqldump -u root -p mysql > /root/mysql.dump
Enter password:
[root@server222 ~]# ls
anaconda-ks.cfg       mysql.dump  模板  图片  下载  桌面
initial-setup-ks.cfg  公共        视频  文档  音乐

进入MariaDB数据库管理系统,删除mysql数据库

[root@server222 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 45
Server version: 5.5.56-MariaDB MariaDB Server

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

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

MariaDB [(none)]> DROP DATABASE mysql;
Query OK, 24 rows affected, 2 warnings (0.00 sec)

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

MariaDB [(none)]> CREATE DATABASE mysql;
Query OK, 1 row affected (0.00 sec)

恢复数据库mysql

[root@server222 ~]# mysql -u root -p mysql > /root/mysql.dump
Enter password:
[root@server222 ~]#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值