centos7 yum以及通用二进制配置安装Mariadb数据库

10 篇文章 0 订阅

MariaDB

MariaDB 介绍

MariaDB数据库管理系统是 MySQL 的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

在MariaDB工作与在MySQL下工作几乎一模一样,她们有相同的命令、界面,以及在MySQL中的库与API,所以MariaDB可以说是为替换MySQL量身定做的,所以它们之间是相通用(兼容),换用后连数据库都不必转换!并可以获得MariaDB提供的许多更好的新特性。

与 MySQL 相比较, MariaDB 更强的地方在于:

Maria 存储引擎
PBXT 存储引擎
XtraDB 存储引擎
FederatedX 存储引擎

更快的复制查询处理
线程池
更少的警告和bug
运行速度更快
更多的 Extensions
更好的功能测试
慢查询日志的扩展统计
支持对 Unicode 的排序

yum配置安装Mariadb数据库

安装文献参考

安装官方参考

mariadb官网

mariadb国内仓库官网

CentOS 6 或早期的版本中提供的是 MySQL 的服务器/客户端安装包,但 CentOS 7 已使用了 MariaDB 替代了默认的 MySQL。MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

检测为纯净系统

安装前面需要检测是否之前服务器安装过数据库等服务,如果有则删除数据库相关服务软件包等。

全部删除MySQL/MariaDB

MySQL 已经不再包含在 CentOS 7 的源中,而改用了 MariaDB;

#搜索 MariaDB 现有的包

#rpm -qa | grep MariaDB

[root@CentOS-7 ~]# rpm -qa |grep mariadb
mariadb-libs-5.5.65-1.el7.x86_64
mariadb-5.5.65-1.el7.x86_64
#如果存在(删不了的话,就一个一个删)
#全部删除
#rpm -e --nodeps MariaDB*
#搜索 mysql 现有的包
#rpm -qa | grep mysql*

如果存在(删除你实际查询出来的mysql结果即可),使用

yum remove mysql mysql-server mysql-libs compat-mysql51 -y
#如果只有上面的结果就说明是个纯净的系统
[root@CentOS-7 ~]# rpm -qa mariadb*
mariadb-libs-5.5.65-1.el7.x86_64
mariadb-5.5.65-1.el7.x86_64

配置yum源和仓库

开始新的安装, 创建mysql.repo文件

[root@centos7K ~]#cd /etc/yum.repos.d/
[root@centos7K yum.repos.d]#ls
aliyun.repo  bak2019-11-18_21:30:49  

[root@centos7K yum.repos.d]#vi mysql.repo 

到官网找到对应数据仓库配置文件,这里如下图在这里插入图片描述
官网链接

进行对应的修改。因为这个是国外的镜像仓库,下载可能比较慢。所以我们可以找国内的仓库源路径。

清华大学国内仓库
在这里插入图片描述
修改仓库配置文件如下:

[root@centos7K yum.repos.d]#vim mysql.repo 
[mariadb]
name = MariaDB
baseurl =https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/10.2/centos7-amd64/
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=0
                                                                                                       

加载对应的仓库
在这里插入图片描述

之后运行安装命令安装MariaDB。这里注意mariadb的大小写

安装mariadb

[root@centos7K ~]#yum -y install MariaDB-server 

开启对应的mariadb数据库服务,查看默认端口号3306是否开启。
在这里插入图片描述
登录到数据库用 mysql -uroot 这里默认第一次登入没有密码

[root@centos7K ~]#mysql -uroot 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.2.29-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)]> 

安全加固

之后对于新安装的数据库进行简单的安全加固

[root@centos7K ~]#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!

字符集配置

配置MariaDB的字符集,如果在my.cnf.d目录下有报错写在主配置文件下面也可以。

[root@centos7K ~]#vim /etc/my.cnf.d/server.cnf 
[mysql]
init_connect='SET collation_connection = utf8_unicode_ci' 
init_connect='SET NAMES utf8' 
character-set-server=utf8 
collation-server=utf8_unicode_ci 
skip-character-set-client-handshake

在这里插入图片描述

重启数据库

[root@centos7K ~]#systemctl restart mariadb

第二次登入需要输入口令 这里口令为centos

[root@centos7K ~]#mysql -pcentos
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.29-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 variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database   | utf8_unicode_ci |
| collation_server     | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> 

至此,centos7 yum配置安装Mariadb数据库基本安装完成,下面的创建用户授权按照正常的SQL语句执行即可

