嘘~ 正在从服务器偷取页面 . . .

CentOS7安装mariaDB及基本配置


在Oracle收购了Sun之后,很多公司开始担忧MySQL的开源前景,而最近Oracle进一步闭源的举措更是让人
难以安心,众多互联网公司纷纷开始寻求MySQL的替代方案。MySQL之父Widenius决定从新开发代码全部开
源免费关系型数据库,并以其女儿的名子命名,这就是MariaDB。MariaDB跟MySQL在绝大多数方面是兼容的
,对于使用者来说,几乎感觉不到任何不同。目前MariaDB是发展最快的MySQL分支版本,新版本发布速度已
经超过了Oracle官方的MySQL版本。相较于 MySQL, MariaDB 数据库管理系统有了很多新鲜的扩展特性,
例如对微秒级别的支持、线程池、子查询优化、进程报告等。在CentOS7下,默认安装的数据库已经是
MariaDB而不是mysql了,可使用系统自带的repos进行安装。

安装

1,安装mariaDB
在RHEL/CentOS7上默认没有安装MariaDB数据库管理系统(DBMS),所以需要先安装:
● mariaDB-server: mariaDB服务器安装包
● mariaDB:客户端shell接口,用于控制和查询服务器
yum install -y mariadb mariadb-server
================================================================================================
Package Arch Version Repository Size
Installing:
mariadb x86_64 1:5.5.60-1.el7_5 updates 8.9 M
mariadb-server x86_64 1:5.5.60-1.el7_5 updates 11 M
Installing for dependencies:
perl-DBD-MySQL x86_64 4.023-6.el7 base 140 k
Updating for dependencies:
mariadb-libs x86_64 1:5.5.60-1.el7_5 updates 758 k
2,启动mariadb服务并设置开机自动启动:
systemctl start mariadb
systemctl enable mariadb

初始化操作

为了确保数据库的安全性和正常运转,需要先对数据库程序进行初始化操作。我们需要调用安全安装脚本通
过几个简单的步骤来加强安全性。
初始化操作包括以下5个步骤:
● 设置root管理员在数据库中的密码,注意这个密码默认是空的,所以直接回车即可
● 设置root管理员在数据库中的专有密码。
● 删除匿名账户,禁止远程登录数据库
● 删除默认的测试数据库,取消测试数据库的一系列访问权限。
● 刷新授权列表,让初始化的设定立即生效。

1,初始化MariaDB 服务
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 #禁示远程访问,注意如果是站库分离,此项不应该禁止
… 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 #删除默认的测试数据库

  • 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!
2,配置防火墙
如果启用了防火墙,并且使用了站库分离架构,则需要配置防火墙使其放行对数据库服务程序的访问请求,
数据库服务程序默认占用3306端口,在防火墙策略中服务名称统一叫作mysql
firewall-cmd –permanent –add-service=mysql
firewall-cmd –reload
3,测试登录
测试是否可以使用MariaDB命令行mysql在本地连接并登录到MariaDB服务
mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.60-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)]>

数据库管理

