centos一键安装mysql_CentOS6.x/CentOS7.x一键安装MySQL5.6/5.7并定制数据目录

本文档详细介绍了如何在CentOS6.x和7.x上使用官方rpm包一键安装MySQL5.6/5.7,并定制数据目录到/data1。首先,介绍了系统环境和所需的安装包。接着,提供了下载和安装MySQL各个组件的步骤,包括删除默认的mysql-libs、安装libaio等。然后,展示了my.cnf配置文件的示例,用于自定义数据存储位置和优化设置。此外,还给出了一个安装脚本示例,该脚本简化了整个安装过程。最后,强调了安装过程中需要注意的问题,例如关闭selinux、初始化密码等。
摘要由CSDN通过智能技术生成

一、系统环境及说明

系统:CentOS6.x_x64  MySQL:社区版5.6.21,开源数据库用的最多的MySQL,编译安装比较繁琐,yum安装版本比较低且默认安装的位置是/var/下,本次采用官方下载的rpm包,通过脚本自动化安装并定制数据目录到独立分区中(本次为/data1目录)

另外补充部分是给出针对Ubuntu/CentOS7的实际配置;最后提供了脚本一键安装;注意的是glibc版本要大于2.12

二、准备安装包

1、官方下载

#https://dev.mysql.com/downloads/mysql/5.6.html#downloads 选择对应的版本和平台软件包:

MySQL-shared-compat-5.6.x-1.el6.x86_64.rpm    #x就是你下的版本号以下类同

MySQL-devel-5.6.x-1.el6.x86_64.rpm

MySQL-shared-5.6.x-1.el6.x86_64.rpm

MySQL-client-5.6.x-1.el6.x86_64.rpm

MySQL-test-5.6.x-1.el6.x86_64.rpm

MySQL-server-5.6.x-1.el6.x86_64.rpm

2、本实验用到的包

------------------------------------------分割线------------------------------------------

相关资料可从以下信息得到下载:

点击这个http://www.linuxidc.com/Linux/2013-12/93755.htm 链接关注 Linux公社官方微信,关注后回复数字151894。即可得到网友的分享密码。

如果取消关注Linux公社公众号,即使再次关注,也将无法提供本服务!

------------------------------------------分割线------------------------------------------

包含了以下软件包(也是从官方下载):

MySQL-shared-compat-5.6.21-1.el6.x86_64.rpm

MySQL-devel-5.6.21-1.el6.x86_64.rpm

MySQL-shared-5.6.21-1.el6.x86_64.rpm

MySQL-client-5.6.21-1.el6.x86_64.rpm

MySQL-test-5.6.21-1.el6.x86_64.rpm

MySQL-server-5.6.21-1.el6.x86_64.rpm

三、安装mysql5.6

1、如果是官方下载的请直接执行下面操作

#rpm -ivh MySQL-shared-compat-5.6.x-1.el6.x86_64.rpm      #提供基础依赖组件,需要第一个安装

#yum remove mysql-libs -y              #御载默认自带的mysql-lib

#yum install libaio -y                        #安装libiao

#rpm -ivh MySQL-devel-5.6.x-1.el6.x86_64.rpm

#rpm -ivh MySQL-shared-5.6.x-1.el6.x86_64.rpm

#rpm -ivh MySQL-client-5.6.x-1.el6.x86_64.rpm

#rpm -ivh MySQL-test-5.6.x-1.el6.x86_64.rpm

#rpm -ivh MySQL-server-5.6.x-1.el6.x86_64.rpm

说明:以上直接安装的数据目录在/var/lib/mysql下 但有时你并不希望 数据目录和/目录在一个分区(磁盘)上

因此可通过脚本重重新初始化安装到别的目录;完成自定制;

2、本实验脚本如下:

将以上rpm包放到一个目录,创建my.cnf文件(公供参考)

cat my.cnf

[mysqld]

# GENERAL #

user                          = mysql

default-storage-engine        = InnoDB

socket                        = /data1/mysqldb/data/mysql.sock

pid-file                      = /data1/mysqldb/data/mysql.pid

# MyISAM #

key-buffer-size                = 32M

myisam-recover                = FORCE,BACKUP

# SAFETY #

max-allowed-packet            = 16M

max-connect-errors            = 1000000

# DATA STORAGE #

datadir                        = /data1/mysqldb/data

# BINARY LOGGING #

log-bin                        = /data1/mysqldb/log/mysql-bin

expire-logs-days              = 14

sync-binlog                    = 1

# CACHES AND LIMITS #

tmp-table-size                = 32M

max-heap-table-size            = 32M

query-cache-type              = 0