通用二进制配置安装Mariadb数据库

先确认自己的数据库是否纯净

[root@centos7K ~]#rpm -qa mariadb*
mariadb-libs-5.5.60-1.el7_5.x86_64

[root@centos7K ~]#rpm -qa MariaDB*
[root@centos7K ~]#rpm -qa mysql*
[root@centos7K ~]#

如果查询不到任何相关信息则说明这个系统是个没有数据库的纯净系统

准备用户

groupadd -r -g 306 mysql 先创建组
useradd -r -g 306 -u 306 –d /data/mysql mysql 再创建用户

[root@centos7K ~]#useradd -r -s /sbin/nologin  -d /data/mysql mysql

准备数据目录,建议使用逻辑卷

mkdir /data/mysql
chown mysql:mysql /data/mysql

转化逻辑卷

把/data变成逻辑卷,方便存储数据。也可以新增加一块硬盘来做实验。这里选择前者
在这里插入图片描述
先取消挂载。这里对应的分区是sad5。

[root@centos7K ~]#umount /data/

之后需要改变成为逻辑卷的标识
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
最后需要同步一下即可

 [root@centos7K ~]#partprobe 
Warning: Unable to open /dev/sr0 read-write (Read-only file system). 
 /dev/sr0 has been opened read-only.

现在/data/就可以当作逻辑卷用的逻辑分区,现在可以直接创建逻辑卷

如果这路最小化安装的系统没有创建逻辑卷工具,先安装一下lvm2即可

[root@centos7K yum.repos.d]#yum install lvm2 -y

[root@centos7K yum.repos.d]#pvcreate /dev/sda5
WARNING: xfs signature detected on /dev/sda5 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/sda5.
  Physical volume "/dev/sda5" successfully created.
[root@centos7K yum.repos.d]#pvs
  PV         VG Fmt  Attr PSize  PFree 
  /dev/sda5     lvm2 ---  50.00g 50.00g
  
[root@centos7K yum.repos.d]#vgcreate vg0 /dev/sda5
  Volume group "vg0" successfully created
[root@centos7K yum.repos.d]#vgs
  VG  #PV #LV #SN Attr   VSize   VFree  
  vg0   1   0   0 wz--n- <50.00g <50.00g
  
[root@centos7K yum.repos.d]#lvcreate -n lv_mysql -l 100%free vg0
  Logical volume "lv_mysql" created.
[root@centos7K yum.repos.d]#lvs
  LV       VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_mysql vg0 -wi-a----- <50.00g                                                    

格式化系统并且创建文件系统

[root@centos7K yum.repos.d]#mkfs.xfs /dev/vg0/lv_mysql 
meta-data=/dev/vg0/lv_mysql      isize=512    agcount=4, agsize=3276544 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=13106176, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=6399, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

之后进行挂载。这里我们用对应的逻辑卷名称进行简单临时挂载。生产中需要用UUID最好。

[root@centos7K ~]#vi /etc/fstab 

在这里插入图片描述
也可以通过blkid查看对应的UUID进行相应的挂载

[root@centos7K ~]#blkid

在这里插入图片描述
重新挂载一下即可
在这里插入图片描述

二进制安装准备

准备二进制程序

tar xf mariadb-VERSION-linux-x86_64.tar.gz -C /usr/local
cd /usr/local
ln -sv mariadb-VERSION mysql
chown -R root:root /usr/local/mysql/

准备压缩的二进制程序文件
这里当然也可以按照规范把安装二进制包放在 /usr/local/src 目录下面
在这里插入图片描述
在这里插入图片描述
因为是官方以及编译好的,所以安装路径要按照官方编译的路径结果来对应执行。
存放的路径默认在:/usr/local/

[root@centos7K ~]#tar xvf mariadb-10.2.29-linux-systemd-x86_64.tar.gz -C /usr/local/

名字不一致,所以这里要么改一下对应的名字,或者创建一个软链接,软链接比较方便
在这里插入图片描述
里面的所属组和所有者不对,也需要进行对应的修改
在这里插入图片描述

修改PATH变量

PATH路径

echo ‘PATH=/user/local/mysql/bin:$PATH’ > /etc/profile.d/mysql.sh .
/etc/profile.d/mysql.sh

[root@centos7K local]#echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@centos7K local]#. /etc/profile.d/mysql.sh 
[root@centos7K local]#echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

配置文件准备

准备准备配置文件

cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
chkconfig --add mysqld service mysqld start

