找回密码
 注册
查看: 3675|回复: 0

CentOS 7.0系统下安装配置mariadb步骤详解

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2017-5-23 09:03:45 | 显示全部楼层 |阅读模式

: f% A; c2 G: R' _, e, O
  S) E$ ?6 o7 o8 D7 N
[p=120, null, left]shell >

[p=120, null, left]hostname  mariadbServer  

[p=120, null, left]#临时设置主机名为

[p=120, null, left]mariadbServer  

[p=143, null, left]mariadbServer  

[p=143, null, left]#修改localhost.localdomain为

[p=143, null, left]mariadbServer  

[p=143, null, left]:wq!  #保存退出

[p=121, null, left]shell >

[p=121, null, left]vi /etc/hostname #编辑配置文件

[p=143, null, left]127.0.0.1   localhost  mariadbServer  

[p=143, null, left]#修改localhost.localdomain为

[p=143, null, left]mariadbServer  

[p=143, null, left]:wq!  #保存退出

[p=120, null, left]shell >

[p=120, null, left]vi /etc/hosts #编辑配置文件

[p=143, null, left]a.

[p=143, null, left]若安装时没有设置主机名

[p=143, null, left],

[p=143, null, left]则参考以下步骤设置主机名

[p=143, null, left]:

[p=121, null, left]shell >

[p=121, null, left]cd  /etc/sysconfig/network-scripts/

[p=143, null, left]HWADDR=00:0C:29:8D:24:73

[p=142, null, left]TYPE=Ethernet

[p=143, null, left]BOOTPROTO=static  #启用静态IP地址

[p=143, null, left]DEFROUTE=yes

[p=142, null, left]PEERDNS=yes

[p=143, null, left]PEERROUTES=yes

[p=143, null, left]IPV4_FAILURE_FATAL=no

[p=143, null, left]IPV6INIT=yes

[p=142, null, left]IPV6_AUTOCONF=yes

[p=143, null, left]IPV6_DEFROUTE=yes

[p=143, null, left]IPV6_PEERDNS=yes

[p=142, null, left]IPV6_PEERROUTES=yes

[p=143, null, left]IPV6_FAILURE_FATAL=no

[p=143, null, left]NAME=eno16777736

[p=143, null, left]UUID=ae0965e7-22b9-45aa-8ec9-3f0a20a85d11

[p=142, null, left]ONBOOT=yes  #开启自动启用网络连接

[p=143, null, left]IPADDR0=192.168.21.128  #设置IP地址

[p=143, null, left]PREFIXO0=24  #设置子网掩码

[p=142, null, left]GATEWAY0=192.168.21.2

[p=142, null, left] #设置网关

[p=143, null, left]DNS1=8.8.8.8  #设置主DNS

[p=143, null, left]DNS2=8.8.4.4  #设置备DNS

[p=143, null, left]:wq!  #保存退出

[p=121, null, left]shell >

[p=121, null, left]vi  ifcfg-eno16777736

[p=121, null, left]shell >

[p=121, null, left]service network restart   #重启网络

[p=120, null, left]--

[p=120, null, left]若习惯

[p=120, null, left]ifconfig  

[p=120, null, left]可以使用

[p=120, null, left]yum install net-tools

[p=120, null, left]进行安装

[p=121, null, left]shell >

[p=121, null, left]ip addr  #CentOS 7 ifconfig

[p=121, null, left]命令已经过时

[p=121, null, left]查看IP地址使用

[p=121, null, left]ip addr

[p=121, null, left]

[p=121, null, left]ip link

[p=142, null, left]b.

[p=142, null, left]网络配置

[p=143, null, left]CentOS 7.0默认使用的是firewall作为防火墙,这里改为常用的iptables防火墙。

[p=143, null, left]systemctl stop firewalld.service #停止firewall

[p=143, null, left]systemctl disable firewalld.service #禁止firewall开机启动

[p=142, null, left]     --

[p=142, null, left]关闭firewall

[p=142, null, left]:

