Centos7 使用yum安装MariaDB与MariaDB的简单配置与使用

一.mariadb的安装

MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。

开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。

MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

方法1:阿里源下yum安装mariadb版本会旧一点

Red Hat Enterprise Linux/CentOS 7.0 发行版已将默认的数据库从 MySQL 切换到MariaDB。

安装命令

# yum -y install mariadb mariadb-server

安装完成MariaDB,首先启动MariaDB,两条命令都可以

systemctl start mariadb

#centos6命令

service mariadb start

方法2:yum安装mariadbrepo仓库配置安装

# 如果已经添加了阿里云的源又想安装最新版本的mariadb,那么少就使用以下步骤

# 编辑创建mariadb.repo仓库文件

vi /etc/yum.repos.d/MariaDB.repo

添加repo仓库配置

[mariadb]

name=MariaDB

baseurl=http://yum.mariadb.org/10.1/centos7-amd64

gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB

gpgcheck=1

当 MariaDB 仓库地址添加好后,你可以通过下面的一行命令轻松安装 MariaDB。

yum install MariaDB-server MariaDB-client -y

二.MariaDB基本配置

1.开启关闭与查看mariadb命令

mariadb数据库的相关命令是:

systemctl start mariadb  #启动MariaDB
systemctl stop mariadb  #停止MariaDB
systemctl restart mariadb  #重启MariaDB
systemctl enable mariadb  #设置开机启动

# 查看mariadb进程

[[email protected] home]# netstat -ntlp |grep 3306

tcp      0     0 0.0.0.0:3306      0.0.0.0:*         LISTEN      3931/mysqld

2.初始化MariaDB 数据库

在确认 MariaDB 数据库软件程序安装完毕并成功启动后请不要立即使用。为了确保数据 库的安全性和正常运转,需要先对数据库程序进行初始化操作。这个初始化操作涉及下面 5 个 步骤。

设置 root 管理员在数据库中的密码值(注意,该密码并非 root 管理员在系统中的密 码,这里的密码值默认应该为空,可直接按回车键)。

设置 root 管理员在数据库中的专有密码。

随后删除匿名账户,并使用 root 管理员从远程登录数据库,以确保数据库上运行的业

务的安全性。

删除默认的测试数据库,取消测试数据库的一系列访问权限。

刷新授权列表,让初始化的设定立即生效。

初始化命令:

mysql_secure_installation

首先是设置密码,会提示先输入密码

Enter current password for root (enter for none):<–初次运行直接回车

设置密码

Set root password? [Y/n]  y<– 是否设置root用户密码,输入y并回车或直接回车

New password: <– 设置root用户的密码

Re-enter new password: <– 再输入一次你设置的密码

其他配置

Remove anonymous users? [Y/n]  y<– 是否删除匿名用户,回车

Disallow root login remotely? [Y/n] n<–是否禁止root远程登录,回车,不过一般为y

Remove test database and access to it? [Y/n] y<– 是否删除test数据库,回车

Reload privilege tables now? [Y/n] y<– 是否重新加载权限表,回车

初始化MariaDB完成,接下来测试登录

# mysql -uroot -p    进入数据库(数据库中的操作命令和mysql是一样的)

3.中文编码设置,utf8编码

文件/etc/my.cnf

vi /etc/my.cnf

在[mysqld]标签下添加

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

进行查看就支持utf8了

三.mariadb的使用

关于mariadb的使用其实是和mysql的语句完全是一样的

1.开启关闭和设置开机启动

mariadb数据库的相关命令是:

systemctl start mariadb  #启动MariaDB

systemctl stop mariadb  #停止MariaDB

systemctl restart mariadb  #重启MariaDB

systemctl enable mariadb  #设置开机启动

2.设置密码建库建表

# 修改mysql密码

MariaDB [(none)]>  set password = PASSWORD(‘hsz123‘);

Query OK, 0 rows affected (0.34 sec)

# 创建tests数据库 如果加charset=utf8  表示指定utf8编码

MariaDB [(none)]> create database test;

Query OK, 1 row affected (0.00 sec)

# 进入test数据库

MariaDB [(none)]> use test;

Database changed

# 创建mytest数据表

MariaDB [test]> create table mytest(id int,name char(32));

Query OK, 0 rows affected (0.02 sec)

# 查看数据表

MariaDB [test]> show tables;

+----------------+

| Tables_in_test |

+----------------+

| mytest         |

+----------------+

1 row in set (0.00 sec)