query-cache-size              = 0

max-connections                = 500

thread-cache-size              = 50

open-files-limit              = 65535

table-definition-cache        = 1024

table-open-cache              = 2048

# INNODB #

innodb-flush-method            = O_DIRECT

innodb-log-files-in-group      = 2

innodb-log-file-size          = 64M

innodb-flush-log-at-trx-commit = 1

innodb-file-per-table          = 1

innodb-buffer-pool-size        = 256M

# LOGGING #

log-error                      = /data1/mysqldb/log/mysql-error.log

log-queries-not-using-indexes  = 0

slow-query-log                = 1

slow-query-log-file            = /data1/mysqldb/log/mysql-slow.log

注意:生产环境有些优化参数请按需修改;以上仅供参考;您也可以到https://tools.percona.com/  网站填写相关参数生成配置文件;

并在目录中创建一个install.sh脚本内容如下:

cat install.sh

#!/bin/bash

src_dir=$(pwd)

cd $src_dir

rpm -ivh MySQL-shared-compat-5.6.21-1.el6.x86_64.rpm

yum remove mysql-libs -y

yum install libaio -y

rpm -ivh MySQL-devel-5.6.21-1.el6.x86_64.rpm

rpm -ivh MySQL-shared-5.6.21-1.el6.x86_64.rpm

rpm -ivh MySQL-client-5.6.21-1.el6.x86_64.rpm

rpm -ivh MySQL-test-5.6.21-1.el6.x86_64.rpm

rpm -ivh MySQL-server-5.6.21-1.el6.x86_64.rpm

#service mysql start && echo "mysql5.6.21 has installes sucess!"

#mysql_root_pwd=`gawk -F : '{ print $4 }' /root/.mysql_secret`

#echo "A random root password has been set. You will find it in '/root/.mysql_secret'."

#echo "The random root password was:'${mysql_root_pwd// }'"

service mysql stop

[ -d /data1/mysqldb/data ] || mkdir -p /data1/mysqldb/data

[ -d /data1/mysqldb/log ] || mkdir -p /data1/mysqldb/log

chown mysql.mysql /data1/mysqldb -R

cd $src_dir

cp ${src_dir}/my.cnf /etc

cd /usr/bin

./mysql_install_db --user=mysql --basedir=/usr --datadir=/data1/mysqldb/data  #迁移数据目录到/data1/mysqldb/data下可自行定制

cd /var/lib

mv mysql /tmp

mkdir mysql

chown mysql.mysql mysql

service mysql start && echo "Mysql root password was empty.Please change when you login mysql."

ln -s /data1/mysqldb/data/mysql.sock  /var/lib/mysql/mysql.sock      #由于一些mysql工具默认到这里找mysql.sock所以做个软链

四、注意问题

1、关闭selinux否则脚本安装不成功

2、安装后系统自带的postfix和crontab工具被御了,重新yum安装 一次即可

3、mysql服务的root密码为空,请自行修改root密码

或在脚本中添加以下行 在mysql迁移好目录启动后自动修改mysql root密码

mysqladmin -u root password "123.com"

补充部分:

CentOS7.x安装 mysql 5.7.21

到mysql官方下载Linux通用包wget https://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz

解压:

tar -xvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz  -C /usr/local

ln -sv mysql-5.7.21-linux-glibc2.12-x86_64  mysql

添加环境变量

cat /etc/profile.d/mysql.sh

export PATH=/usr/local/mysql/bin:$PATH

初始化:

#useradd -r mysql

#mkdir -pv /data/mysql

#chown mysql.mysql /data/mysql

#mysqld --initialize-insecure  --datadir=/data/mysql --user=mysql --basedir=/usr/local/mysql/

#mkdir /usr/local/mysql/etc/my.cnf.d

#chown mysql.mysql -R /usr/local/mysql/etc

#cp /etc/my.cnf /usr/local/mysql/etc/

#cat /usr/local/mysql/etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/tmp/mysql.sock

[mysqld_safe]

log-error=/usr/local/mysql/log/error.log

pid-file=/var/run/mysql/mysql.pid

!includedir /usr/local/mysql/etc/my.cnf.d

复制启动脚本:

#cp /usr/local/mysql/suppor-files/mysql.server /etc/init.d/mysqld

#touch /usr/local/mysql/log/error.log

#chwon mysql.mysql /usr/local/mysql/log/error.log

#chmod 755 /usr/local/mysql/log/error.log

#chkconfig --add mysqld

#chkconfig mysqld on

#service mysqld start

Ubuntu 16.04上安装

下载同样的包

