CentOS-7下安装MySQL5.6.22

一 安装环境

(1)CentOS版本:CentOS-7

查看方法

[root@bogon 桌面]# cat /etc/redhat-release

CentOS Linux release 7.0.1406 (Core)

来源:官网下载

下载地址:http://www.centos.org/

(2)MysqL版本:mysql-5.6.22

下载地址:https://edelivery.oracle.com/EPD/Search/handle_go

或者http://pan.baidu.com/s/1dDu6n9R

名称MysqL Database 5.6.22 RPM for Oracle Linux / RHEL 7 x86 (64bit)

二 安装方式选择

在网上搜了一下,Linux下安装MysqL有三种方式:

1 通过yum命令在线下载安装

2 下载离线rpm安装包安装

3 下载源码编译安装

方式1不打算用,因为我们大部分项目服务器是不能直接上Internet的,网上关于在线安装的教程很多,方式3对于只要应用MysqL的人来说没必要多此一举。

三 安装步骤

1. 解压下载的zip包,会发现有以下几个rpm包:

MysqL-client-advanced-5.6.22-1.el7.x86_64.rpm

MysqL-devel-advanced-5.6.22-1.el7.x86_64.rpm

MysqL-embedded-advanced-5.6.22-1.el7.x86_64.rpm

MysqL-server-advanced-5.6.22-1.el7.x86_64.rpm

MysqL-shared-advanced-5.6.22-1.el7.x86_64.rpm

MysqL-shared-compat-advanced-5.6.22-1.el7.x86_64.rpm

MysqL-test-advanced-5.6.22-1.el7.x86_64.rpm

2. 卸载MariaDB

如果直接点击rpm包安装会得到错误提示。因为CentOS的数据库已经不再是MysqL了,而是MariaDB,为什么呢?

MariaDB数据库管理系统是MysqL一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MysqL后,有将MysqL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。MariaDB的目的是完全兼容MysqL包括API和命令行,使之能轻松成为MysqL的代替品。

查看当前安装的mariadb包:

[root@bogon 桌面]# rpm -qa | grep mariadb

将它们统统强制性卸载掉:

[root@bogon 桌面]# rpm -e --nodeps mariadb-libs-5.5.35-3.el7.x86_64

[root@bogon 桌面]# rpm -e --nodeps mariadb-5.5.35-3.el7.x86_64

[root@bogon 桌面]# rpm -e --nodeps mariadb-server-5.5.35-3.el7.x86_64

3. 安装MysqL

双击下面三个包进行自动安装:

提示:其实第二个包devel我也不知道是干什么的,也不知道是不是必须的(上网搜了一下应该不是必须的),没有测试是否必须就已经点来装上了,也不想花时间去测试是否必须了,有测试过的朋友麻烦留言告知。

4. 启动MysqL

[root@bogon 桌面]#service MysqL start

得到错误ERROR!The server quit without updating PID file

我们这里主要是因为:selinux惹的祸,如果是centos系统,认会开启selinux。解决方法关闭它,打开/etc/selinux/config,把SELINUX=enforcing改为SELINUX=disabled后存盘退出重启机器。

然后再启动MysqL就没问题了:

查看MysqL运行状态:

[root@bogon 桌面]# service MysqL status

SUCCESS! MysqL running (2377)

5. 认root用户登录MysqL

[root@bogon 桌面]# MysqL -u root -p

Enter password:

ERROR 1045 (28000):Access denied for user 'root'@'localhost' (using password: YES)

发现有有错误,然后在网上查了一下说使用下面命令修改root初始化密码:

[root@bogon 桌面]# /usr/bin/MysqLadmin -u root password 'passok'

/usr/bin/MysqLadmin: connect to server at'localhost' Failed

error: 'Accessdenied for user 'root'@'localhost' (using password: NO)'

发现MysqL数据库认的root用户还是没办法设置密码进行登录,需要做一下操作:

重置MysqL中root用户密码及验证

还是不行,然后在网上又找到一个重置MysqL中root用户密码及验证的方法

(1) 停止MysqL服务

[root@bogon 桌面]# service MysqL stop

Shutting down MysqL.. SUCCESS!

(2) 输入绕过密码认证命令

[root@bogon 桌面]# MysqLd_safe --user=MysqL --skip-grant-tables--skip-networking &