1,查询数据库
使用show databases;命令查询数据库,注意后面要加上分号”;”,注意在MariaDB中执行数据库命令时,
都需要在命令后面用分号( ;)结尾。
MariaDB [(none)]> show databases;
+——————–+
Database
+——————–+
information_schema
mysql
performance_schema
+——————–+
3 rows in set (0.00 sec)
2,修改root账号的密码
比如,将root管理员在数据库管理系统中的密码修改为kclouder,修改后,可退出并重新登录数据库管理系
统验证可以使用新密码登录
MariaDB [(none)]> set password = PASSWORD(‘kclouder’);
Query OK, 0 rows affected (0.01 sec)
3,管理账户以及授权
为了保障数据库系统的安全性,通常我们需要在MariaDB数据库管理系统中创建专用的数据库管理账户,使用
以下语法:
CREATE USER 用户名@主机名 IDENTIFIED BY ‘密码’;
MariaDB [(none)]> create user dbadmin@localhost identified by ‘kclouder’;
Query OK, 0 rows affected (0.00 sec)
4,查询用户
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=”dbadmin”;
+———–+———+——————————————-+
HOST USER PASSWORD
+———–+———+——————————————-+
localhost dbadmin *56CCE17C9A5D3AF02EF7241708A56864F57B8711
+———–+———+——————————————-+
1 row in set (0.00 sec)
5,设置用户权限
上一步新建的用户”dbadmin”还只是普通用户,需要对其进行授权使其成为管理员,使其拥有查询、更新、
删除以及插入等权限
MariaDB [mysql]> grant select,update,delete,insert on mysql.user to dbadmin@localhost;
Query OK, 0 rows affected (0.00 sec)
查看权限:
MariaDB [mysql]> show grants for dbadmin@localhost;
+—————————————————————————————————————-+
Grants for dbadmin@localhost
+—————————————————————————————————————-+
GRANT USAGE ON . TO ‘dbadmin‘@’localhost’ IDENTIFIED BY PASSWORD ‘*56CCE17C9A5D3AF02EF7241708A56864F57B8711’
GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql` . ` user` TO ‘dbadmin‘@’localhost’
+—————————————————————————————————————-+
2 rows in set (0.00 sec)
6,使用新用户”dbadmin”登录,并查询数据库
mysql -u dbadmin -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.60-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)]> show databases;
+——————–+
Database
+——————–+
information_schema
mysql
+——————–+
2 rows in set (0.00 sec)

创建数据库与表单

在 MariaDB 数据库管理系统中,一个数据库可以存放多个数据表,数据表单是数据库中
最重要最核心的内容。根据自己的实际需求自定义数据库表结构,然后在其中合理地存
放数据,以便后期轻松地维护和修改。
相关命令:
CREATE DATABASE 数据库名称: #创建新的数据库
DESCRIBE 表单名称: #描述表单
UPDATE 表单名称 SET attribute=新值 WHERE attribute>原值: #更新表单中的数据
USE 数据库名称: #指定使用的数据库
SHOW databases: #显示当前已有的数据库
SHOW tables: #显示当前数据库中的表单
SELECT * FROM 表单名称: #从表单中选中某个记录值
DELETE FROM 表单名 WHERE attribute=值: #从表单中删除某个记录值
1,创建名为”kclouderdb”的数据库
MariaDB [(none)]> create database kclouderdb;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+——————–+
Database
+——————–+
information_schema
kclouderdb
mysql
performance_schema
+——————–+
4 rows in set (0.00 sec)
2,创建表单
这里我们假设创建一个名为”mybook”的数据库表单并定义三个字段项,其中,”name”字段为长度10个字符
的字符型字段用来存放图书名称,整型字段”price”和”pages”分别存储图书的价格和页数
MariaDB [(none)]> use kclouderdb
Database changed
MariaDB [kclouderdb]> create table mybook (name char(10),price int,pages int);
Query OK, 0 rows affected (0.04 sec)
MariaDB [kclouderdb]> describe mybook;
+——-+———-+——+—–+———+——-+
Field Type Null Key Default Extra
+——-+———-+——+—–+———+——-+
name char(10) YES NULL
price int(11) YES NULL
pages int(11) YES NULL
+——-+———-+——+—–+———+——-+
3 rows in set (0.00 sec)
3,插入表单
使用”insert”命令,并对需要赋值的项进行赋值,然后使用”select”查询
MariaDB [kclouderdb]> insert into mybook(name,price,pages) values(‘book1’,’100’,’510’);
Query OK, 1 row affected (0.03 sec)
MariaDB [kclouderdb]> select * from mybook;
+——-+——-+——-+
name price pages
+——-+——-+——-+
book1 100 510
+——-+——-+——-+
1 row in set (0.00 sec)
4,修改表单
修改price为200
MariaDB [kclouderdb]> update mybook set price=150;
Query OK, 1 rows affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [kclouderdb]> select * from mybook;
+——-+——-+——-+
name price pages
+——-+——-+——-+
book1 150 510
+——-+——-+——-+
1 rows in set (0.00 sec)
5,删除表单
MariaDB [kclouderdb]> DELETE FROM mybook;
Query OK, 2 rows affected (0.02 sec)
MariaDB [kclouderdb]> select * from mybook;
Empty set (0.00 sec)

备份与恢复

使用mysqldump命令备份数据库数据
“mysqldump [参数] [数据库名称]“
格式1,标准格式
mysqldump -u root -p kclouderdb > /backup/database/kclouderdb.sql
Enter password:
格式2:自动输入密码,这个格式适合自动化脚本,比如利用crontab计划任务进行备份
mysqldump -uroot -pkclouder kclouderdb > /backup/database/kclouderdb.sql2
格式3:备份文件自动添加时间日期信息
mysqldump -uroot -pkclouder –opt kclouderdb > /backup/database/kclouderdb.sql.`date +"%Y-%m-%d-%H-%M-%S"`
查看备份,可以看到三种格式生成的备份文件
[root@mariaDB database]# ll total 12 -rw-r–r–. 1 root root 1912 Sep 27 15:23 kclouderdb.sql -rw-r–r–. 1 root root 1912 Sep 27 15:26 kclouderdb.sql2 -rw-r–r–. 1 root root 1912 Sep 27 15:32 kclouderdb.sql.2018-09-27-15-32-23

其它

1,mysql命令不区分大小写,你可能会注意到在本文中所有命令都是小写的。为了区分命令与参数,建议将
命令都用大写字母,这样在做比较繁琐的配置时,可以直观的对命令进行区分或排错。
2,前面我们创建的用户’dbadmin’并赋予了一些权限,下面我们对该用户赋予对数据库最高权限,并确认该
用户可以对数据库”kclouderdb”进行查改增删这4项基本操作
[root@mariaDB database]# mysql -u root -p
MariaDB [(none)]> grant all on kclouderdb.* to dbadmin@localhost identified by ‘kclouder’ with grant option;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
[root@mariaDB database]# mysql -u dbadmin -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 22
Server version: 5.5.60-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)]> show databases;
+——————–+
Database
+——————–+
information_schema
kclouderdb
mysql
+——————–+
3 rows in set (0.00 sec)
MariaDB [(none)]> use kclouderdb
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 [kclouderdb]> select * from mybook;
+——-+——-+——-+
name price pages
+——-+——-+——-+
book1 100 510
+——-+——-+——-+
1 rows in set (0.00 sec)
MariaDB [kclouderdb]> update mybook set price=300;
Query OK, 1 rows affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [kclouderdb]> select * from mybook;
+——-+——-+——-+
name price pages
+——-+——-+——-+
book1 300 510
+——-+——-+——-+
1 rows in set (0.01 sec)


文章作者: kclouder
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 kclouder !
  目录