MariaDb简介,由于Oracle公司在2009年收购了myql的母公司Sun,因此mysql数据项目也随之纳入了Oracle公司,逐渐演变为保持着开源软件的身份,但是又申请了多项商业专利的软件系统。所以mysql项目创始者重新研发了一款为maridb的全新数据库管理系统,几乎完全兼容mysql,在使用上几乎一致。

1.1 安装mariadb服务

# yum安装
[root@localhost ~]# yum install mariadb mariadb-server -y
# 设置开机自启动
[root@localhost ~]# systemctl enable mariadb
ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'
[root@localhost ~]# systemctl start mariadb

1.2 设置防火墙

[root@localhost Desktop]# firewall-cmd --permanent --add-service=mysql
success
[root@localhost Desktop]# firewall-cmd --reload
success

1.3 初始化mariadb

# 初始化数据库
[root@localhost Desktop]# mysql_secure_installation 
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

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 【设置数据库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] 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【加载配置】
 ... 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!

1.4 数据库的基本操作命令

# 登录数据库
[root@localhost Desktop]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.35-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

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

MariaDB [(none)]> 

# 重置root用户密码为‘redhat’
MariaDB [(none)]> set password=password('redhat');
Query OK, 0 rows affected (0.00 sec)

# 创建用户developer,并授权developer对mysql.user表的select,insert,update,delete权限
MariaDB [(none)]> create user developer@localhost identified by 'redhat';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> grant select,insert,update,delete on mysql.user to developer@localhost;
Query OK, 0 rows affected (0.00 sec)

# 移除developer用户对mysql.user表的select,insert,update,delete权限
MariaDB [(none)]> revoke select,insert,update,delete on mysql.user from developer@localhost;
Query OK, 0 rows affected (0.00 sec)

# 创建一个名为db的数据库
MariaDB [(none)]> create database db;
Query OK, 1 row affected (0.00 sec)
# 进入db数据库
MariaDB [(none)]> use db;
Database changed

# 创建一个表单
MariaDB [db]> create table user(name char(15),id int,age int);
Query OK, 0 rows affected (0.01 sec)

# 查看表结构
MariaDB [db]> desc user;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name  | char(15) | YES  |     | NULL    |       |
| id    | int(11)  | YES  |     | NULL    |       |
| age   | int(11)  | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.01 sec)

# 列出所有表单
MariaDB [db]> show tables;
+--------------+
| Tables_in_db |
+--------------+
| user         |
+--------------+
1 row in set (0.00 sec)

# insert 命令,插入数据
MariaDB [db]> insert into user(name,id,age) values('xiaosong',1,18);
Query OK, 1 row affected (0.01 sec)
MariaDB [db]> insert into user(name,id,age) values('zhangwu',2,24);
Query OK, 1 row affected (0.00 sec)
MariaDB [db]> insert into user(name,id,age) values('wangliu',3,31);
Query OK, 1 row affected (0.00 sec)

# 查询user表单的所有数据
MariaDB [db]> select * from user;
+----------+------+------+
| name     | id   | age  |
+----------+------+------+
| xiaosong |    1 |   18 |
| zhangwu  |    2 |   24 |
| wangliu  |    3 |   31 |
+----------+------+------+
3 rows in set (0.00 sec)

# update命令,更新小松的年龄为15
MariaDB [db]> update user set age=15 where name='xiaosong';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
MariaDB [db]> select * from user;
+----------+------+------+
| name     | id   | age  |
+----------+------+------+
| xiaosong |    1 |   15 |
| zhangwu  |    2 |   24 |
| wangliu  |    3 |   31 |
+----------+------+------+
3 rows in set (0.00 sec)

# delete命令,删除wangliu用户信息
MariaDB [db]> delete from user where name='wangliu';
Query OK, 1 row affected (0.00 sec)

MariaDB [db]> select * from user;
+----------+------+------+
| name     | id   | age  |
+----------+------+------+
| xiaosong |    1 |   15 |
| zhangwu  |    2 |   24 |
+----------+------+------+
2 rows in set (0.00 sec)

# 删除user表
MariaDB [db]> drop table user;
# 删除db库
MariaDB [db]> drop database db;

1.5 数据库的备份和恢复

备份db库到当前目录的db.dump文件

[root@localhost ~]# mysqldump -uroot -p db >db.dump
Enter password:  【root密码】

恢复

[root@localhost ~]# mysq -uroot -p db