[1] 5807

150117 22:23:31 MysqLd_safe Logging to '/var/lib/MysqL/bogon.err'.

150117 22:23:31 MysqLd_safe Starting MysqLddaemon with databases from /var/lib/MysqL

(3) 输入登录用户命令

[root@bogon 桌面]# MysqL -u root MysqL

Reading table information for completion oftable and column names

You can turn off this feature to get aquicker startup with -A

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

Your MysqL connection id is 1

Server version:5.6.22-enterprise-commercial-advanced MysqL Enterprise Server - AdvancedEdition (Commercial)

copyright (c) 2000,2014,Oracle and/or itsaffiliates. All rights reserved.

Oracle is a registered Trademark of OracleCorporation and/or its

affiliates. Other names may be Trademarksof their respective

owners.

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

(4) 输入修改root密码sql语句

MysqL> UPDATEuser SET Password=PASSWORD('passok') where USER='root';

Query OK,4 rows affected (0.04 sec)

Rows matched: 4 Changed: 4 Warnings: 0

(5) 输入数据刷新命令

MysqL> FLUSHPRIVILEGES;

ottom:8px;list-style:none;text-indent:2em;color:rgb(51,0 rows affected (0.00 sec)

(6) 退出

MysqL> quit

Bye

(7) 启动MysqL

[root@bogon 桌面]# service MysqL start

Starting MysqL SUCCESS!

登录MysqL,查看所有数据库

MysqL> show databases;

ERROR 1820 (HY000):You must SET PASSWORD before executing this statement

还是有错误啊,提示要再设置一下密码:

MysqL> SETPASSWORD = PASSWORD('passok');

显示数据库

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

| Database |

| information_schema |

| MysqL |

| performance_schema |

| test |

4 rows in set (0.00 sec)

进入数据库创建表、显示表:

MysqL> use test;

Database changed

MysqL> show tables;

Empty set (0.02 sec)

MysqL>create table testTable(name char(15) not null,passwd char(15) not null);

ottom:8px;list-style:none;text-indent:2em;color:rgb(51,0 rows affected (0.87 sec)

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

| Tables_in_test |

| testTable |

1 row in set (0.00 sec)

备注:我的设置的密码是“passok”。大家在执行showdatabases收到错误ERROR 1820 (HY000): You must SET PASSWORD before executing this statement后也可以试试以下面方式登录MysqL来执行showdatabases是否就不会有这个错误,我没有条件测试了:

登录MysqL界面:MysqL �Curoot -p(修改的新密码)

例如:MysqL -u root -p123456

MysqL安装后三个主要的目录及其功能

/var/lib/MysqL 数据库文件

/usr/share/MysqL 命令及配置文件

/usr/bin MysqLadmin、MysqLdump等命令

6. windows7上使用workbench连接到远端MysqL Server

(1) 下载安装workbench(MysqL Workbench 6.2.4 MSI for Windows x86(64bit))

(2) Windows下安装MysqL workbench需要具备以下环境:

Microsoft .NET Framework 4 Client Profile

Microsoft Visual C++ 2013 RedistributablePackage (x64)

一个就不用说了,第二个下载地址在:

http://www.microsoft.com/zh-CN/download/details.aspx?id=40784

(3)安装完成之后打开,进行连接配置:

发现连不上。

打开cmd命令行输入telnet 192.168.1.108 3306也提示连接不上(BTY:windows7认没有安装telnet,需要通过控制面板中的打开或者关闭windows功能来打开telnet客户端功能)。

如果要想远端访问MysqL数据库,还需要:

(1) 给指定用户赋予远端访问MysqL数据库的权限;

(2) 配置防火墙放开对3306端口的限制;

1给指定用户赋予远端访问MysqL数据库的权限

授权命令是:

grant 权限1,权限2,…权限n on 数据库名.表名 to用户名@用户地址 identified by‘口令’

[root@bogon 桌面]# MysqL -uroot-ppassok -e "GRANT ALL PRIVILEGES ON *.* TO'root'@'%' IDENTIFIED BY 'passok' WITH GRANT OPTION;"

Warning: Using a password on the commandline interface can be insecure.

用root用户的身份执行grant命令(-e参数表示执行一段sql命令),含义是:把在所有数据库的所有表的所有权限赋值给位于所有IP地址的root用户。如果你只想让位于192.168.1.108机器上面的root用户访问,命令如下:

MysqL -uroot -ppassok -e "GRANT ALL PRIVILEGES ON *.* TO'root'@'192.168.1.108' IDENTIFIED BY 'passok' WITH GRANT OPTION;"

2 配置防火墙放开3306端口的限制

CentOS 7.0版本的防火墙,认使用的是firewall,与之前的版本使用iptables是不一样,经过我的测试,只要firewall处于开启状态,就不可能远端访问MysqL数据库

首先将firewall关闭

[root@bogon 桌面]# systemctl stop firewalld.service #停止firewall

[root@bogon 桌面]# systemctl disable firewalld.service #禁止firewall开机启动

CentOS虽然认的不是iptables,但是也是已经安装好的,然后我按照网上的方法,为3306端口配置开放规则:

[root@bogon 桌面]# vi /etc/sysconfig/iptables

# sample configuration for iptables service

# you can edit this manually or usesystem-config-firewall

# please do not ask us to add additionalports/services to this default configuration

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A INPUT -m state --stateRELATED,ESTABLISHED -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT

-ARH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp--dport 22 -j ACCEPT

-A INPUT -j REJECT --reject-withicmp-host-prohibited

-A FORWARD -j REJECT --reject-withicmp-host-prohibited

COMMIT

然后重启防火墙:

[root@bogon 桌面]# service iptables restart

然后立刻用MysqL workbench测试,发现还是一样连不上。

后执行下面命令永久关闭iptables:

[root@bogon bin]# chkconfig iptables off

注意:正在将请求转发到“systemctl disable iptables.service”。

重启CentOS系统,连接成功

苏梅岛自由行,先看点评更安心

【点击进入】

TripAdvisor猫途鹰,为您提供全球旅行者的 旅游点评。让出国旅游吃 喝 玩 乐更安心!

查 看


看来要重启才能生效。

进一步测试:

查看firewall状态是关闭的:

[root@bogon bin]# service firewall status

Redirecting to /bin/systemctl status firewall.service

firewall.service

Loaded: not-found (Reason: No such file or directory)

Active: inactive (dead)

查看iptables状态也是关闭的:

[root@bogon bin]# service iptables status

Redirecting to /bin/systemctl status iptables.service

iptables.service - IPv4 firewall withiptables

Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled)

然后我又启动iptables防火墙,还是能够访问,然后把/etc/sysconfig/iptables中的规则全部注释掉之后,还是能够访问,重启还是可以,不知道为什么,查看iptables的状态也是处于激活状态的:

[root@bogonbin]# service iptables status

Active: active (exited) since 日 2015-01-1818:17:07 CST; 20s ago

Process: 14440 ExecStop=/usr/libexec/iptables/iptables.init stop(code=exited,status=0/SUCCESS)

Process: 14648 ExecStart=/usr/libexec/iptables/iptables.init start(code=exited,249);">MainPID: 14648 (code=exited,249);">1月 18 18:17:07 bogoniptables.init[14648]: iptables: Applying firewall rules: [ 确定 ]

1月 18 18:17:07 bogonsystemd[1]: Started IPv4 firewall with iptables.

反正永久关闭iptables防火墙是可以的,但是记得要重启。

7. 设置MysqL开机自启动

设置开机启动服务选择使用chkconfig命令,可以看到我们永久性关闭iptables就用的这个命令,命令的格式如下:

chkconfig 功能说明:检查,设置系统的各种服务。

语法:chkconfig [--add][--del][--list][系统服务]或 chkconfig [--level <等级代号>][系统服务][on/off/reset]

--add 添加服务

--del 删除服务

--list 查看各服务启动状态

我这里安装好了MysqL之后认就是开机自启动的:

[root@bogon 桌面]# chkconfig --list MysqL

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。

如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。

欲查看对特定 target 启用的服务请执行

'systemctl list-dependencies [target]'。

MysqL 0:关 1:关 2:开 3:开 4:开 5:开 6:关

如果不是开机自启动,使用开启MysqL服务自动开启命令:

chkconfig MysqLd on

chkconfig MysqL on

相关文章

Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native