#如果有对应的service 文件可以执行下面
cp support-files/systemd/mariadb.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl enable --now mariadb

在这里插入图片描述

[root@centos7K mysql]#vim /etc/my.cnf

在这里插入图片描述

初始化数据库文件

创建数据库文件

cd /usr/local/mysql/
./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
注意,这里需要的是相对路径,如果发生了路径错误的情况可以写绝对路径

[root@centos7K ~]#/usr/local/mysql/scripts/mysql_install_db --datadir=/data/mysql --user=mysql
[root@centos7K ~]#ll /data/
total 0
drwx------ 5 mysql root 251 Nov 19 11:02 mysql
权限自动设好700   所有者自动设成了mysql   所以这里可以跳过第二步中创建文件夹以及修改所有者所属组
mkdir /data/mysql                 chown mysql:mysql /data/mysql

启动服务脚本

准备服务脚本,并启动服务
方法一:新版systemd
在这里插入图片描述

 [root@centos7K mysql]#cp /usr/local/mysql/support-files/systemd/mariadb.service /usr/lib/systemd/system

在这里插入图片描述

在这里插入图片描述
启动服务
方法二:旧版本mysql.server

在这里插入图片描述

[root@centos7K mysql]#cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

复制到/etc/init.d/,并且取个名字叫mysqld
为了测试,把刚刚启动好的数据库停了,查看端口3306端口以及关闭。表示服务已经停止
在这里插入图片描述
在这里插入图片描述
服务已经打开,直接启动
在这里插入图片描述
centos6上面用第二种方法。

Mariadb安全加固

安全加固

[root@centos7K ~]#mysql_secure_installation 

由于之前以及加了PATH变量。所以这里直接可以运行对应的程序即可

字符集配置

配置MariaDB的字符集

[root@centos7K ~]#vim /etc/my.cnf.d/server.cnf 
[mysql]
init_connect='SET collation_connection = utf8_unicode_ci' 
init_connect='SET NAMES utf8' 
character-set-server=utf8 
collation-server=utf8_unicode_ci 
skip-character-set-client-handshake

然后重启数据库服务既可完成二进制安装的Mariadb。

Mariadb数据库常用操作命令

基础命令

不进入数据库直接执行命令,但是存在数据库密码明文的问题,需要慎重

mysql -u root -p -e "show databases;"
#-u(指定用户),-p(密码参数),-e(执行命令)
后面再加-e 可以执行多次数据库命令操作

常用数据库操作命令:

USE [database name] - 设置当前默认数据库,也可以用作数据库之间切换。
MariaDB [(none)]> use wordpress;
Database changed
MariaDB [wordpress]> 

SHOW DATABASES; - 列出服务器上当前的数据库。
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)

SHOW TABLES; - 列出所有非临时表,需要指定数据库。
MariaDB [(none)]> show tables;
ERROR 1046 (3D000): No database selected
MariaDB [(none)]> use wordpress;
Database changed
MariaDB [wordpress]> show tables;
+-----------------------+
| Tables_in_wordpress   |
+-----------------------+
| wp_commentmeta        |
| wp_comments           |
| wp_links              |
| wp_options            |
| wp_postmeta           |
| wp_posts              |
| wp_term_relationships |
| wp_term_taxonomy      |
| wp_termmeta           |
| wp_terms              |
| wp_usermeta           |
| wp_users              |
+-----------------------+
12 rows in set (0.03 sec)


SHOW COLUMNS FROM [table name] - 提供与指定表有关的列信息。
MariaDB [wordpress]> SHOW COLUMNS FROM wp_users;
+---------------------+---------------------+------+-----+---------------------+----------------+
| Field               | Type                | Null | Key | Default             | Extra          |
+---------------------+---------------------+------+-----+---------------------+----------------+
| ID                  | bigint(20) unsigned | NO   | PRI | NULL                | auto_increment |
| user_login          | varchar(60)         | NO   | MUL |                     |                |
| user_pass           | varchar(255)        | NO   |     |                     |                |
| user_nicename       | varchar(50)         | NO   | MUL |                     |                |
| user_email          | varchar(100)        | NO   | MUL |                     |                |
| user_url            | varchar(100)        | NO   |     |                     |                |
| user_registered     | datetime            | NO   |     | 0000-00-00 00:00:00 |                |
| user_activation_key | varchar(255)        | NO   |     |                     |                |
| user_status         | int(11)             | NO   |     | 0                   |                |
| display_name        | varchar(250)        | NO   |     |                     |                |
+---------------------+---------------------+------+-----+---------------------+----------------+
10 rows in set (0.00 sec)


