Part1:写在最前
MYSQL5.7.15是截止至本文撰写当日,mysql官网的最新社区版,mysql5.7的多项功能优化可以用激动人心来形容,嫌安装麻烦?没关系,跟着本文,带你1分钟搞定MySQL5.7.15数据库安装部署。
Part2:仅仅安装就够了?
不,当然不够,MySQL5.7的多项功能特性更新,无法一一赘述,因此,我们先从和本文最相关的my.cnf,来解读一些MySQL5.7的部分新特性。
在之前我写过一篇MySQL5.6的新特性参数,诸如:
innodb_buffer_pool_dump_at_shutdown=1 它dump的不是数据,是Id号
innodb_buffer_pool_load_at_startup=1
开启这个两个参数当数据库重启后把这些热数据重新加载回去
只有正常关库才会dump热数据块,宕机和kill -9不会
更多5.6安装内容可移步:
http://suifu.blog.51cto.com/9167728/1846671
在5.7里,一个新的系统参数:innodb_buffer_pool_dump_pct,其默认值为25
允许DBA控制每个缓冲池最近使用页的百分比来导出,以减缓导出innodb buffer pool所有页占用过大的磁盘。如果innodb buffer pool里缓存了100个页,那么会将最近使用的25个页导出去,最大值是100,意思为全部导出,如下图所示:
注:在MariaDB10.1里,同样提供了该参数,不过其默认值是100
在线调整innodb_buffer_pool_size,不用重启mysql进程
在MySQL5.6/MariaDB 10.1版本,调整该参数必须重启MySQL进程才可以生效,而mysql5.7版本里,可以直接动态设置,非常方便。
这个功能应用场景如:
1.机器新增内存,DBA忘记调大该参数;
2.工作交接,新来的DBA发现前任DBA对该参数设置不合理。
下面在我机器里进行测试,把512M内存变更为256M,见下图:
需注意的是,在调整buffer_pool期间,用户的请求会被阻塞,直到调整完毕,所以请勿在高峰期调整。
Part3:整个流程分3步
1.mysql官网下载二进制版本的mysql5.7.15安装包(mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz)至/root目录
2.执行下文所述的mysql_auto_install.sh脚本
3.输入您设置的密码登录数据库
Part4:自动化脚本
[root@HE2 ~]# cat mysql_auto_install.sh###### 二进制自动安装数据库脚本root密码MANAGER将脚本和安装包放在/root目录即可#####################数据库目录/usr/local/mysql##################数据目录/data/mysql##################慢日志目录/data/slowlog##################端口号默认3306其余参数按需自行修改############ ###################author:rrhelei@126.com####################!/bin/bash # Check if user is rootif [ $(id -u) != "0" ]; then echo "Error: You must be root to run this script, please use root to install" exit 1fi clearecho "========================================================================="echo "A tool to auto-compile & install MySQL 5.7.15 on Redhat/CentOS Linux "echo "========================================================================="cur_dir=$(pwd) #set mysql root password echo "===========================" mysqlrootpwd="MANAGER" echo -e "Please input the root password of mysql:" read -p "(Default password: MANAGER):" mysqlrootpwd if [ "$mysqlrootpwd" = "" ]; then mysqlrootpwd="MANAGER" fi echo "===========================" echo "MySQL root password:$mysqlrootpwd" echo "===========================" #which MySQL Version do you want to install?echo "===========================" isinstallmysql57="n" echo "Install MySQL 5.7.15,Please input y" read -p "(Please input y , n):" isinstallmysql57 case "$isinstallmysql57" in y|Y|Yes|YES|yes|yES|yEs|YeS|yeS) echo "You will install MySQL 5.7.15" isinstallmysql57="y" ;; *) echo "INPUT error,You will exit install MySQL 5.7.15" isinstallmysql57="n" exit esac get_char() { SAVEDSTTY=`stty -g` stty -echo stty cbreak #dd if=/dev/tty bs=1 count=1 2> /dev/null stty -raw stty echo stty $SAVEDSTTY } echo "" echo "Press any key to start...or Press Ctrl+c to cancel" char=`get_char` # Initialize the installation related content.function InitInstall(){ cat /etc/issue uname -a MemTotal=`free -m | grep Mem | awk ‘{print $2}‘` echo -e "\n Memory is: ${MemTotal} MB " #Set timezone #rm -rf /etc/localtime #ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #Delete Old Mysql program rpm -qa|grep mysql rpm -e mysql #Disable SeLinux if [ -s /etc/selinux/config ]; then sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/g‘ /etc/selinux/config fi setenforce 0 } #Installation of depend on and optimization options.function InstallDependsAndOpt(){cd $cur_dir cat >>/etc/security/limits.conf<<EOF* soft nproc 65535* hard nproc 65535* soft nofile 65535* hard nofile 65535EOF echo "fs.file-max=65535" >> /etc/sysctl.conf} #Install MySQLfunction InstallMySQL57(){echo "============================Install MySQL 5.7.15=================================="cd $cur_dir #Backup old my.cnf#rm -f /etc/my.cnfif [ -s /etc/my.cnf ]; then mv /etc/my.cnf /etc/my.cnf.`date +%Y%m%d%H%M%S`.bakfi echo "============================MySQL 5.7.15 installing…………========================="#mysql directory configurationtar xvf /root/mysql-5.7.15-linux-glibc2.5-x86_64.tar.gzmv /root/mysql-5.7.15-linux-glibc2.5-x86_64 /usr/local/mysqlgroupadd mysql -g 512useradd -u 512 -g mysql -s /sbin/nologin -d /home/mysql mysqlmkdir -p /data/mysqlmkdir -p /data/slowlogchown -R mysql:mysql /data/mysqlchown -R mysql:mysql /usr/local/mysqlchown -R mysql:mysql /data/slowlog #edit /etc/my.cnfSERVERID=`ifconfig eth0 | grep "inet addr" | awk ‘{ print $2}‘| awk -F. ‘{ print $3$4}‘`cat >>/etc/my.cnf<<EOF[client]port=3306socket=/tmp/mysql.sockdefault-character-set=utf8 [mysql]no-auto-rehashdefault-character-set=utf8 [mysqld]port=3306character-set-server=utf8socket=/tmp/mysql.sockbasedir=/usr/local/mysqldatadir=/data/mysqlexplicit_defaults_for_timestamp=truelower_case_table_names=1back_log=103max_connections=3000max_connect_errors=100000table_open_cache=512external-locking=FALSEmax_allowed_packet=32Msort_buffer_size=2Mjoin_buffer_size=2Mthread_cache_size=51query_cache_size=32M#query_cache_limit=4Mtransaction_isolation=REPEATABLE-READtmp_table_size=96Mmax_heap_table_size=96M ###***slowqueryparameterslong_query_time=1slow_query_log = 1slow_query_log_file=/data/slowlog/slow.log ###***binlogparameterslog-bin=mysql-binbinlog_cache_size=4Mmax_binlog_cache_size=8Mmax_binlog_size=1024Mbinlog_format=MIXEDexpire_logs_days=7 ###***relay-logparameters#relay-log=/data/3307/relay-bin#relay-log-info-file=/data/3307/relay-log.info#master-info-repository=table#relay-log-info-repository=table#relay-log-recovery=1 #***MyISAMparameterskey_buffer_size=16Mread_buffer_size=1Mread_rnd_buffer_size=16Mbulk_insert_buffer_size=1M #skip-name-resolve ###***master-slavereplicationparametersserver-id=$SERVERID#slave-skip-errors=all #***Innodbstorageengineparametersinnodb_buffer_pool_size=512Minnodb_data_file_path=ibdata1:10M:autoextend#innodb_file_io_threads=8innodb_thread_concurrency=16innodb_flush_log_at_trx_commit=1innodb_log_buffer_size=16Minnodb_log_file_size=512Minnodb_log_files_in_group=2innodb_max_dirty_pages_pct=75innodb_buffer_pool_dump_pct=75innodb_lock_wait_timeout=50innodb_file_per_table=on [mysqldump]quickmax_allowed_packet=32M [myisamchk]key_buffer=16Msort_buffer_size=16Mread_buffer=8Mwrite_buffer=8M [mysqld_safe]open-files-limit=8192log-error=/data/mysql/error.logpid-file=/data/mysql/mysqld.pid EOF /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql --initialize-insecure cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqldchmod 700 /etc/init.d/mysqldchkconfig --add mysqldchkconfig --level 2345 mysqld on cat >> /etc/ld.so.conf.d/mysql-x86_64.conf<<EOF/usr/local/mysql/libEOFldconfig if [ -d "/proc/vz" ];thenulimit -s unlimitedfi /etc/init.d/mysqld start cat >> /etc/profile <<EOFexport PATH=$PATH:/usr/local/mysql/binexport LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mysql/libEOF /usr/local/mysql/bin/mysqladmin -u root password $mysqlrootpwd cat > /tmp/mysql_sec_script<<EOFuse mysql;delete from mysql.user where user!=‘root‘ or host!=‘localhost‘;grant all privileges on *.* to ‘sys_admin‘@‘%‘ identified by ‘MANAGER‘;flush privileges;EOF /usr/local/mysql/bin/mysql -u root -p$mysqlrootpwd -h localhost < /tmp/mysql_sec_script #rm -f /tmp/mysql_sec_script #/etc/init.d/mysqld restart echo "============================MySQL 5.7.15 install completed========================="} function CheckInstall(){echo "===================================== Check install ==================================="clearismysql=""echo "Checking..." if [ -s /usr/local/mysql/bin/mysql ] && [ -s /usr/local/mysql/bin/mysqld_safe ] && [ -s /etc/my.cnf ]; then echo "MySQL: OK" ismysql="ok" else echo "Error: /usr/local/mysql not found!!!MySQL install failed."fi if [ "$ismysql" = "ok" ]; thenecho "Install MySQL 5.7.15 completed! enjoy it."echo "========================================================================="netstat -ntlelseecho "Sorry,Failed to install MySQL!"echo "You can tail /root/mysql-install.log from your server."fi} #The installation logInitInstall 2>&1 | tee /root/mysql-install.logInstallDependsAndOpt 2>&1 | tee -a /root/mysql-install.logInstallMySQL57 > /dev/nullCheckInstall 2>&1 | tee -a /root/mysql-install.log
Part5:登录&享受吧
输入用户名密码(默认为MANAGER)登录数据库
至此,MySQL5.7.15安装完成
——总结——
MySQL5.7拥有诸多让我们激动和期待的新功能,限于文章篇幅原因,这里仅仅介绍冰山一角。由于笔者的水平有限,编写时间也很仓促,文中难免会出现一些错误或者不准确的地方,不妥之处恳请读者批评指正。
本文出自 “岁伏” 博客,请务必保留此出处http://suifu.blog.51cto.com/9167728/1855415