一、系统环境及说明
系统:CentOS6.x_x64 mysql:社区版5.6.21,开源数据库用的最多的mysql,编译安装比较繁琐,yum安装版本比较低且默认安装的位置是/var/下,本次采用官方下载的rpm包,通过脚本自动化安装并定制数据目录到独立分区中(本次为/data1目录)
另外补充部分是给出针对Ubuntu/CentOS7的实际配置;最后提供了脚本一键安装;注意的是glibc版本要大于2.12
二、准备安装包
1、官方下载
# 选择对应的版本和平台软件包:
MySQL-shared-compat-5.6.x-1.el6.x86_64.rpm #x就是你下的版本号以下类同
MySQL-devel-5.6.x-1.el6.x86_64.rpmMySQL-shared-5.6.x-1.el6.x86_64.rpmMySQL-client-5.6.x-1.el6.x86_64.rpmMySQL-test-5.6.x-1.el6.x86_64.rpmMySQL-server-5.6.x-1.el6.x86_64.rpm2、本实验用到的包
包含了以下软件包(也是从官方下载):
MySQL-shared-compat-5.6.21-1.el6.x86_64.rpm
MySQL-devel-5.6.21-1.el6.x86_64.rpmMySQL-shared-5.6.21-1.el6.x86_64.rpmMySQL-client-5.6.21-1.el6.x86_64.rpmMySQL-test-5.6.21-1.el6.x86_64.rpmMySQL-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 = mysqldefault-storage-engine = InnoDBsocket = /data1/mysqldb/data/mysql.sockpid-file = /data1/mysqldb/data/mysql.pid# MyISAM #key-buffer-size = 32Mmyisam-recover = FORCE,BACKUP# SAFETY #max-allowed-packet = 16Mmax-connect-errors = 1000000# DATA STORAGE #datadir = /data1/mysqldb/data# BINARY LOGGING #log-bin = /data1/mysqldb/log/mysql-binexpire-logs-days = 14sync-binlog = 1# CACHES AND LIMITS #tmp-table-size = 32Mmax-heap-table-size = 32Mquery-cache-type = 0query-cache-size = 0max-connections = 500thread-cache-size = 50open-files-limit = 65535table-definition-cache = 1024table-open-cache = 2048# INNODB #innodb-flush-method = O_DIRECTinnodb-log-files-in-group = 2innodb-log-file-size = 64Minnodb-flush-log-at-trx-commit = 1innodb-file-per-table = 1innodb-buffer-pool-size = 256M# LOGGING #log-error = /data1/mysqldb/log/mysql-error.loglog-queries-not-using-indexes = 0slow-query-log = 1slow-query-log-file = /data1/mysqldb/log/mysql-slow.log
注意:生产环境有些优化参数请按需修改;以上仅供参考;您也可以到 网站填写相关参数生成配置文件;
并在目录中创建一个install.sh脚本内容如下:
cat install.sh
#!/bin/bashsrc_dir=$(pwd)cd $src_dirrpm -ivh MySQL-shared-compat-5.6.21-1.el6.x86_64.rpmyum remove mysql-libs -yyum install libaio -yrpm -ivh MySQL-devel-5.6.21-1.el6.x86_64.rpmrpm -ivh MySQL-shared-5.6.21-1.el6.x86_64.rpmrpm -ivh MySQL-client-5.6.21-1.el6.x86_64.rpmrpm -ivh MySQL-test-5.6.21-1.el6.x86_64.rpmrpm -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/logchown mysql.mysql /data1/mysqldb -Rcd $src_dircp ${src_dir}/my.cnf /etccd /usr/bin./mysql_install_db --user=mysql --basedir=/usr --datadir=/data1/mysqldb/data #迁移数据目录到/data1/mysqldb/data下可自行定制cd /var/libmv mysql /tmpmkdir mysqlchown mysql.mysql mysqlservice 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
解压:
#yum install libaio1 numactl -y#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.shexport 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/mysqlsocket=/tmp/mysql.sock[mysqld_safe]log-error=/usr/local/mysql/log/error.logpid-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 方式管理
因此需要复制
#apt-get install libaio1 numactl ## 安装依赖包#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
如图:
mysql -uroot -p
如图:
注意安装后mysql root密码为空;请自行设置 root密码;以下脚本一键安装亦是如此!
一键安装脚本
以上在CentOS7上安装和Ubuntu 16.04下安装大同小异,均可以安装成功;因此整理成一键安装脚本!
#cat auto_install_mysql.sh
#!/bin/bash#version 2018-04-01 by sansetenforce 0yum install wget -ysourceDir=$(pwd)installDir=/usr/localmysqlData=/data1/mysqldbmysqlBaseDir=/usr/local/mysqlmysqldb="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 fifi}check_mariadb[ -f $sourceDir ]||mkdir $sourceDir -pv[[ $(id mysql >/dev/null && echo $?) == 0 ]] && echo "mysql is exsits!" || useradd -r mysqlcd $sourceDir[ -f ${sourceDir}/${mysqldb} ] || wget https://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gztar -xvf $mysqldb -C $installDircd $installDirln -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.shsource /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/logecho '[mysqld]datadir=/data1/mysqldbsocket=/data1/mysqldb/mysql.sockkey_buffer_size = 16Mmax_allowed_packet = 16Mthread_stack = 192Kthread_cache_size = 8query_cache_limit = 1Mquery_cache_size = 64Mquery_cache_type = 1symbolic-links=0#binlogserver-id = 1log_bin = /data1/mysqldb/mysql-bin.log#建议打开innodb_file_per_table=ONskip_name_resolve=ON[mysqld_safe]log-error=/usr/local/mysql/log/error.logpid-file=/var/run/mysql/mysql.pid!includedir /usr/local/mysql/etc/my.cnf.d' >/$mysqlBaseDir/etc/my.cnftouch /usr/local/mysql/log/error.logchown mysql.mysql $mysqlBaseDir/* -Rchown mysql.mysql /usr/local/mysql/etc/my.cnf.d -R#add manager scriptcp -rp $mysqlBaseDir/support-files/mysql.server /etc/init.d/mysqldchmod +x /etc/init.d/mysqldchkconfig --add mysqldchkconfig mysqld onsystemctl daemon-reloadservice mysqld startln -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.rpmyum install ./percona-xtrabackup-24-2.4.8-1.el7.x86_64.rpm -y
补充:
python pip install MySQL-python 时报错
mysql_config 找不到问题
# ln -s /usr/local/mysql/bin/mysql_config /usr/bin/mysql_config
以及error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
ubuntu 执行如下命令
sudo apt-get install python-dev \
build-essential libssl-dev libffi-dev \
libxml2-dev libxslt1-dev zlib1g-dev \