[p=142, null, left]yum install iptables-

[p=142, null, left]services #安装

[p=143, null, left]# sample configuration for iptables service

[p=142, null, left]# you can edit this manually or use system-config-firewall

[p=143, null, left]# please do not ask us to add additional ports/services to this default configuration

[p=143, null, left]*filter

[p=143, null, left]:INPUT ACCEPT [0:0]

[p=142, null, left]:FORWARD ACCEPT [0:0]

[p=143, null, left]:OUTPUT ACCEPT [0:0]

[p=143, null, left]-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

[p=142, null, left]-A INPUT -p icmp -j ACCEPT

[p=143, null, left]-A INPUT -i lo -j ACCEPT

[p=143, null, left]-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT

[p=143, null, left]-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

[p=142, null, left]-A INPUT -p tcp -m state --state NEW -m tcp --dport 12888 -j ACCEPT

[p=143, null, left]-A INPUT -j REJECT --reject-with icmp-host-prohibited

[p=143, null, left]-A FORWARD -j REJECT --reject-with icmp-host-prohibited

[p=142, null, left]COMMIT

[p=143, null, left]vi /etc/sysconfig/iptables #编辑防火墙配置文件

[p=143, null, left]:wq! #保存退出

[p=143, null, left]     --

[p=143, null, left]安装iptables防火墙

[p=143, null, left]c.

[p=143, null, left]防火墙设置

[p=143, null, left]

5 }" W3 v3 r. O" J' q+ O
( I6 L" ^) u# x! G
- o7 ^2 s; C+ B% o) l. r1 ^' X