安装的步骤与上面CentOS7类似,不同的是ubuntu 16.04上不支持service 方式管理

因此需要复制

#cp /usr/local/mysql/suppor-files/mysql.server /etc/init.d/mysqld

#sudo systemctl daemon-reload

#chown root.mysql /usr/local/mysql -R

#chmod 775 /usr/local/mysql -R

#systemctl enable mysqld

#systemctl start mysqld

#systemctl status mysqld

如图:

69e43fb5d4d1a6ccdbe7bbcbaaa68270.png

mysql -uroot -p

如图:

8c065d6a33cd368dac0c0ad7174efb61.png

注意安装后mysql root密码为空;请自行设置 root密码;以下脚本一键安装亦是如此!

一键安装脚本

以上在CentOS7上安装和Ubuntu 16.04下安装大同小异,均可以安装成功;因此整理成一键安装脚本!

#cat auto_install_mysql.sh

#!/bin/bash

#version 2018-04-01 by san

setenforce 0

yum install wget -y

sourceDir=$(pwd)

installDir=/usr/local

mysqlData=/data1/mysqldb

mysqlBaseDir=/usr/local/mysql

mysqldb="mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz"

xtrabackup="percona-xtrabackup-24-2.4.8-1.el7.x86_64.rpm"

check_mariadb(){

if [ $(rpm -qa|grep mariadb|wc -l) -gt 1 ]

then

echo "Find mariadb installed!"

read -t 6 -p "default y|Y remove pause 6 seconds!n|N Cacle install!" yesNo

if [[ $yesNo == "n" || $yesNo == "N" ]]

then

echo "Cacle install mysqldb" && exit 0

else

yum -y remove mariadb mariadb-server

rm -rf /etc/my.cnf

fi

fi

}

check_mariadb

[ -f $sourceDir ]||mkdir $sourceDir -pv

[[ $(id mysql >/dev/null && echo $?) == 0 ]] && echo "mysql is exsits!" || useradd -r mysql

cd $sourceDir

[ -f ${sourceDir}/${mysqldb} ] || wget https://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz

tar -xvf  $mysqldb  -C $installDir

cd $installDir

ln -sv mysql-5.7.21-linux-glibc2.12-x86_64  mysql

[ -f /etc/profile.d/mysql.sh ]|| echo "export PATH=$mysqlBaseDir/bin:$PATH" >/etc/profile.d/mysql.sh

source /etc/profile.d/mysql.sh

#创建mysql数据目录

[ -d $mysqlData ]|| mkdir -pv $mysqlData

chown mysql.mysql $mysqlData

$mysqlBaseDir/bin/mysqld --initialize-insecure  --datadir=$mysqlData --user=mysql --basedir=$mysqlBaseDir/

[ -d $mysqlBaseDir/etc/my.cnf.d ] || mkdir -pv $mysqlBaseDir/etc/my.cnf.d

[ -d $mysqlBaseDir/log ] || mkdir -pv $mysqlBaseDir/log

echo '

[mysqld]

datadir=/data1/mysqldb

socket=/data1/mysqldb/mysql.sock

key_buffer_size        = 16M

max_allowed_packet      = 16M

thread_stack            = 192K

thread_cache_size      = 8

query_cache_limit      = 1M

query_cache_size        = 64M

query_cache_type        = 1

symbolic-links=0

#binlog

server-id              = 1

log_bin                = /data1/mysqldb/mysql-bin.log

#建议打开

innodb_file_per_table=ON

skip_name_resolve=ON

[mysqld_safe]

log-error=/usr/local/mysql/log/error.log

pid-file=/var/run/mysql/mysql.pid

!includedir /usr/local/mysql/etc/my.cnf.d

' >/$mysqlBaseDir/etc/my.cnf

touch /usr/local/mysql/log/error.log

chown mysql.mysql $mysqlBaseDir/* -R

chown mysql.mysql /usr/local/mysql/etc/my.cnf.d -R

#add manager script

cp -rp $mysqlBaseDir/support-files/mysql.server /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld

chkconfig --add mysqld

chkconfig mysqld on

systemctl daemon-reload

service mysqld start

ln -sv /data1/mysqldb/mysql.sock /tmp/mysql.sock

###

echo "mysql root password is empty"

echo 'install xtrabackup tool'

cd $sourceDir

[ -f ${sourceDir}/${xtrabackup} ] || wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.8/binary/RedHat/7/x86_64/percona-xtrabackup-24-2.4.8-1.el7.x86_64.rpm

yum install ./percona-xtrabackup-24-2.4.8-1.el7.x86_64.rpm -y

0b1331709591d260c1c78e86d0c51c18.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值