Centos7离线安装MySQL5.6(详细步骤)
历史版本卸载
1、卸载系统自带的Mariadb
[root@localhost ~]# rpm -qa|grep mariadb // 查询出来已安装的mariadb
[root@localhost ~]# rpm -e --nodeps 文件名 // 卸载mariadb,文件名为上述命令查询出来的文件
一、安装
1、下载安装包 mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz(可根据系统版本前往官网自行下载:http://dev.mysql.com/downloads/mysql/)
2、创建mysql用户组
[root@localhost ~]# groupadd mysql
3、创建用户并加入mysql用户组
[root@localhost ~]# useradd -g mysql mysql
[root@localhost ~]# tar -xvf mysql-5.7.20-1.el7.x86_64.rpm-bundle.tar
4、压缩包放至 /usr/local/(通过mv 要移动的文件 /usr/local/)
[root@localhost ~]# tar -xvf mysql-5.7.20-1.el7.x86_64.rpm-bundle.tar
5、解压安装包
[root@localhost ~]# mv mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz /usr/local/ // 通过mv 要移动的文件 /usr/local/
[root@localhost ~]# tar -zxvf mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz // 解压
注:如果压缩包为:mysql-5.6.42-linux-glibc2.12-x86_64.tar,则解压命令为 tar -xvf mysql-5.6.42-linux-glibc2.12-x86_64.tar
6、将解压好的文件夹重命名为mysql
[root@localhost local]# mv 解压出来的文件夹名 mysql
7、配置my.cnf
[root@localhost ~]# cd usr/local/mysql/support-files
[root@localhost support-files]# cp my-default.cnf /etc/my.cnf
[root@localhost support-files]# vim /etc/my.cnf
通过vim编辑器编辑my.cnf代码如下:
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock
[mysqld]
skip-name-resolve
#设置3306端口
port = 3306
socket=/var/lib/mysql/mysql.sock
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_name=1
max_allowed_packet=16M
8、进入安装mysql软件目录
[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# chown -R mysql:mysql ./ 修改当前目录拥有着为mysql用户
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ 安装数据库
注:若执行以上最后一个命令出现以下问题:
FATAL ERROR: please install the following Perl modules before executing
./scripts/mysql_install_db:Data::Dumper
解决方法 :安装autoconf库
命令: yum -y install autoconf //此包安装时会安装Data:Dumper模块
安装完成重新执行上述最后一个命令
重新回到上述第三个命令继续操作:
[root@localhost mysql]# chown -R mysql:mysql data //修改当前data目录的拥有者为mysql用户
到此数据库安装完毕!
二、配置
1、授予my.cnf最大权限
[root@localhost ~]# chown 777 /etc/my.cnf
2、复制启动脚本到资源目录
[root@localhost mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
3、增加mysqld服务控制脚本执行权限
[root@localhost mysql]# chmod +x /etc/rc.d/init.d/mysqld
4、将mysqld服务加入到系统服务
[root@localhost mysql]# chkconfig --add mysqld
5、检查mysqld服务是否已经生效
[root@localhost mysql]# chkconfig --list mysqld
命令输出类似下面的结果:
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
表明mysqld服务已经生效,在2、3、4、5运行级别随系统启动而自动启动,以后可以使用service命令控制mysql的启动和停止
service mysqld start // 启动服务
service mysqld stop // 停止服务
service mysqld restart // 重启服务
service mysqld status // 状态查看
6、启动mysqld
[root@localhost mysql]# service mysqld start
7、将mysql的bin目录加入PATH环境变量,编辑 ~/.bash_profile文件
[root@localhost mysql]# vim ~/.bash_profile
在文件最后添加如下信息:
export PATH=$PATH:/usr/local/mysql/bin
按ESC键输入:wq回车保存并退出
执行下面的命令是修改的内容立即生效:
[root@localhost mysql]# source ~/.bash_profile
8、root账户登录mysql,默认是没有密码的
[root@localhost mysql]# mysql -uroot -p
要输入密码的时候直接回车即可。
9、设置root账户密码
mysql>use mysql;
mysql>update user set password=password('你的新密码') where user='root' and host='localhost';
mysql>flush privileges;
注:mysql 新设置用户或更改密码后需用flush privileges刷新MySQL的系统权限相关表,否则会出现拒绝访问,还有一种方法,就是重新启动mysql服务器,来使新设置生效。
10、设置远程主机登录
mysql>GRANT ALL PRIVILEGES ON *.* TO 'your username'@'%' IDENTIFIED BY 'your password' WITH GRANT OPTION;
三、常见问题
1、初始化mysql数据库时出现下面错误,FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper 原因是缺少Data:dumper模块,需要安装autoconf库,这里我们采用离线方式安装。
下载地址:http://ftp.gnu.org/gnu/autoconf/
我选择的是2.69版本下载http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
[root@localhost ~]# tar -zxvf autoconf-2.69.tar.gz
[root@localhost ~]# cd autoconf-2.69
[root@localhost autoconf-2.69]# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
configure: autobuild project... GNU Autoconf
configure: autobuild revision... 2.69
configure: autobuild hostname... localhost
configure: autobuild timestamp... 20170115T063135Z
checking whether /bin/sh -n is known to work... yes
checking for characters that cannot appear in file names... none
checking whether directories can have trailing spaces... yes
checking for expr... /usr/bin/expr
checking for GNU M4 that supports accurate traces... configure: error: no acceptable m4 could be found in $PATH.
GNU M4 1.4.6 or later is required; 1.4.16 or newer is recommended.
GNU M4 1.4.15 uses a buggy replacement strstr on some systems.
Glibc 2.9 - 2.12 and GNU M4 1.4.11 - 1.4.15 have another strstr bug.
执行./configure时出现下面错误:
checking for GNU M4 that supports accurate traces... configure: error: no acceptable m4 could be found in $PATH.
GNU M4 1.4.6 or later is required; 1.4.16 or newer is recommended.
原因是需要安装M4
打开m4下载地址:
http://ftp.gnu.org/gnu/m4/
我下载的是最新版本http://ftp.gnu.org/gnu/m4/m4-1.4.18.tar.gz
[root@localhost ~]# tar -zxvf m4-1.4.18.tar.gz
[root@localhost ~]# cd m4-1.4.18
[root@localhost m4-1.4.18]# ./configure
m4安装完成之后再次执行再次切换到autoconf目录继续安装
[root@localhost ~]# cd autoconf-2.69
[root@localhost autoconf-2.69]# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
configure: autobuild project... GNU Autoconf
configure: autobuild revision... 2.69
configure: autobuild hostname... bogon
configure: autobuild timestamp... 20170115T064711Z
checking whether /bin/sh -n is known to work... yes
checking for characters that cannot appear in file names... none
checking whether directories can have trailing spaces... yes
checking for expr... /usr/bin/expr
checking for GNU M4 that supports accurate traces... /usr/local/bin/m4
checking whether /usr/local/bin/m4 accepts --gnu... yes
checking how m4 supports trace files... --debugfile
checking for perl... no
configure: error: perl is not found
发现了新的错误:
checking for perl... no
configure: error: perl is not found
是因为没有安装perl
可以快速yum安装,也可以向下看源码安装
[root@localhost ~]# yum -y install perl perl-devel
下载地址:https://www.perl.org/get.html
源码包地址:http://www.cpan.org/src/5.0/perl-5.24.0.tar.gz
[root@localhost ~]# tar -zxvf perl-5.24.0.tar.gz
[root@localhost ~]# cd perl-5.24.0
[root@localhost perl-5.24.0]# ./Configure
[root@localhost perl-5.24.0]# make
[root@localhost perl-5.24.0]# make install
perl安装完成之后再次执行再次切换到autoconf目录继续安装
[root@localhost ~]# cd autoconf-2.69
[root@localhost autoconf-2.69]# ./configure
[root@localhost autoconf-2.69]# make
[root@localhost autoconf-2.69]# make install
至此autoconf通过源码方式安装完成
2、mysql未卸载干净导致安装失败
使用以下命令查看当前安装mysql情况,查找以前是否装有mysql
rpm -qa|grep -i mysql
可以看到如下图的所示:
image.png
显示之前安装了:
MySQL-client-5.5.25a-1.rhel5
MySQL-server-5.5.25a-1.rhel5
停止mysql服务、删除之前安装的mysql
删除命令:rpm -e –nodeps 包名
rpm -ev MySQL-client-5.5.25a-1.rhel5
rpm -ev MySQL-server-5.5.25a-1.rhel5
如果提示依赖包错误,则使用以下命令尝试:
rpm -ev MySQL-client-5.5.25a-1.rhel5 --nodeps
如果提示错误:error: %preun(xxxxxx) scriptlet failed, exit status 1
则用以下命令尝试:
rpm -e --noscripts MySQL-client-5.5.25a-1.rhel5
查找之前老版本mysql的目录、并且删除老版本mysql的文件和库
find / -name mysql
查找结果如下:
find / -name mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/lib64/mysql
删除对应的mysql目录
rm -rf /var/lib/mysql
rm -rf /var/lib/mysql
rm -rf /usr/lib64/mysql
具体的步骤如图:查找目录并删除
rm -rf /etc/my.cnf
再次查找机器是否安装mysql
rpm -qa|grep -i mysql
无结果,说明已经卸载彻底,接下来直接安装mysql即可。
3、缺少libaio依赖
[root@localhost ~]# rpm -qa|grep libaio
libaio-0.3.109-13.el7.x86_64
如果不存需要下载离线包:
http://mirror.centos.org/centos/6/os/x86_64/Packages/
安装libaio库:
rpm -ivh libaio-0.3.107-10.el6.x86_64.rpm(如果可以连接外网可执行yum install libaio)
本文参考以下资料:
https://blog.51cto.com/liuzhenlife/1892070
https://www.cnblogs.com/javahr/p/9245443.html
https://www.cnblogs.com/panzhaohui/p/7169905.html
https://blog.csdn.net/sld880311/article/details/78776563