CentOS   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了CentOS 7 安装 MySQL 5.6大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

http://www.centoscn.com/mysql/2016/0315/6844.html 环境 CentOS 7.1 (64-bit system) MySQL 5.6.24 CentOS 安装 参http://www.waylau.com/centos-7-installation-and-configuration/ 依赖 MySQL 依赖 libaio,所以先要安装 libai

http://www.centoscn.com/MysqL/2016/0315/6844.html

环境

CentOS 安装

http://www.waylau.com/centos-7-installation-and-configuration/

依赖

MysqL 依赖 libaio,所以先要安装 libaio

yum search libaio # 检索相关信息 yum install libaio # 安装依赖包 

成功安装,提示如下:

[root@bogon /]# yum install libaio 已加载插件fastestmirror Loading @H_714_79@mirror speeds from cached hostfile * base: @H_714_79@mirrors.yun-idc.com * extras: .163updates: .com 软件包 libaio-0.3.109-12.el7.x86_64 已安装并且是最新版本 无须任何处理 

检查 MysqL 是否已安装

yum list installed | grep MysqL 

如果有,就先全部卸载,命令如下:

-y remove MysqL-libs.x86_64 

若有多个依赖文件则依次卸载。当结果显示为 Complete!即卸载完毕。

下载

下载 MysqL Yum Repository

地址为http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

执行

wget http://dev.MysqL.com/get/MysqL-community-release-el7-5.noarch.rpm 

如果提示-bash: wget: 未找到命令,请先执行yum install wget安装 wget

安装

添加 MysqL Yum Repository

添加 MysqL Yum Repository 到你的系统 repository 列表中,执行

@H_896_197@localinstall MysqL-community-release-el7-5.noarch.rpm

显示

[root@bogon software]# yum localinstall MysqL-community-release-el7-5.noarch.rpm 已加载插件:fastestmirror 正在检查 MysqL-community--5.noarch.rpm: MysqL-community--5.noarch MysqL-community--5.noarch.rpm 将被安装 正在解决依赖关系 --> 正在检查事务 ---> 软件包 MysqL-community-release.noarch.0.el7-5 将被 安装 --> 解决依赖关系完成 依赖关系解决 ================================================================================ Package 架构 版本 源 大小 ================================================================================ 正在安装: MysqL-community-release noarch el7-5 /MysqL-community--5.noarch 4.3 k 事务概要 ================================================================================ 安装 1 软件包 总计:k 安装大小:k Is this ok [y/d/N] 

提示是否 OK ,输入 y

Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test transaction test succeeded Running transaction 正在安装 : MysqL-community-release-el7-5.noarch 1/1 验证中 : MysqL-community-release-el7-5.noarch 1/1 已安装: MysqL-community-release.noarch 0:el7-5 完毕! 

提示“完成!”,则说明 源添加成功。

验证下是否添加成功

yum repolist enabled | grep "MysqL.*-community.*" 

可以看到下面内:

[root@bogon software]# yum repolist enabled | grep "MysqL.*-community.*" mysql-connectors-community/x86_64 MysqL Connectors Community 1 MysqL-tools-community/x86_64 MysqL Tools Community 1 MysqL56-community/x86_64 MysqL 5.6 Community Server 13 

选择要启用 MysqL 版本

查看 MysqL 版本,执行

yum repolist all | 可以看到 5.5, 5.7 版本是认禁用的,因为现在最新的稳定版是 5.6

# yum repolist all | grep MysqL
mysql-connectors-community/x86_64 MysqL Connectors Community 启用: 14 mysql-connectors-community-source MysqL Connectors Community - sourc 禁用 MysqL-tools-community/x86_64 MysqL Tools Community 启用: 17 MysqL-tools-community-source MysqL Tools Community - source 禁用 MysqL55-community/x86_64 MysqL 5.5 Community Server 禁用 MysqL55-community-source MysqL 5.5 Community Server - sourc 禁用 MysqL56-community/x86_64 MysqL 5.6 Community Server 启用: 139 MysqL56-community-source MysqL 5.6 Community Server - sourc 禁用 MysqL57-community-dmr/x86_64 MysqL 5.7 Community Server Develop 禁用 MysqL57-community-dmr-source MysqL 5.7 Community Server Develop 禁用

