千家信息网

CentOS安装并设置MariaDB

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,说明: 首先必须能链接外网. 如果不能直接访问,那也可以设置代理,请参考: 在内网机器上设置yum代理使用 yum 的权限要求是 root 用户,如果你不是,那么可以需要 在 shell命令之前加上
千家信息网最后更新 2026年11月01日CentOS安装并设置MariaDB


说明: 首先必须能链接外网. 如果不能直接访问,那也可以设置代理,请参考: 在内网机器上设置yum代理


使用 yum 的权限要求是 root 用户,如果你不是,那么可以需要 在 shell命令之前加上 sudo, 或者 su root 切换到 super 管理员进行操作. 并可能需要输入密码.


1. 添加 yum 数据源;


建议命名为 MariaDB.repo 类似的名字:


[plain] view plain copy 在CODE上查看代码片派生到我的代码片

cd /etc/yum.repos.d/

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


然后,写入文件内容:(建议使用 10.0)


[plain] view plain copy 在CODE上查看代码片派生到我的代码片

# MariaDB 10.0 CentOS repository list - created 2015-08-12 10:59 UTC

# http://mariadb.org/mariadb/repositories/

[mariadb]

name = MariaDB

baseurl = http://yum.mariadb.org/10.0/centos6-amd64

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

gpgcheck=1


该文件的内容是参考官网,并从官网上生成的,设置安装源仓库的 具体的地址为: https://downloads.mariadb.org/mariadb/repositories/

选择好操作系统版本之后既可以查看,其他操作系统的安装源也可以在此处查看并设置。


如果服务器不支持https协议,或者gpgkey 保错,确保没问题的话,可以将 gpgcheck=1 修改为 gpgcheck=0,则不进行校验.


我的示例:


[root@localhost ~]# cat /etc/yum.repos.d/MariaDB.repo


# MariaDB 10.1 CentOS repository list - created 2017-04-05 08:04 UTC

# http://downloads.mariadb.org/mariadb/repositories/

[mariadb]

name = MariaDB

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

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

gpgcheck=1


2. 安装数据库


# yum remove MariaDB-server MariaDB-client


yum -y install MariaDB-server MariaDB-client


如果要删除旧的数据库可以使用remove, 参数 -y 是确认,不用提示。此处,安装的是服务器和客户端,一般来说安装这两个就可以了。


3. 启动数据库


如果不用进行其他的操作,则现在就可以直接启动数据库,并进行测试了。


# 查看mysql状态;关闭数据库

# service mysql status

# service mysql stop

# 启动数据库

service mysql start

4. 修改root密码

# 修改root密码

mysqladmin -u root password 'root'


因为安装好以后的root密码是空,所以需要设置; 如果是测试服务器,那么你可以直接使用root,不重要的密码很多时候可以设置为和用户名一致,以免忘记了又想不起来。

如果是重要的服务器,请使用复杂密码,例如邮箱,各种自由组合的规则的字符等。

我的示例:


[root@localhost ~]# service mysql start

Starting MySQL.170405 17:20:34 mysqld_safe Logging to '/var/lib/mysql/localhost.localdomain.err'.

170405 17:20:34 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

[ OK ]

root@localhost ~]# ps aux|grep mysq

root 8824 0.0 0.0 11436 1564 pts/0 S 17:20 0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/localhost.localdomain.pid

mysql 8898 1.1 1.6 824048 134948 pts/0 Sl 17:20 0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/localhost.localdomain.err --pid-file=/var/lib/mysql/localhost.localdomain.pid


5. 登录数据库


mysql -u root -p


如果是本机,那可以直接使用上面的命令登录,当然,需要输入密码. 如果是其他机器,那么可能需要如下的形式:

mysql -h 127.0.0.1 -P 3306 -u root -p

[root@localhost ~]# mysql

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

Your MariaDB connection id is 3

Server version: 10.1.22-MariaDB MariaDB Server


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


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


MariaDB [(none)]>

MariaDB [(none)]> show variables like 'innodb_file_per%';

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

| Variable_name | Value |

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

| innodb_file_per_table | ON |

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

1 row in set (0.00 sec)


6. 简单SQL测试


-- 查看MySQL的状态

status;

-- 显示支持的引擎

show engines;

-- 显示所有数据库

show databases;

-- 切换数据库上下文,即设置当前会话的默认数据库

use test;

-- 显示本数据库所有的表

show tables;

-- 创建一个表