0 t5 `# z" q7 }6 W* C' U# B
% p8 h, s% h7 j  Z/ @
; I( }) q  ?7 k( E

* C+ U: u% X, t5 n% X& C: G

6 d# L  J! B8 Y4 Y+ h( x7 {' a
+ L$ z( c: Y$ P' M/ [+ D
5 \9 N7 F3 p) M, ^

: C: B1 R8 q4 o# c: S

# w; P% q2 B) {7 z5 T+ P! m" }7 i+ O8 h$ ]- u& C' M0 u' Q
; D; Z4 W. Y# H
systemctl restart iptables.service #最后重启防火墙使配置生效  h- J$ b* i7 C6 e5 ]; g" T7 e
systemctl enable iptables.service #设置防火墙开机启动
6 f8 _( q0 O" s) Yvi /etc/selinux/config#SELINUX=enforcing #注释掉#SELINUXTYPE=targeted #注释掉SELINUX=disabled #增加) @9 h8 B6 j8 P/ B) n/ s
:wq! #保存退出立即启用setenforce 0: H0 f- b8 w; ?, L
d.关闭SELINUX' ]# |2 B  |& r7 n, z4 E
>vi /etc/ssh/sshd_config
- a6 f* I' b' R# H" k--打开并修改PermitRootLogin 为noservice sshd restart
* f) W: q3 P3 A3 d: E4 e--打开并修改port为12887
! v# H* t+ l5 m+ x! l, {e.修改ssh默认端口,并取消允许root的ssh权限,防止暴力破解
) ^8 a/ \- d  [a.上传安装source到目录,使用wincp或vsftpd,建议使用wincpshell> yum install -y gcc gcc-c++ ' {/ X8 r0 D; S0 J1 f- ~: E
    安装依赖的package:
8 Y! t7 h/ D$ D; T  I7 F0 l. t      shell> yum install -y cmakelibaio bison ncurses-devel8 A% j0 t* y( [+ E: g. F7 ?
shell> groupadd mariadb --创建mariadb组4 Z! y, U* ^, c! F' T
shell> useradd -g mariadbmariadb --创建mariadb用户, h+ P9 Z/ H- R/ r' K0 h
shell>passwd mariadb  --修改密码b.添加用户和组
. m* _. n9 y8 Y8 u2 u! eshell>cd /usr/local
& U  T1 v4 g/ V3 |9 Nshell>tar -zxvf /root/mariadb-10.0.13-linux-x86_64.tar.gz shell>ln -s /usr/local/mariadb-10.0.13-linux-x86_64mariadbc.解压缩并创建链接. g  @* u1 J. l. X
shell> cd mariadb
! Y" E$ e9 }' Q' ^/ {: Zshell> chown -R mariadb .shell> chgrp -R mariadb.d.修改文件所属用户及群组
1 n3 V1 e: _' n" `& h0 fshell> scripts/mysql_install_db --user=mariadb  (root用户安装需指定用户,若使用mariadb用户安装则不需指定)e.创建数据目录并初始化权限表
9 I2 N) z1 M% R7 ~; _& tf.再次修改文件所属用户
& x9 z( ]) Q: F* P* |& Cshell> chown -R root .
6 R$ u4 g/ R. gshell> chown -R mariadbdata           数据目录开放给mariadb用户,其他可以开放给root3 c0 X: M( [3 s
g.安装完成启动mariadb$ G+ ^- ]1 R# y3 m0 h, X
shell > mkdir /var/log/mariadb$ J; u4 C0 c5 |4 P2 D# Y0 n" {  l
shell > cd /var/log | chown mariadb:mariadb mariadbshell > mkdir /var/run/mariadb
% e/ L+ l0 `' b( H" U+ H+ }shell > cd /var/run | chown mariadb:mariadb mariadb
  p4 b0 g* j- S2 Q& Y    --创建链接
1 Z* `8 J' L' Nshell > ln -s /usr/local/mariadb /usr/local/mysql    --启动数据库
1 d3 g& p2 l% p  ^shell> bin/mysqld_safe --user=mariadb            --创建两个日志目录并赋予相应权限
$ Y/ W- v4 t; ]4 _3 i# W. h将/usr/local/mariadb/bin加入$PATH变量中,并设置别名alias mariadb = 'mysql'( t8 R. r- x; N
编译环境变量source  .bash_profileshell > mysql) {7 c& |6 D( p) ^2 `* H
h.环境变量配置并尝试连接mysql4 h1 c) O! r" K
***************************************************************************************************** Remove any pre-installed old mysql files / libraries usually linux distros installs MySQL 5.1 **6 q$ s9 R* C( A
***************************************************************************************************rm -fR /etc/mysql) F8 \  B8 a- @/ G, l6 A
rm -fR /var/lib/mysql/mysql
- Q: q' U0 r' Y" x+ g- trm -fR /var/lib/mysqlfind -R / -name mysql*find / -name mysql*
2 p+ e9 k4 c* d6 D! l# U) c+ W** 中途如有错误,需重新安装,重新安装前先删除对应的文件( A$ L/ M+ V( D$ a/ c3 _) z* |
四.mariadb安装及配置) j' Y8 Q1 L( s+ d+ V2 z
   ; |, F, f6 V+ D5 F) ^

- r4 w- J' c- q+ j
5 {' `) q! s1 t: Y$ \4 D, W