# 查看mytest数据表的表结构

MariaDB [test]> desc mytest;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| id    | int(11)  | YES  |     | NULL    |       |

| name  | char(32) | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

2 rows in set (0.05 sec) 

2.简单的增删改查

# 给表增加两条数据

MariaDB [test]> insert into mytest(id,name) values(1,"zero"),(2,"one");

Query OK, 2 rows affected (0.35 sec)

Records: 2  Duplicates: 0  Warnings: 0

# 查看id,name 字段mytest的数据

MariaDB [test]> select id,name from mytest;

+------+------+

| id   | name |

+------+------+

|    1 | zero |

|    2 | one  |

+------+------+

2 rows in set (0.00 sec)

# 删除mytest表中id=3 的数据

MariaDB [test]> delete from mytest id=2;

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 ‘id=2‘ at line 1

MariaDB [test]> delete from mytest where id=2;

Query OK, 1 row affected (0.00 sec)

# 查看表的所有数据

MariaDB [test]> select * from mytest;

+------+------+

| id   | name |

+------+------+

|    1 | zero |

+------+------+

1 row in set (0.01 sec)

# 更新表id=1表的字段name=ten

MariaDB [test]> update mytest set name=ten where id=1;

ERROR 1054 (42S22): Unknown column ‘ten‘ in ‘field list‘

MariaDB [test]> update mytest set name="ten" where id=1;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [test]> select * from mytest;

+------+------+

| id   | name |

+------+------+

|    1 | ten  |

+------+------+

1 row in set (0.01 sec)

3.关于用户及权限常用命令

# 创建用户和密码

MariaDB [test]> create user [email protected]‘%‘ identified by ‘zero‘;

Query OK, 0 rows affected (0.01 sec)

mysql使用grant命令对账户进行授权,grant命令常见格式如下

grant 权限 on 数据库.表名 to 账户@主机名            对特定数据库中的特定表授权

grant 权限 on 数据库.* to 账户@主机名              对特定数据库中的所有表给与授权

grant 权限1,权限2,权限3 on *.* to 账户@主机名      对所有库中的所有表给与多个授权

grant all privileges on *.* to 账户@主机名      对所有库和所有表授权所有权限

#授予用户最大的权限,所有的权限

grant all privileges on *.* to [email protected]‘%‘ identified by ‘password‘;
#授予zero用户,只有创建test数据库的权限

MariaDB [test]> grant create on test.* to [email protected]‘%‘ identified by ‘zero‘;

Query OK, 0 rows affected (0.00 sec)

# 所以查询zero用户的数据库只有如下所示

[[email protected] ~]# mysql -uzero -pzero

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 6

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 |

| test               |

+--------------------+

2 rows in set (0.00 sec)

MariaDB [(none)]>
#授予one创建的权限,对于所有的库表生效

MariaDB [test]> grant create  on *.* to one@"%"  identified by ‘one‘;

Query OK, 0 rows affected (0.00 sec)

# 所以查询数据库可以显示如下所示

[[email protected] ~]# mysql -uone -pone

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 7

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              |

| performance_schema |

| test               |

| text               |

+--------------------+

5 rows in set (0.00 sec)

MariaDB [(none)]>

# 删除one用户

MariaDB [test]> drop user one;

Query OK, 0 rows affected (0.00 sec)

# 刷新权限

MariaDB [test]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

4.数据库备份与恢复

# mysqldump命令用于备份数据库数据

##备份所有数据库命令

[[email protected] ~]# mysqldump -u root -p --all-databases > /tmp/db.dump

Enter password:

[[email protected] ~]# ll /tmp/db.dump

-rw-r--r--. 1 root root 515562 Sep 13 23:00 /tmp/db.dump

##备份单个数据库命令

[[email protected] ~]# mysqldump -u root -p text > /tmp/text.sql

Enter password:

[[email protected] ~]# ll /tmp/text.sql

-rw-r--r--. 1 root root 1261 Sep 13 23:01 /tmp/text.sql

## 将备份的数据库导入

[[email protected] ~]# mysql -uroot -p  text2< /tmp/text.sql

# 删除数据库

MariaDB [text2]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

| text               |

| text2              |

+--------------------+

6 rows in set (0.00 sec)

MariaDB [text2]> drop database text;

Query OK, 0 rows affected (0.00 sec)

MariaDB [text2]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

| text2              |

+--------------------+

5 rows in set (0.00 sec)

原文地址:https://www.cnblogs.com/hszstudypy/p/11518638.html