可以通过类似下面的语句来启动某些版本

yum-config-manager --disable MysqL56-community yum-config-manager --enable MysqL57-community-dmr 

或者通过修改/etc/yum.repos.d/MysqL-community.repo文件

# Enable to use MysqL 5.6 [MysqL56-community] name=MysqL 5.6 Community Server baseurl=http://repo.MysqL.com/yum/MysqL-5.6-community/el/7/$basearch/ enabled=1 gpgcheck=1 gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-MysqL 

其中enabled=0是指禁用,enabled=1指启用。

注意: 任何时候,只能启用一个版本。

查看当前的启动的 MysqL 版本

# yum repolist enabled | grep MysqL mysql-connectors-community/x86_64 MysqL Connectors Community 14 MysqL-tools-community/x86_64 MysqL Tools Community 17 MysqL56-community/x86_64 MysqL 139 

本例,我们启用的是 5.6 版本。

通过 Yum 来安装 MysqL

yum install MysqL-community-server

Yum 会自动处理 MysqL 与其他组件的依赖关系:

[root@bogon software]# yum server 已加载插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.yun-idc.com * extras: mirrors.163.com * updates: mirrors.163.com 正在解决依赖关系 ---> 软件包 MysqL-community-server.x86_64.0.5.6.24-3.el7 将被 安装 --> 正在处理依赖关系 MysqL-community-common(x86-64) = 5.6.24-3.el7,它被软件包 m ysql-community-server-5.6.24-3.el7.x86_64 需要 --> 正在处理依赖关系 MysqL-community-client(x86-64) = 5.6.24-3.el7,它被软件包 m ysql-community-server-5.6.24-3.el7.x86_64 需要 --> 正在处理依赖关系 perl(warnings),它被软件包 MysqL-community-server-5.6.24-3. el7.x86_64 需要 --> 正在处理依赖关系 perl(Strict),它被软件包 MysqL-community-server-5.6.24-3.el 7.x86_64 需要 --> 正在处理依赖关系 perl(if),它被软件包 MysqL-community-server-5.6.24-3.el7.x8 6_64 需要 --> 正在处理依赖关系 perl(Sys::HostName),它被软件包 MysqL-community-server-5.6. 24-3.el7.x86_64 需要 --> 正在处理依赖关系 perl(POSIX),它被软件包 MysqL-community-server-5.6.24-3.el7 .x86_64 需要 --> 正在处理依赖关系 perl(Getopt::Long),它被软件包 MysqL-community-server-5.6.2 4-3.el7.x86_64 需要 --> 正在处理依赖关系 perl(File::Temp),它被软件包 MysqL-community-server-5.6.24- 3.el7.x86_64 需要 --> 正在处理依赖关系 perl(File::SpeC),它被软件包 MysqL-community-server-5.6.24- 3.el7.x86_64 需要 --> 正在处理依赖关系 perl(File::Path),它被软件包 MysqL-community-server-5.6.24- 3.el7.x86_64 需要 --> 正在处理依赖关系 perl(File::Copy),它被软件包 MysqL-community-server-5.6.24- 3.el7.x86_64 需要 --> 正在处理依赖关系 perl(File::BaseName),它被软件包 MysqL-community-server-5.6 .24-3.el7.x86_64 需要 --> 正在处理依赖关系 perl(Fcntl),它被软件包 MysqL-community-server-5.6.24-3.el7 .x86_64 需要 --> 正在处理依赖关系 perl(Data::Dumper),它被软件包 MysqL-community-server-5.6.2 4-3.el7.x86_64 需要 --> 正在处理依赖关系 perl(DBI),它被软件包 MysqL-community-server-5.6.24-3.el7.x 86_64 需要 --> 正在处理依赖关系 net-tools,它被软件包 MysqL-community-server-5.6.24-3.el7.x 86_64 需要 --> 正在处理依赖关系 /usr/bin/perl,它被软件包 MysqL-community-server-5.6.24-3.e l7.x86_64 需要 ---> 软件包 MysqL-community-client.x86_64.0.5.6.24-3.el7 将被 安装 --> 正在处理依赖关系 MysqL-community-libs(x86-64) = 5.6.24-3.el7,它被软件包 mys ql-community-client-5.6.24-3.el7.x86_64 需要 --> 正在处理依赖关系 perl(Exporter),它被软件包 MysqL-community-client-5.6.24-3. el7.x86_64 需要 ---> 软件包 MysqL-community-common.x86_64.0.5.6.24-3.el7 将被 安装 ---> 软件包 net-tools.x86_64.0.2.0-0.17.20131004git.el7 将被 安装 ---> 软件包 perl.x86_64.4.5.16.3-285.el7 将被 安装 --> 正在处理依赖关系 perl-libs = 4:5.16.3-285.el7,它被软件包 4:perl-5.16.3-285. el7.x86_64 需要 --> 正在处理依赖关系 perl(Socket) >= 1.3,它被软件包 4:perl-5.16.3-285.el7.x86_6 4 需要 --> 正在处理依赖关系 perl(Scalar::Util) >= 1.10,它被软件包 4:perl-5.16.3-285.el 7.x86_64 需要 --> 正在处理依赖关系 perl-macros,它被软件包 4:perl-5.16.3-285.el7.x86_64 需要 --> 正在处理依赖关系 perl-libs,它被软件包 4:perl-5.16.3-285.el7.x86_64 需要 --> 正在处理依赖关系 perl(threads::shared),它被软件包 4:perl-5.16.3-285.el7.x86 _64 需要 --> 正在处理依赖关系 perl(threads),它被软件包 4:perl-5.16.3-285.el7.x86_64 需要 --> 正在处理依赖关系 perl(constant),它被软件包 4:perl-5.16.3-285.el7.x86_64 需 要 --> 正在处理依赖关系 perl(Time::Local),它被软件包 4:perl-5.16.3-285.el7.x86_64 需要 --> 正在处理依赖关系 perl(Time::HiRes),它被软件包 4:perl-5.16.3-285.el7.x86_64 需要 --> 正在处理依赖关系 perl(StorablE),它被软件包 4:perl-5.16.3-285.el7.x86_64 需 要 --> 正在处理依赖关系 perl(Socket),它被软件包 4:perl-5.16.3-285.el7.x86_64 需要 --> 正在处理依赖关系 perl(Scalar::Util),它被软件包 4:perl-5.16.3-285.el7.x86_64 需要 --> 正在处理依赖关系 perl(Pod::Simple::XHTML),它被软件包 4:perl-5.16.3-285.el7. x86_64 需要 --> 正在处理依赖关系 perl(Pod::Simple::Search),它被软件包 4:perl-5.16.3-285.el7 .x86_64 需要 --> 正在处理依赖关系 perl(Filter::Util::Call),它被软件包 4:perl-5.16.3-285.el7. x86_64 需要 --> 正在处理依赖关系 perl(Carp),它被软件包 4:perl-5.16.3-285.el7.x86_64 需要 --> 正在处理依赖关系 libperl.so()(64bit),它被软件包 4:perl-5.16.3-285.el7.x86_6 4 需要 ---> 软件包 perl-DBI.x86_64.0.1.627-4.el7 将被 安装 --> 正在处理依赖关系 perl(RPC::PlServer) >= 0.2001,它被软件包 perl-DBI-1.627-4. el7.x86_64 需要 --> 正在处理依赖关系 perl(RPC::PlClient) >= 0.2000,它被软件包 perl-DBI-1.627-4. el7.x86_64 需要 ---> 软件包 perl-Data-Dumper.x86_64.0.2.145-3.el7 将被 安装 ---> 软件包 perl-File-Path.noarch.0.2.09-2.el7 将被 安装 ---> 软件包 perl-File-Temp.noarch.0.0.23.01-3.el7 将被 安装 ---> 软件包 perl-Getopt-Long.noarch.0.2.40-2.el7 将被 安装 --> 正在处理依赖关系 perl(Pod::UsagE) >= 1.14,它被软件包 perl-Getopt-Long-2.40- 2.el7.noarch 需要 --> 正在处理依赖关系 perl(Text::ParseWords),它被软件包 perl-Getopt-Long-2.40-2. el7.noarch 需要 ---> 软件包 perl-PathTools.x86_64.0.3.40-5.el7 将被 安装 ---> 软件包 mariadb-libs.x86_64.1.5.5.41-2.el7_0 将被 取代 ---> 软件包 MysqL-community-libs.x86_64.0.5.6.24-3.el7 将被 舍弃 ---> 软件包 perl-Carp.noarch.0.1.26-244.el7 将被 安装 ---> 软件包 perl-Exporter.noarch.0.5.68-3.el7 将被 安装 ---> 软件包 perl-Filter.x86_64.0.1.49-3.el7 将被 安装 ---> 软件包 perl-PlRPC.noarch.0.0.2020-14.el7 将被 安装 --> 正在处理依赖关系 perl(Net::Daemon) >= 0.13,它被软件包 perl-PlRPC-0.2020-14. el7.noarch 需要 --> 正在处理依赖关系 perl(Net::Daemon::Test),它被软件包 perl-PlRPC-0.2020-14.el 7.noarch 需要 --> 正在处理依赖关系 perl(Net::Daemon::Log),它被软件包 perl-PlRPC-0.2020-14.el7 .noarch 需要 --> 正在处理依赖关系 perl(Compress::Zlib),它被软件包 perl-PlRPC-0.2020-14.el7.n oarch 需要 ---> 软件包 perl-Pod-Simple.noarch.1.3.28-4.el7 将被 安装 --> 正在处理依赖关系 perl(Pod::Escapes) >= 1.04,它被软件包 1:perl-Pod-Simple-3. 28-4.el7.noarch 需要 --> 正在处理依赖关系 perl(EncodE),它被软件包 1:perl-Pod-Simple-3.28-4.el7.noarc h 需要 ---> 软件包 perl-Pod-Usage.noarch.0.1.63-3.el7 将被 安装 --> 正在处理依赖关系 perl(Pod::Text) >= 3.15,它被软件包 perl-Pod-Usage-1.63-3.e l7.noarch 需要 --> 正在处理依赖关系 perl-Pod-Perldoc,它被软件包 perl-Pod-Usage-1.63-3.el7.noar ch 需要 ---> 软件包 perl-Scalar-List-Utils.x86_64.0.1.27-248.el7 将被 安装 ---> 软件包 perl-Socket.x86_64.0.2.010-3.el7 将被 安装 ---> 软件包 perl-Storable.x86_64.0.2.45-3.el7 将被 安装 ---> 软件包 perl-Text-ParseWords.noarch.0.3.29-4.el7 将被 安装 ---> 软件包 perl-Time-HiRes.x86_64.4.1.9725-3.el7 将被 安装 ---> 软件包 perl-Time-Local.noarch.0.1.2300-2.el7 将被 安装 ---> 软件包 perl-constant.noarch.0.1.27-2.el7 将被 安装 ---> 软件包 perl-libs.x86_64.4.5.16.3-285.el7 将被 安装 ---> 软件包 perl-macros.x86_64.4.5.16.3-285.el7 将被 安装 ---> 软件包 perl-threads.x86_64.0.1.87-4.el7 将被 安装 ---> 软件包 perl-threads-shared.x86_64.0.1.43-6.el7 将被 安装 ---> 软件包 perl-Encode.x86_64.0.2.51-7.el7 将被 安装 ---> 软件包 perl-IO-Compress.noarch.0.2.061-2.el7 将被 安装 --> 正在处理依赖关系 perl(Compress::raw::Zlib) >= 2.061,它被软件包 perl-IO-Comp ress-2.061-2.el7.noarch 需要 --> 正在处理依赖关系 perl(Compress::raw::Bzip2) >= 2.061,它被软件包 perl-IO-Com press-2.061-2.el7.noarch 需要 ---> 软件包 perl-Net-Daemon.noarch.0.0.48-5.el7 将被 安装 ---> 软件包 perl-Pod-Escapes.noarch.1.1.04-285.el7 将被 安装 ---> 软件包 perl-Pod-Perldoc.noarch.0.3.20-4.el7 将被 安装 --> 正在处理依赖关系 perl(parent),它被软件包 perl-Pod-Perldoc-3.20-4.el7.noarch 需要 --> 正在处理依赖关系 perl(http::Tiny),它被软件包 perl-Pod-Perldoc-3.20-4.el7.no arch 需要 ---> 软件包 perl-podlators.noarch.0.2.5.1-3.el7 将被 安装 ---> 软件包 perl-Compress-Raw-Bzip2.x86_64.0.2.061-3.el7 将被 安装 ---> 软件包 perl-Compress-Raw-Zlib.x86_64.1.2.061-4.el7 将被 安装 ---> 软件包 perl-http-Tiny.noarch.0.0.033-3.el7 将被 安装 ---> 软件包 perl-parent.noarch.1.0.225-244.el7 将被 安装 Package 架构 版本 源 大小 ================================================================================ 正在安装: MysqL-community-libs x86_64 5.6.24-3.el7 MysqL56-community 2.0 @H_247_131@m 替换 mariadb-libs.x86_64 1:5.5.41-2.el7_0 MysqL-community-server x86_64 58 @H_247_131@m 为依赖而安装: MysqL-community-client x86_64 19 @H_247_131@m MysqL-community-common x86_64 256 k net-tools x86_64 2.0-0.17.20131004git.el7 base 304 k perl x86_64 4:5.16.3-285.el7 base 8.0 @H_247_131@m perl-Carp noarch 1.26-244.el7 base k perl-Compress-Raw-Bzip2 x86_64 2.061-3.el7 base 32 Raw-Zlib x86_64 -4.el7 base 57 k perl-DBI x86_64 1.627802 Data-Dumper x86_64 2.14547 Encode x86_64 2.51-7.el7 base 1.5 @H_247_131@m perl-Exporter noarch 5.6828 File-Path noarch 2.09-2.el7 base 26 File-Temp noarch 0.23.0156 k perl-Filter x86_64 1.4976 k perl-Getopt-Long noarch 2.40http-Tiny noarch 0.03338 k perl-IO-Compress noarch 260 k perl-Net-Daemon noarch 0.48-5.el7 base 51 k perl-PathTools x86_64 3.4082 k perl-PlRPC noarch 0.2020-14.el7 base 36 k perl-Pod-Escapes noarch 1.0450 k perl-Pod-Perldoc noarch 3.2087 k perl-Pod-Simple noarch 3.28216 k perl-Pod-Usage noarch 1.6327 k perl-Scalar-List-Utils x86_64 1.27-248.el7 base k perl-Socket x86_64 2.01049 k perl-Storable x86_64 2.4577 k perl-Text-ParseWords noarch 3.2914 Time-HiRes x86_64 1.972545 Time-Local noarch 1.230024 constant noarch k perl-libs x86_64 687 k perl-macros x86_64 42 parent noarch 0.22512 k perl-podlators noarch 2.5.1112 k perl-threads x86_64 1.87k perl-threads-shared x86_64 1.43-6.el7 base 39 2 软件包 (+37 依赖软件包) 总下载量:92 @H_247_131@m N]: 

