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,则不进行校验.

我的示例:

[[email protected] ~]# 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,不重要的密码很多时候可以设置为和用户名一致,以免忘记了又想不起来。

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

我的示例:

[[email protected] ~]# 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  ]

[email protected] ~]# 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

[[email protected] ~]# 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系统变量来达成目的.

# 停止数据库

[[email protected] ~]# service mysql stop

Shutting down MySQL...                                     [  OK  ]

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

[[email protected] ~] # mkdir -p /data/mysql

#设置权限

[[email protected] ~]# chown -R mysql:mysql /data/

[[email protected] ~]# ll /data/

total 4

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

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

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

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

[[email protected] ~]# 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 保存并退出

我的示例:

[[email protected] ~]# 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

[[email protected] ~]# 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

[[email protected] ~]# 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(‘[email protected]‘) 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 [email protected]‘%‘ identified by ‘pancou‘;

时间: 2024-08-11 01:35:13

CentOS安装并设置MariaDB的相关文章

CentOS Linux 中文输入法安装及设置

安装: 1.需要root权限,所以要用root登录 ,或su root 2.yum install "@Chinese Support" 3.exit 4.回到桌面,system->preferences->input method 5.如果没有,先注销一下. 6.按照提示添加输入法. 7.最后 再次注销,登录即可. 设置: 在linux CentOS中安装完中文输入法之后,还不能使用,必须进行相应的设置,就如同在windows中设置中文输入法一样,必须把刚刚安装的输入法添

Centos 安装FTP配置目录权限,iptables设置ftp服务

Centos 安装FTP配置目录权限,iptables设置ftp服务 2012-07-06 admin Leave a comment Go to comments CentOS 安装vsftpd,设置Iptables 限制用户访问自己目录 安装好vsftpd后,打开配置文件: [root@hexuweb101 ~]$vi /etc/vsftpd/vsftpd.conf 1 [root@hexuweb101 ~]$vi /etc/vsftpd/vsftpd.conf 把下面几行注释去掉,让其配置

虚拟机安装CentOS以及SecureCRT设置【完美无错版】

一.CentOS简介 CentOS是Linux的发行版之一,它安全.稳定.高效,是我最喜欢的Linux发行版之一.CentOS根据Red Hat Enterprise Linux开放源代码编译而成,与RedHat Linux并没有什么本质上的差别.但Red Hat Enterprise Linux是商业软件,使用必须向RedHat公司付费,而CentOS并没有任何使用上的限制.如果你需要企业级操作系统的稳定性,又不想付费去获得服务支持,CentOS绝对会是你最好的选择. 二.CentOS下载 目

Centos7 编译安装 Nginx、MariaDB、PHP

前言 本文主要大致介绍CentOS 7下编译安装Nginx.MariaDB.PHP.面向有Linux基础且爱好钻研的朋友.技艺不精,疏漏再所难免,还望指正. 环境简介: 系统: CentOS 7,最小化安装 IP: 192.168.170.128 Nginx: 1.6.1 MariaDB: 5.5.39 PHP: 5.5.16 1.准备工作 1.1.系统硬件准备 尽管Linux能最大化发挥硬件资源,但RHEL/CentOS随着版本增加对最低硬件的配置也越来越高[1].RHEL7/CentOS最低

Zabbix 3.4.10 服务端的安装与设置

实验验目的:  Zabbix 3.x 服务端的安装 实验主机:  m01  (centos 7.4)  IP 10.0.0.61/172.16.1.61 1) 配置yum源, 并用wget命令把相关的软件包下载到本地, 然后再进行安装, 如下所示 #配置yum 源解决依赖的问题 [[email protected] /]# rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-

CentOS 安装 PHP

分享一下我老师大神的人工智能教程吧.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net 1.获取PHP安装文件: downloads  或直接下载 php-5.5.9.tar.gz 获取安装php需要的支持文件: libxml2  或直接下载 libxml2-2.9.1.tar.gz 2.安装libxml2 tar zxvf libxml2-2.9.1.tar.gz cd libxml2-2.6.32./configu

阿里云 CentOS7 安装MySQL (MariaDB)

自从 CentOS 7 开始,自带的数据库就变成 MariaDB 了,yum 安装之后的默认版本是 5.5 添加Maria源 添加并编辑文件 添加MariaDB源 vi /etc/yum.repos.d/MariaDB.repo 粘贴官方的或者阿里云的镜像: [mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.3/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-

CentOS 安装redis 2.8.7

波折了好几下才装上 1.下载 wget http://download.redis.io/releases/redis-2.8.7.tar.gz 下载后的文件在当前目录里 redis-2.8.7.tar.gz 2.编译安装 tar xf redis-2.8.7.tar.gz cd redis-2.8.7 make make install 如果没有安装gcc的话会提示gcc not found 于是就需要安装一下gcc: yum -y install gcc 因为刚开始把yum的源换成163的了

Centos 安装 禅道

Centos 安装  禅道 一.环境准备: 1.服务器:Centos6.7 新系统 2.查看对应的系统版本:uname -a和cat /etc/redhat CentOS release 6.7 (Final) 二.安装: 1.下载对应系统版本的zbox禅道一键安装包,解压至/opt目录下 从window  电脑 到下面的地址下载最新的禅道版本 https://sourceforge.net/projects/zentao/files/9.0.1/ZenTaoPMS.9.0.1.zbox_64.