4 L, H( P# i! b

- g. V; ]' D+ T2 u# L. A
: n$ v( J0 o) A  G6 ?( ]/ N7 A
7 f5 A  [% ~. p

0 c. l$ W1 u. U/ ~! ?, D
/ ?8 P$ l) H; J1 f# Q+ x, @/ E
9 a* d9 i; q* Y, e& c
& S' o6 N1 w# f) [) M; [
rm -fR /opt/mysql
) \) p' X, o4 A' K& K; n+ C3 Xfind / -name mysql*重新解压文件.j.用户安全配置/ c6 `: [& M6 [: L
列举出当前不需要密码的用户:) V. d' r/ E; S! A9 q+ t7 o" U
列举出不需要密码可以连接的db:
  a8 O; A" J5 d. I( U  [shell > SELECT User, Host, Password FROM mysql.user;! ~" ]0 V4 ?$ s6 a+ B9 h
允许匿名用户或无权限的用户访问test或test_前缀的数据库;--清除该表内容1 l% C9 E" d; `: q0 e, z
shell> mysql -u root
% P& d" Q- R# J1 i, R9 L& Vmysql> UPDATE mysql.user SET Password = PASSWORD('newpwd')
# C. `/ X9 H& p. `-> WHERE User = 'root';mysql> FLUSH PRIVILEGES;1).update
: K9 l  E5 S- l) K, q% Ashell> mysql -u root0 k8 o2 W; s' |2 R, x1 m$ C- i
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpwd');mysql> SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('newpwd');
$ v* h: I0 o6 u6 Pmysql> SET PASSWORD FOR 'root'@'::1' = PASSWORD('newpwd');
) M" S% e2 J  h; Vmysql> SET PASSWORD FOR 'root'@'host_name' = PASSWORD('newpwd');2).set password2 c  a$ b' Z5 W, ?0 c
shell> mysqladmin -u root password "newpwd"
" c, D: f, J; `" n! @% |5 o% t2 Hshell> mysqladmin -u root -h host_name password "newpwd"
. x4 b8 u! _9 y1 K4 g5 i! }2 t3).mysqladmin
. R% C- f2 \: r5 G! T: o+ {root密码的设定可以有三种方式:
& W( y4 l0 X, E; Amysql>drop user ''@'localhost';mysql>drop user ''@'mariadb\_server';
# D1 @7 @" y3 j# l+ b6 A( _删除空用户:) f0 Z  O) ^$ i3 J: Q/ @; `
shell > select user,db from mysql.db;
; u6 v6 v( T5 h0 u--测试数据库的安全保障:shell> mysql -u root -p
' [- D1 ~6 o$ f! z0 C# L$ u6 \Enter password: (enter root password here)
& M* U1 J3 Z$ n1 g* v" Umysql> DELETE FROM mysql.db WHERE Db LIKE 'test%';mysql> FLUSH PRIVILEGES;--彻底删除测试数据库
7 F/ t, N( A7 J( Imysql> drop database test;
( @) J7 e3 X9 W2 D重启系统后报错找不到对应的/usr/local/mysql/bin/mysqld# w5 b9 ]# A. a$ |6 f" l" @' G
原因是mysqld只认mysql/bin下面的mysqld,因此需要做一个链接到正确的mariadb/bin/mysqld允许远程root用户登录+ O8 u1 I: e& ]
    mysql> grant all privileges on *.* to 'root'@'192.168.1.111' identified by 'y}%oTiaU-3' with grant option;     mysql> flush privileges;     k.用户管理:; ?8 \! j. g; N' i/ f( a3 v
--cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mariadb  #将服务文件拷贝到init.d下,并重命名为mariadb--  vi mariadb  #修改mysqld_safe --datadir ..  为mysqld_safe --user=mariadb...--chkconfig --add mysql       添加服务
! t+ C# M$ z: L: e) Z--chkconfig --list            显示服务列表
2 }2 }  T1 X2 g            如果看到mysql的服务,并且3,4,5都是on的话则成功,如果是off,则键入          chkconfig --level 345 mysql on
! g" n& |: h- C4 i( }--reboot重启电脑
' S  x. n/ C7 H- u0 ]9 |& {" W+ i7 U8 w, U--netstat -na | grep 3306,如果看到有监听说明服务启动了
  n: w, h' ]2 ~/ a' I# w      l.设置mariadb开机自启动5 p3 E& c# K- R7 x- F( n; }
--vi /etc/my.cnf    [mysqld]    ...
, \# d7 d/ A& r# t) d  C; a    character-set-server=utf8mariadb默认的数据库字符集为latin1,如果需要修改为支持中文字符的数据库,则需要修改以下配置参数。
" c" O% O) R" y5 W7 I( ~0 g4 w* L3 t9 U2 q) n; J& k; _
您需要登录后才可以回帖 登录 | 注册

本版积分规则

返回首页|Archiver|手机版|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )

GMT+8, 2026-6-12 00:56 , Processed in 0.017309 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表