输入 y 继续下载相关文件

 -------------------------------------------------------------------------------- 总计 1.1 MB/s | 92 MB 01:24 从 file:/etc/pki/rpm-gpg/RPM-GPG-KEY-MysqL 检索密钥 导入 GPG key 0x5072E1F5: 用户ID : "MysqL Release ENGIneering <MysqL-build@oss.Oracle.com>" 指纹 : a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5 软件包 : MysqL-community-release-el7-5.noarch (@/MysqL-community-release-el7-5.noarch) 来自 : file:/etc/pki/rpm-gpg/RPM-GPG-KEY-MysqL 是否继续?[y/N]: 

遇到上述提示,输入 y 继续,执行完成会提示“完毕!”。此时MysqL 安装完成,它包含了 MysqL-community-server、MysqL-community-client、MysqL-community-common、MysqL-community-libs 四个包。

@H_962_944@rpm -qi MysqL-community-server.x86_64 0:5.6.24-3.el7

whereis MysqL

可以看到 MysqL 的安装目录是 /usr/bin/

[root@localhost ~]# whereis MysqL MysqL: /usr/bin/MysqL /usr/lib64/MysqL /usr/share/MysqL /usr/share/man/man1/MysqL.1.gz 

启动和关闭 MysqL Server

启动 MysqL Server