时间: 2024-08-15 10:45:48

Centos7 使用yum安装MariaDB与MariaDB的简单配置与使用的相关文章

阿里云Centos7使用yum安装MySQL5.6.24的正确姿势

阿里云Centos7使用yum安装MySQL5.6.24 阿里云Centos7使用yum安装MySQL5.6.24 前言:由于某些不可抗力,我要在自己的阿里云服务器上搭建hadoop+hive+mysql+tomcat环境,下为mysql的安装记录 →_→大家都知道,centos自带的repo是不会自动更新每个软件的最新版本,所以无法通过yum方式安装MySQL的高级版本.所以,即使我使劲用yum -y install mysql mysql-server mysql-devel,也是没有人会鸟

centos7通过yum安装nginx

centos7通过yum安装nginx nginx不支持centos7通过yum直接安装~~~ 1.查看操作系统位数[[email protected] ~]# rpm -aq|grep centos-releasecentos-release-7-4.1708.el7.centos.x86_64/ 2.创建nginx的yum源[[email protected] ~]# cat  /etc/yum.repos.d/nginx.repo[nginx]name=nginx repobaseurl=

centos7.0 yum 安装php服务器

https://blog.csdn.net/jiaoshenmo/article/details/50923900 首先收一下:centos7.0用yum直接安装apache.php他们的默认版本是apache2.4和php5.4 1.安装之前先检查一下系统是否有默认安装的apache或者php rpm -qa | grep httpd rpm -qa | frep php 2.把上面指令列出来的包删除 rpm -e * * * *(包名) 3.在安装前 ,更新一下系统 yum update 4

Centos7下yum安装mongodb

https://www.cnblogs.com/flying1819/articles/9035408.html Centos7下yum安装mongodb 简介 MongoDB 是一个基于分布式 文件存储的NoSQL数据库 由C++语言编写,运行稳定,性能高 旨在为 WEB 应用提供可扩展的高性能数据存储解决方案 查看官方网站 MongoDB特点 模式自由 :可以把不同结构的文档存储在同一个数据库里 面向集合的存储:适合存储 JSON风格文件的形式 完整的索引支持:对任何属性可索引 复制和高可用

Centos7.4 yum 安装MariaDB

#系统及版本选择:https://downloads.mariadb.org/mariadb/repositories/#mirror=tunavim /etc/yum.repos.d/MariaDB.repo[mariadb]name = MariaDBbaseurl = http://yum.mariadb.org/10.2.4/centos7-amd64gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDBgpgcheck=1 yum -y i

CentOS7下yum安装mysql5.6

由于CentOS7默认的数据库yum源切换成了MariaDB,需要手动设置yum源才可安装mysql. 1.安装yum源 # rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm 2.检测可用的yum源 # yum repolist enabled | grep "mysql.*-community.*" mysql-connectors-community/x86_64 MySQL Con

CentOS7使用yum安装lamp

环境介绍 虚拟机 : VMware Workstation 14 Pro 镜像 : CentOS Linux release 7.4.1708 (Core) 物理机 : windows 7 64位 第一步:安装apache yum install -y httpd 第二步:安装PHP及相关扩展 yum install -y php php-mysql php-mbstring 目前最新的centos7默认安装的PHP版本是5.4.16 第三步:安装mysql 在目前的centos中,mysql已

CentOS7用yum安装 MySQL

首先CentOS7 已经不支持mysql,因为收费了你懂得,所以内部集成了mariadb,而安装mysql的话会和mariadb的文件冲突,所以需要先卸载掉mariadb,以下为卸载mariadb,安装mysql的步骤. #列出所有被安装的rpm package rpm -qa | grep mariadb #卸载rpm -e mariadb-libs-5.5.37-1.el7_0.x86_64错误:依赖检测失败:libmysqlclient.so.18()(64bit) 被 (已安裝) pos

CentOS7下安装Mysql失败经历--CentOS7使用yum安装和卸载Mysql过程

起因 自己租用的BandwagonVPS上安装了个CentOS7,然后开始安装各种软件,结果yum安装MySQL发现MySQL在yum源中的Mysql不对劲,于是自己百度搜索安装方法. 终于我搜到了这篇文章:http://www.mamicode.com/info-detail-503994.html,于是我就兴高采烈地安装了起来. 安装过程 我不建议这么安装,因为我经历的是一个失败的过程,最后我也卸载掉了. 下载mysql的repo源 注意这是社区版的mysql 我在/usr/local下面新