CREATE TABLE t_test (

id int(11) UNSIGNED NOT NULL AUTO_INCREMENT,

userId char(36),

lastLoginTime timestamp,

PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 插入测试数据

insert into t_test(userId)

values

('admin')

,('haha')

;

-- 简单查询

select * from t_test;

select id,userId from t_test where userId='admin' ;


7. 修改数据存放目录


mysql, MariaDB 的默认数据存放在 /var/lib/mysql/ 目录下,如果不想放到此处,或者是想要程序和数据分离,或者是磁盘原因,

需要切换到其他路径,则可以通过修改 datadir系统变量来达成目的.


# 停止数据库


[root@localhost ~]# service mysql stop

Shutting down MySQL... [ OK ]

# 创建目录,假设没有的话


[root@localhost ~] # mkdir -p /data/mysql


#设置权限

[root@localhost ~]# chown -R mysql:mysql /data/


[root@localhost ~]# ll /data/

total 4

drwxr-xr-x 5 mysql mysql 4096 Apr 5 17:50 mysql


# 按下面的命令重新初始化数据库

[root@localhost ~]# /usr/bin/mysql_install_db --defaults-file=/etc/my.cnf.d/server.cnf --datadir=/data/mysql --user=mysql

# 查看/data/mysql下面的是否生成数据


[root@localhost ~]# ls /data/mysql/

aria_log.00000001 bogon.err ib_logfile0 localhost.localdomain.err performance_schema

aria_log_control ibdata1 ib_logfile1 mysql test

# 其实查看 /etc/my.cnf 文件可以发现

# MariaDB 的此文件之中只有一个包含语句

# 所以需要修改的配置文件为 /etc/my.cnf.d/server.cnf


cp /etc/my.cnf.d/server.cnf /etc/my.cnf.d/server.cnf_original

vim /etc/my.cnf.d/server.cnf


然后 按 i 进入编辑模式,可以插入相关内容.使用键盘的上下左右键可以移动光标, 编辑完成以后,按 ESC 退出编辑模式(进入命令模式), 然后输入命令:wq 保存并退出

我的示例:


[root@localhost ~]# cat /etc/my.cnf.d/server.cnf

#

# These groups are read by MariaDB server.

# Use it for options that only the server (but not clients) should see

#

# See the examples of server my.cnf files in /usr/share/mysql/

#


# this is read by the standalone daemon and embedded servers

[server]


# this is only for the mysqld standalone daemon

[mysqld]


datadir=/data/mysql #设置/data/mysql为新文件的数据目录

socket=/var/lib/mysql/mysql.sock

#

# * Galera-related settings

#

[galera]

# Mandatory settings

#wsrep_on=ON

#wsrep_provider=

#wsrep_cluster_address=

#binlog_format=row

#default_storage_engine=InnoDB

#innodb_autoinc_lock_mode=2

#

# Allow server to accept connections on all interfaces.

#

#bind-address=0.0.0.0

#

# Optional setting

#wsrep_slave_threads=1

#innodb_flush_log_at_trx_commit=0


# this is only for embedded server

[embedded]


# This group is only read by MariaDB servers, not by MySQL.

# If you use the same .cnf file for MySQL and MariaDB,

# you can put MariaDB-only options here

[mariadb]


# This group is only read by MariaDB-10.1 servers.

# If you use the same .cnf file for MariaDB of different versions,

# use this group for options that older servers don't understand

[mariadb-10.1]

#重启MySQL


[root@localhost ~]# service mysql start

Starting MySQL.170405 17:50:20 mysqld_safe Logging to '/data/mysql/localhost.localdomain.err'.

170405 17:50:20 mysqld_safe Starting mysqld daemon with databases from /data/mysql

[ OK ]


提示:


/usr/bin/mysqld_safe_helper: Can't create/write to file '/data/mysql/bogon.err' (Errcode: 13 "Permission denied")

ERROR!

害苦了我,多方查找才发selinx开启这呢,果断禁用,然后重启操作系统:OK!!!


vim /etc/sysconfig/selinux

SELINUX=disabled


[root@localhost ~]# mysql

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

Your MariaDB connection id is 3

Server version: 10.1.22-MariaDB MariaDB Server


Copyright (c) 2000, 2016, 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 |

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

4 rows in set (0.00 sec)


7.1 创建慢查询日志文件


既然上面指定了慢查询日志文件,我后来看了下MariaDB的err日志,发现MariaDB不会自己创建该文件,所以我们需要自己创建,并修改相应的文件权限(比如 MySQL 采用 mysql用户,可能我们使用 root用户创建的文件,此时要求慢查询日志文件对mysql用户可读可写就行。)



touch /usr/local/ieternal/mysql_data/slow_query_log.log

chmod 666 /usr/local/ieternal/mysql_data/slow_query_log.log

然后重新启动MySQL.



service mysql start


8、mysql初始设置

1、删除匿名用户

mysql> delete from mysql.user where user='';


2、设置root密码

1)、

mysqladmin -u root password "newpass"

2)、

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');

3)、

mysql> UPDATE mysql.user SET Password = PASSWORD('db@pass123') WHERE user = 'root';

mysql> FLUSH PRIVILEGES;


4)、在丢失root密码的时候,可以这样


   mysqld_safe --skip-grant-tables&

   mysql -u root mysql

mysql> UPDATE user SET password=PASSWORD("new password") WHERE user='root';

   mysql> FLUSH PRIVILEGES;


mysql> grant all on *.* to pancou@'%' identified by 'pancou';


数据 数据库 文件 密码 用户 命令 代码 日志 服务器 目录 系统 服务 查询 测试 操作系统 内容 权限 模式 示例 面的 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技会 互联网巨头科技公司 互联网 科技公司 金融科技与互联网金融 万易互联网科技 有木互联网科技 深圳期待互联网科技 亿人帮互联网科技 互联网和高科技公司 宁夏众和广鑫互联网科技有限公司 互联网校园科技培训 昊天互联网科技有限公司 广州市恰铎互联网科技有限公司 互联网科技人工智能实验室 中国互联网公司排名锤子科技 广东工业互联网大赛深圳学泰科技 广东鹅鹅鹅互联网科技有限公司 求路客互联网科技电话 科技互联网公司两年变化 河南商务圈互联网科技有限公司 美国互联网前沿科技 常德市电业局互联网科技部 中国互联网有什么科技 江苏互联网智能科技软件 张召忠谈中国互联网科技 卡尔迅互联网科技有限公司 中国新注册互联网科技 唐山双旺合互联网科技产业园 重庆科技产业互联网有哪些 颐高云石互联网科技
0