systemctl start MysqLd 

查看 MysqL Server 状态

systemctl status MysqLd 

关闭 MysqL Server

@H_896_197@stop MysqLd

测试是否安装成功

MysqL 

可以进入 MysqL 命令行界面

# MysqL Welcome to the MysqL monitor. Commands end with ; or \g. Your MysqL connection id is 2 Server version: .24 MysqL Community Server (GPL) Copyright (C) 2000,2015,Oracle and/or its affiliates. All rights reserved. Oracle is a registered Trademark of Oracle Corporation or its affiliates. Other names may be Trademarks of their respective owners. Type 'Help;' or '\h' for Help. Type '\c' to clear the current input statement. MysqL> 

防火墙设置

远程访问 MysqL, 需开放认端口号 3306.

方式1:iptables(CentOS 7.x版本之前用法,不推荐)

打开 iptables 的配置文件

vi /etc/sysconfig/iptables 

修改

*filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0] :OUTPUT ACCEPT [0] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -j REjeCT --reject-with icmp-host-prohibited -A FORWARD -j REjeCT --reject-with icmp-host-prohibited COMMIT 

在里面加入这2行:

-A RH-Firewall-1-INPUT -@H_247_131@m statestate NEW -@H_247_131@m tcp -p tcp –dport 3306 -j ACCEPT -A RH-Firewall-@H_83_241@m udp -p udp –dport 3306 -j ACCEPT 