SHOW INDEX FROM TABLENAME [table name] - 提供与指定表相关的表索引信息。
MariaDB [wordpress]> SHOW INDEX FROM wp_users;
+----------+------------+----------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table    | Non_unique | Key_name       | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+----------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| wp_users |          0 | PRIMARY        |            1 | ID            | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| wp_users |          1 | user_login_key |            1 | user_login    | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| wp_users |          1 | user_nicename  |            1 | user_nicename | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| wp_users |          1 | user_email     |            1 | user_email    | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+----------+------------+----------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec)


SHOW TABLE STATUS LIKE [table name] \ G - - 提供有关非临时表的信息的表,以及LIKE子句用于获取表名后显示的模式。 

选择查询
SELECT field, field2,... FROM table_name, table_name2,... WHERE...;

删除查询
DELETE FROM table_name [WHERE …]

创建数据表
CREATE TABLE table_name (column_name column_type);

删除数据表
DROP TABLE table_name ;

创建数据库
CREATE DATABASE database_name;
删除数据库
DROP DATABASE database_name;

插入查询
INSERT INTO tablename (field,field2,...) VALUES (value, value2,...);

更新查询
UPDATE table_name SET field=new_value, field2=new_value2,...[WHERE ...];

为数据表添加字段
alter table haloo add birth varchar(20);

指定添加的字段位置
alter table haloo add hometown varchar(20) after name;

mariadb数据库密码修改

mysqladmin -u root -pxxxxxx  password yyyyyy
#password 选项后的yyyyyy为新密码

忘记mariadb数据库密码

[root@centos-7 ~]# vim /etc/my.cnf[mysql]模块添加一行:skip-grant-tables
#设置进入数据库进行操作时跳过权限或者密码认证

[root@centos-7 ~]# systemctl restart mysql

[root@centos-7 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.2.31-MariaDB-log 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)]> use mysql; #选择对应的数据库
Database changed
#把密码修改为ubantu,
MariaDB [mysql]> update user set Password=password('ubantu') where User='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> exit
Bye
[root@centos-7 ~]# systemctl restart mysql

[root@centos-7 ~]# mysql -uroot -pubantu
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.2.31-MariaDB-log 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)]> 

简单数据库备份

备份

数据库备份(以名称为wordpress的数据库为例)

mysqldump -uroot -pcentos wordpress > wordpress.dump
#备份某个数据库

mysqldump -uroot -pcentos --all-databases > backup.dump
#备份所有数据库的数据

#恢复时不需要指定任何数据库,命令格式如下:
mysql -uroot -pubantu </mnt/all.dump 

# mysqldump -uroot -pcentos --no-data wordpress > wordpress.dump
#只备份数据库中的表结构,不备份数据

数据库备份与恢复实际操作

1.备份数据库
mysqldump -u root -pcentos wordpress >/mnt/wordpress.sql
#cat /mnt/wordpress.sql查看备份的数据库文件,发现里面全是sql语句 

2.删除数据库
mysql -uroot -pcentos -e 'drop database wordpress;'
或者使用命令mysqladmin -uroot -pcentos drop wordpress

3.创建一个数据恢复的数据库
mysql -uroot -pcentos -e "create database wordpress;"
或者使用命令mysqladmin -uroot -pcentos create wordpress

4.将数据恢复到数据库中
mysql -uroot -pcentos wordpress </mnt/wordpress.sql 

数据库用户管理以及用户授权

创建用户
CREATE USER wordpress@localhost identified by 'ubantu'; #默认对数据库的操作访问权限为最低

用户授权
GRANT INSERT,UPDATE,DELETE,SELECT on wordpress.* to wordpress@localhost;
GRANT SELECT on wordpress.* wordpress@localhost;

重载授权表
FLUSH PRIVILEGES;

查看用户授权
SHOW GRANTS FOR wordpress@localhost;

撤销用户权限
REVOKE DELETE,UPDATE,INSERT on wordpress.* from wordpress@localhost;

删除用户
DROP USER wordpress@localhost;

注意:如果授权了UPDATE权限而没有授权INSERT和SELECT会导致UPDATE命令失败的情况出现,因为:
UPDATE需要执行插入,而INSERT权限不够。如果update命令后有where 条件语句那么也会失败,因为SELECT权限不够

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值