【0.1】mysql版本升级(5.6升级到5.7),mysql5.6安装
【1】升级操作(5.6升级到5.7)
必看参考:http://www.wjhsh.net/houzhiheng-p-14395016.html
【1.1】停止mysql 5.6
【1.2】修改 basedir 为 mysql5.7目录
把环境变量引用到Mysql 5.7 二进制文件目录,basedir 也放到mysql5.7的目录,以便引用新lib、插件等信息
参考下图做法;
用软链接来构造mysql 的 basedir,以及环境变量,最终切换一下软链接指向的文件夹到新版本文件夹即可;非常好用
【1.3】 mysql_upgrade -uroot -p -s --force 升级
起服,然后在linux环境下使用,mysql_upgrade -uroot -p -s --force 升级 (-s参数是只升级系统表,不加该参数使用该命令其实是把5.6的所有表数据以5.7的方式全部重建一次,需要耗时很久)
--停止mysql /etc/init.d/mysqld stop --解除5.6链接 [root@mysql02 local]# unlink mysql [root@mysql02 local]# ll 总用量 951200 drwxr-xr-x. 2 root root 4096 9月 23 2011 bin drwxr-xr-x. 2 root root 4096 9月 23 2011 etc drwxr-xr-x. 2 root root 4096 9月 23 2011 games drwxr-xr-x. 2 root root 4096 9月 23 2011 include drwxr-xr-x. 2 root root 4096 9月 23 2011 lib drwxr-xr-x. 2 root root 4096 9月 23 2011 lib64 drwxr-xr-x. 2 root root 4096 9月 23 2011 libexec drwxr-xr-x. 12 root mysql 4096 7月 14 18:14 mysql-5.6.44-linux-glibc2.12-x86_64 -rw-r--r--. 1 root root 329105487 6月 9 21:55 mysql-5.6.44-linux-glibc2.12-x86_64.tar.gz -rw-r--r--. 1 root root 644869837 5月 19 22:59 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz drwxr-xr-x. 2 root root 4096 9月 23 2011 sbin drwxr-xr-x. 5 root root 4096 7月 9 06:03 share drwxr-xr-x. 2 root root 4096 9月 23 2011 src --创建5.7的链接 [root@mysql02 local]# ln -s mysql-5.7.26-linux-glibc2.12-x86_64 mysql [root@mysql02 local]# ll 总用量 951204 drwxr-xr-x. 2 root root 4096 9月 23 2011 bin drwxr-xr-x. 2 root root 4096 9月 23 2011 etc drwxr-xr-x. 2 root root 4096 9月 23 2011 games drwxr-xr-x. 2 root root 4096 9月 23 2011 include drwxr-xr-x. 2 root root 4096 9月 23 2011 lib drwxr-xr-x. 2 root root 4096 9月 23 2011 lib64 drwxr-xr-x. 2 root root 4096 9月 23 2011 libexec lrwxrwxrwx. 1 root root 35 7月 17 21:46 mysql -> mysql-5.7.26-linux-glibc2.12-x86_64 drwxr-xr-x. 12 root mysql 4096 7月 14 18:14 mysql-5.6.44-linux-glibc2.12-x86_64 -rw-r--r--. 1 root root 329105487 6月 9 21:55 mysql-5.6.44-linux-glibc2.12-x86_64.tar.gz drwxr-xr-x. 9 root root 4096 7月 17 21:46 mysql-5.7.26-linux-glibc2.12-x86_64 -rw-r--r--. 1 root root 644869837 5月 19 22:59 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz drwxr-xr-x. 2 root root 4096 9月 23 2011 sbin drwxr-xr-x. 5 root root 4096 7月 9 06:03 share drwxr-xr-x. 2 root root 4096 9月 23 2011 src --升级 [root@mysql02 bin]# mysql_upgrade -u root -p Enter password: Checking if update is needed. Checking server version. Running queries to upgrade MySQL server. Checking system database. mysql.columns_priv OK mysql.db OK mysql.engine_cost OK mysql.event OK mysql.func OK mysql.general_log OK mysql.gtid_executed OK mysql.help_category OK --登陆 [root@mysql02 bin]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.26-log MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. --在生产环境当中只需要升级系统表即可 mysql_upgrade -u root -p -s --force
【mysql5.7小版本升级】
升级到 5.7.39
升级流程,先升级从实例,后升级主实例。
1、停止mysql,如是slave应先停止slave.
shell> /opt/mysql/base/5.7.33/bin/mysql -uadmin -p -h 127.0.0.1 -P 3306
mysql> stop slave;
mysql> set global innodb_max_dirty_pages_pct = 0;
mysql> set global innodb_fast_shutdown=0;
mysql> shutdown;
2、解压mysql5.7.39
shell> cd /tmp
shell> tar zxvf mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
shell> mv mysql-5.7.39-linux-glibc2.12-x86_64 /opt/mysql/base/5.7.39
shell> chown -R mysql.mysql /opt/mysql/base/5.7.39
shell> unlink /usr/local/mysql
shell> ln -s /opt/mysql/base/5.7.39 /usr/local/mysql
3、修改my.cnf
shell> vim /database/mysql/etc/3306/my.cnf
修改
basedir =/opt/mysql/base/5.7.39
4、修改 /lib/systemd/system/mysql3306.service
shell> vim /lib/systemd/system/mysql3306.service
修改mysqld路径
ExecStart=/opt/mysql/base/5.7.39/bin/mysqld --defaults-file=/database/mysql//etc/3306/my.cnf
及修改环境变量 /etc/profile
5、启动mysqld,主库需要加skip-netowrk避免升级过程中业务数据写入。
shell> systemctl daemon-reload
shell> systemctl start mysql3306.service
/opt/mysql/base/5.7.39/bin/mysqld --defaults-file=/database/mysql//etc/3306/my.cnf --skip-networking &
6、执行升级
/opt/mysql/base/5.7.39/bin/mysql_upgrade -uadmin -p -S /database/mysql/data/3306/mysqld.sock
/opt/mysql/base/5.7.39/bin/mysqladmin -uadmin -p -S /database/mysql/data/3306/mysqld.sock shutdown
7、重启数据库
mysql> systemctl start mysql3306.service
8、检查从库
mysql> set global super_read_only=on;
mysql> show slave status \G
二、mysql5.6、5.7 安装
【2.1】下载与解压
mysql官网下载过来,这里就不具体描述了(不会的参考:如何下载数据库?)
这里5.6和5.7的二进制包全部下载好了,并且解压好了。这里我的目录是 /mysql/app/
【2.2】对解压好的5.6文件夹做软连接
ln -s mysql-5.6.45-linux-glibc2.12-x86_64 mysql
【2.3】准备好my.cnf文件(/mysql/data/3306/my.cnf)
[mysqld] server-id = 3306 port = 3306 user = mysql datadir = /mysql/data/3306/data
basedir = /mysql/app/mysql #这个如果不加,无法使用默认的support-files/mysql.server 文件启动,且配置文件必须放在4个读取路径下
#参数文件默认读取顺序:【1】/etc/my.cnf 【2】/etc/mysql/my.cnf 【3】/usr/local/mysql/my.cnf 【4】~/.my.cnf
【2.4】创建mysql用户,并且配置文件上的目录全部要新建好,并且授权给mysql
groupadd mysql useradd mysql -r -g mysql -s /sbin/false mkdir -p /mysql/data/3306/data
chown -R mysql:mysql /mysql
chomd 755 /mysql
【2.5】设置环境变量
echo 'export PATH=/mysql/app/mysql/bin:$PATH'>>/etc/profile source /etc/profile
【2.6】开始安装
#5.6 切换到 /mysql/app/mysql下
./scripts/mysql_install_db --user=mysql --defaults-file=/mysql/data/3306/my.cnf --basedir=/mysql/app/mysql
#5.7 初始化
mysqld --initialize --defaults-file=/mysql/data/3306/my.cnf --basedir=/mysql/app/mysql --user=mysql --console
【2.7】启动
mysqld_safe --defaluts-file=/mysql/data/3306/my.cnf --user=mysql
【2.8】登录
#5.6直接输入mysql即可,mysql5.6没有默认密码 mysql
#5.7 需要输入密码
#一般情况下,密码再错误日志里面 但是我们上面初始化的时候,加了参数 --console 这就会在屏幕上打印出来,找到密码复制即可
mysql -uroot -p -h127.0.0.1
【3】最佳实践=》自动化安装mysql5.6
#【0】check rpm -qa libaio rpm -qa mysql rpm -qa maria # 【1】os 优化 cat <<EOF >>/etc/security/limits.conf * soft nproc 65535 * hard nproc 65535 * soft nofile 65535 * hard nofile 65535 * soft stack 65535 * hard stack 65535 EOF cat <<EOF >>/etc/sysctl.conf net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.conf.default.disable_ipv6 = 1 net.ipv6.conf.lo.disable_ipv6 = 1 vm.swappiness = 0 vm.min_free_kbytes = 67584 vm.overcommit_memory = 2 vm.overcommit_ratio = 85 kernel.sysrq=1 net.nf_conntrack_max = 1048576 EOF grubby --update-kernel=ALL --args="numa=off" # 需要reboot sysctl -p #【2】目录构建 mkdir -p /data/{mysql,mysql_log} mkdir -p /data/mysql_log/{binlog,errorlog,relaylog,slowlog} #【3】构建账户 groupadd mysql useradd mysql -r -g mysql -s /sbin/false chown -R mysql:mysql /data/mysql* #【4】解压文件 cd /data/dba tar -zxf mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz if [ ! -d /usr/local/mysql ];then mv mysql-5.6.37-linux-glibc2.12-x86_64 /usr/local/mysql;else echo 'the dir is exists!';fi cd /usr/local/mysql echo 'export PATH=/usr/local/mysql/bin:$PATH'>>/etc/profile source /etc/profile chown -R mysql:mysql /usr/local/mysql #【5】my.cnf cat <<eof>/etc/my.cnf [client] port = 3306 socket = /data/mysql/mysql.sock [mysqld] user=mysql basedir = /usr/local/mysql datadir = /data/mysql socket = /data/mysql/mysql.sock pid-file = /data/mysql/mysql.pid tmpdir = /data/mysql slow_query_log_file = /data/mysql_log/slowlog/slow.log log_error = /data/mysql_log/errorlog/error.log log_bin = /data/mysql_log/binlog/mysql-bin relay-log = /data/mysql_log/relaylog/relay-bin lower_case_table_names=1 port = 3306 server_id = 10492401 character_set_server = utf8 skip_name_resolve = 1 max_connections = 8096 max_connect_errors = 100000 max_allowed_packet = 128M tmp_table_size = 32M sort_buffer_size=4M slow_query_log = 1 long_query_time = 1 #binlog gtid_mode = on enforce-gtid-consistency log_slave_updates sync_binlog = 0 max_binlog_size = 1G expire_logs_days = 10 binlog_format = row ##innodb transaction_isolation = REPEATABLE-READ innodb_flush_method=O_DIRECT innodb_buffer_pool_size = 18G innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 8M innodb_log_file_size = 2G innodb_flush_neighbors = 0 innodb_io_capacity = 3000 innodb_io_capacity_max = 6000 innodb_buffer_pool_load_at_startup = 1 innodb_buffer_pool_dump_at_shutdown = 1 eof #【6】初始化 chown -R mysql:mysql /etc/my.cnf cd /usr/local/mysql yum -y install numactl ./scripts/mysql_install_db --user=mysql --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql #【7】封装成服务启动 cp -r ./support-files/mysql.server /etc/init.d/mysqld #【8】启动 service mysqld start #【9】修改密码 new_passwd='123456' mysqladmin -uroot password '${new_passwd}' sed -i "/\[mysql\]/a\password=$new_passwd" /etc/my.cnf sed -i "/\[client\]/a\password=$new_passwd" /etc/my.cnf
MySQL 5.6升级5.7都有什么注意事项
一、升级方式
MySQL升级的方式一般来说有两种
1、通过inplace方式原地升级,升级系统表
2、通过新建实例,高版本作为低版本的从库进行滚动升级
MySQL5.7版本做了非常多的改变,升级5.6到5.7时需要考虑兼容性,避免升级到5.7之后因为种种参数设置不正确导致业务受影响,建议首先逐一查看release note
二、需要注意的参数及问题
1、sql_mode:MySQL 5.7采用严格模式,例如ONLY_FULL_GROUP_BY等
2、innodb_status_output_locks:MySQL 5.7支持将死锁信息打印到error log(其实这个参数MySQL 5.6就已支持)
3、innodb_page_cleaners:MySQL 5.7将脏页刷新线程从master线程独立出来了,对应参数为innodb_page_cleaners
4、innodb_strict_mode:控制CREATE TABLE, ALTER TABLE, CREATE INDEX, 和 OPTIMIZE TABLE的语法问题
5、show_compatibility_56=ON:控制show变量及状态信息输出,如果未开启show status 命令无法获取Slave_xxx 的状态
6、log_timestamps:控制error log/slow_log/genera log日志的显示时间,该参数可以设置为:UTC 和 SYSTEM,但是默认使用 UTC
7、disable_partition_engine_check:在表多的情况下可能导致启动非常慢
8、range_optimizer_max_mem_size:范围查询优化参数,这个参数限制范围查询优化使用的内存,默认8M
9、MySQL 5.7新增优化器选项derived_merge=on,可能导致SQL全表扫描,而在MySQL 5.6下可能表现为auto key
10、innodb_undo_directory && innodb_undo_logs:MySQL 5.7支持将undo从ibdata1独立出来(只支持实例初始化,不支持在线变更)
11、主从复制问题:MySQL5.7到小于5.6.22的复制存在bug(bug 74683)
12、SQL兼容性问题:SQL在MySQL 5.7和MySQL 5.6环境下结果可能不一致,因此建议获取线上SQL,在同样数据的环境下,在两个实例运行获取到的结果计算hash,比较hash值做兼容性判断
三、友情提醒
1、升级前一定要做好备份!!!
2、升级正式环境前提前在测试环境进行仔细测试,确认无误以后再升级正式环境
3、做好相应的回退方案