改为

@H_896_197@j ACCEPT -A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -NEW -m udp -p udp –dport j REjeCT --reject-with icmp-host-prohibited COMMIT
如果该 iptables 配置文件 不存在,先执行yum install iptables-services安装

执行 iptables 重启生效

service iptables restart 

方式2:firewall-cmd(推荐)

firewall-cmd --peRMANent --zone=public --add-port=3306/tcp firewall-cmd --peRMANent --zone=public --add-port=3306/udp

这样就开放了相应的端口。

--reload

使最新的防火墙设置规则生效。

MysqL 安全设置

服务器启动后,可以执行

MysqL_secure_installation; 

看到如下提示

[root@bogon software]# MysqL_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MysqL SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MysqL to secure it,we'll need the current password for the root user. If you've just installed MysqL,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): 

此时输入 root 原始密码(初始化安装的话为空),接下来,为了安全,MysqL提示你重置 root 密码,移除其他用户账号,禁用 root 远程登录,移除 test 数据库,重新加载 privilege 表格等,你只需输入 y 继续执行即可。

OK,successfully used password,moving on... SetTing the root password ensures that nobody can log into the MysqL 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 MysqL installation has an anonymous user,allowing anyone without having to have a user account created for them. This is intended only for tesTing,140)">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 connect from 'localhost'. This ensures that someone cAnnot guess at the root password from the network. Disallow root login remotely? [Y/:rgb(252,MysqL comes with a database named 'test' that anyone can access. This is also intended and should be removed into a production environment. Remove test database access to it? [Y/n] y - Dropping database... ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist ... Failed! Not critical,140)">keep moving... - Removing privileges on database... ... success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables Now? [Y/success! All done! If you've completed all of the above steps,your MysqL installation should Now be secure. Thanks for using MysqL! Cleaning up... 

至此,整个 MysqL 安装完成。

远程访问设置

创建一个普通用户 sa ,密码是 some_pass

CREATE USER 'sa'@'%' IDENTIFIED BY 'some_pass'; 
@H_607_1361@ 给这个用户授予 SELECT,INSERT,updatE,@R_616_9421@E 的远程访问的权限,这个账号一般用于提供给实施的系统访问

@H_896_197@GRANT SELECT,140)">INSERT,140)">updatE,140)">@R_616_9421@E ON *.* TO '%'; @H_607_1361@ 创建一个管理员用户 admin 账号 ,密码是 some_pass

'admin'@ 给这个用户授予所有的远程访问的权限。这个用户主要用于管理整个数据库、备份、还原等操作。

@H_896_197@GRANT

ALL 使授权立刻生效

@H_896_197@flush privileges;

更改数据存放目录

创建数据存放目录

@H_607_1361@ home 目录下建立 data 目录

@H_896_197@mkdir /home/data

MysqL 服务进程停掉

@H_607_1361@ 如果 MysqL 是启动的,要先关闭

MysqLadmin -u root -p shutdown 

移动数据到数据存放目录

@H_607_1361@ /var/lib/MysqL整个目录移到/home/data,执行

@H_857_18@mv /var/lib/MysqL /home/data 
@H_607_1361@ 这样就把 MysqL 的数据文件移动到了/home/data/MysqL

@H_607_1361@ 修改/etc/my.cnf文件

[MysqLd] datadir=/home/data/MysqL socket=/home/data/MysqL/MysqL.sock  [MysqL] socket=/home/data/MysqL/MysqL.sock 
@H_607_1361@ 修改权限

@H_896_197@chown -R MysqL:MysqL /home/data/MysqL @H_607_1361@ 重启后,如果不能启动 MysqL 服务,执行

vi /etc/sysconfig/selinux 
@H_607_1361@ 调整

SELINUX=permissive 
@H_607_1361@ 保存设置,执行 reboot 重启生效

开机自起

@H_607_1361@ 查看 MysqL 服务是否开机启动

# systemctl is-enabled MysqL.service;echo $? enabled 0 
@H_607_1361@ 如果是 enabled 则说明是开机自动,如果不是,执行

chkconfig --levels 235 MysqLd on 

设置字符集

@H_607_1361@ 一般的,为了支持中文,我们应该讲字符集设为 UTF-8, 执行

@H_896_197@SHOW VARIABLES LIKE 'character%'; @H_607_1361@ 查看当前 MysqL 字符集

MysqL> SHOW VARIABLES LIKE 'character%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/MysqL/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 seC) 
@H_607_1361@ 可以看到认服务器的字符器是 latin1 ,对中文不友好。
修改/etc/my.cnf文件添加字符集的设置

[MysqLd] @R_419_115@ = utf8  [MysqL] default-character-set = utf8 
@H_607_1361@ 重启 MysqL,可以看到字符集已经修改

'character%' -> ; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_database | utf8 | | character_set_server | utf8 | | character_set (0.00 seC) 

其他常用配置配置

@H_607_1361@ 调整 MysqL 运行参数,修改/etc/my.cnf文件,常用配置如下:

[MysqLd] basedir = path # 使用给定目录作为根目录(安装目录)。 datadir = path # 从给定目录读取数据库文件。 pid-file = filename # 为MysqLd程序指定一个存放进程ID的文件(仅适用于UNIX/Linux系统); socket = /tmp/MysqL.sock # 为MysqL客户程序与服务器之间的本地通信指定一个套接文件(Linux下认是/var/lib/MysqL/MysqL.sock文件) port = 3306 # 指定Mssql侦听的端口 key_buffer = 384M # key_buffer是用于索引块的缓冲区大小,增加它可得到更好处理的索引(对所有读和多重写)。 索引块是缓冲的并且被所有的线程共享,key_buffer的大小视内存大小而定。 table_cache = 512 # 为所有线程打开表数量增加该值能增加MysqLd要求的文件描述符的数量。可以避免频繁的打开数据表产生的开销 sorT_Buffer_size = 2M # 每个需要进行排序的线程分配该大小的一个缓冲区。增加这值加速ORDER BY或GROUP BY操作。 注意:该参数对应的分配内存是每连接独占!如果有100个连接,那么实际分配的总共排序缓冲区大小为100×6=600MB read_buffer_size = 2M # 读查询操作所能使用的缓冲区大小。和sorT_Buffer_size一样,该参数对应的分配内存也是每连接独享。 query_cache_size = 32M # 指定MySQL查询结果缓冲区的大小 read_rnd_buffer_size = 8M # 改参数在使用行指针排序之后,随机读用的。 myisam_sorT_Buffer_size =64M # MyISAM表发生变化时重新排序所需的缓冲 thread_concurrency = 8 # 最大并发线程数,取值为服务器逻辑cpu数量×2,如果cpu支持H.T超线程,再×2 thread_cache = 8 # #缓存可重用的线程数 skip-locking # 避免MysqL的外部锁定,减少出错几率增强稳定性。 [MysqLdump] max_allowed_packet =16M # 服务器和客户端之间最大能发送的可能信息包 [myisamchk] key_buffer = 256M sorT_Buffer = 256M read_buffer = 2M write_buffer = 2M 

其他可选参数:

BACk_log = 384

指定MysqL可能的连接数量。 当MysqL主线程在很短时间内接收到非常多的连接请求,该参数生效,主线程花费很短时间检查连接并且启动一个新线程。 BACk_log参数的值指出在MysqL暂时停止响应新请求之前的短时间内多少个请求可以被存在堆栈中。 如果系统在一个短时间内有很多连接,则需要增大该参数的值,该参数值指定到来的TCP/IP连接的侦听队列的大小。 试图设定BACk_log高于你的操作系统的限制将是无效的。认值为50。对于Linux系统推荐设置为小于512的整数。

@H_857_18@max_connections = n

MysqL服务器同时处理的数据库连接的最大数量(认设置是100)。超过限制后会报 Too many connections 错误

key_buffer_size = n

用来存放索引区块的RMA值(认设置是8M),增加它可得到更好处理的索引(对所有读和多重写)

record_buffer:

每个进行一个顺序扫描的线程为其扫描的每张表分配这个大小的一个缓冲区。 如果你做很多顺序扫描,你可能想要增加该值。认数值是131072(128K)

wait_timeout:

服务器在关闭它之前在一个连接上等待行动的秒数。

interactive_timeout:

服务器在关闭它前在一个交互连接上等待行动的秒数。 一个交互的客户被定义为对 MysqL_real_connect()使用 CLIENT_INTERACTIVE 选项的客户。 认数值是28800,可以把它改为3600。

skip-name-resolve

禁止MysqL对外部连接进行DNS解析,使用这一选项可以消除MysqL进行DNS解析的时间。 但需要注意,如果开启该选项,则所有远程主机连接授权都要使用IP地址方式,否则MysqL将无法正常处理连接请求!

log-slow-queries = slow.log

记录慢查询,然后对慢查询一一优化

skip-innodb

skip-bdb

关闭不需要的表类型,如果你需要,就不要加上这个

备份、还原

方法1:命令行

备份@H_266_1696@
MysqLdump --socket=/home/data/MysqL/MysqL.sock --single-transaction=TRUE -u root -p emsc > emsc.sql 

还原@H_266_1696@
MysqL --socket=/MysqL/MysqL.sock -u root -p emsc < emsc.sql 

方法2:Workbench

备份@H_266_1696@

还原@H_266_1696@ @H_607_1361@

大佬总结

以上是大佬教程为你收集整理的CentOS 7 安装 MySQL 5.6全部内容,希望文章能够帮你解决CentOS 7 安装 MySQL 5.6所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。