码迷,mamicode.com
首页 > 数据库 > 详细

MySQL升级指南

时间:2017-12-06 17:47:16      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:load   read   x86_64   ldo   登录   调整   ash   hang   mysql5.7   

I、MySQL跨版本升级


一、MySQL升级说明

  • 1、升级到 5.7 流程:

    • 前端业务停服,备份主库和从库数据;先升级从库导入(从库权限+主库备份),后升级主库(主库权限+主库备份),重建主从同步,前端业务开服

二、升级环境说明

  • 1、升级环境

    名称 升级前 升级后 备注
    OS 版本 RHEL 7.3 x86_64 RHEL 7.3 x86_64
    MySQL 版本 5.5.33-log 5.7.18-log Linux Generic Binaries
    MySQL 安装目录 /usr/local/mysql5.5 /usr/local/mysql5.7.18
    MySQL Basedir /usr/local/mysql /usr/local/mysql 通过链接的方式
    MySQL Datadir /r2/mysqldata /r2/mysqldata
    MySQL Port 3306 3306
    my.cnf 路径 /etc/my.cnf /etc/my.cnf
  • 2、安装包下载地址

三、停止应用服务和 MySQL 5.5 实例并备份数据库

  • 1、停止应用服务

    • 前端业务停止服务,确认没有MySQL线程连接(show processlist查看)
  • 2、数据库备份说明

    • 2.1 、分别导出主库和从库用户权限
    • 2.2 、通过mysqldump对主库和从库进行备份,备份时数据库需要正常运行。
    • 2.3 、备份命令 :mysqldump -uroot -p123456 --single-transaction --master-data=2 -E -R --ignore-table=mysql.* --flush-logs -A >/r2/bak_sql/all_20171201.sql
  • 3、备份数据库并关闭MySQL 5.5实例,主库和从库都需要操作步骤(3.1~3.4)

    • 3.1、导出权限并备份数据库
    
    --导出权限
    
    [root@localhost-m(252) /usr/local/src]# cat exp_grants.sh   
    #!/bin/bash
    #Function export user privileges`:
    
    myuser=root      #mysql账号
    mypwd=123456     #mysql密码,导出完请清除掉脚本账号信息
    
    expgrants()
    {
      mysql -B -u${myuser} -p${mypwd} -N $@ -e "SELECT CONCAT(
        ‘SHOW GRANTS FOR ‘‘‘, user, ‘‘‘@‘‘‘, host, ‘‘‘;‘
        ) AS query FROM mysql.user" | \
      mysql -u${myuser} -p${mypwd} $@ | \
      sed ‘s/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/-- \1 /;/--/{x;p;x;}‘
    }
    expgrants > ./grants.sql
    
    [root@localhost-m(252) /usr/local/src]# sh exp_grants.sh
    
    [root@localhost-m(252) /usr/local/src]# less grants.sql   --导出的权限文件
    -- Grants for dbbackup@%
    GRANT SELECT, CREATE, RELOAD, PROCESS, SHOW DATABASES, SUPER, LOCK TABLES, REPLICATION CLIENT, SHOW VIEW, EVENT, TRIGGER, CREATE TABLESPACE ON *.* TO ‘dbbackup‘@‘%‘ IDENTIFIED BY PASSWORD ‘*xxxxxxxxxxxxxxx‘;
    .......
    • 3.2、mysqldump备份数据库
    
    [root@localhost-m(252) /usr/local/src]# mysqldump -uroot -p123456 --single-transaction  --master-data=2   -E -R   --ignore-table=mysql.*  --flush-logs -A  >/r2/bak_sql/all_20171201.sql
    
    [root@localhost-m(252) /usr/local/src]# cd /r2/bak_sql/
    
    [root@localhost-m(252) /r2/bak_sql]# ls -lsh
    total 102M
    102M -rw-r--r-- 1 root root 102M 12月  1 17:16 all_20171201.sql
    • 3.3、关闭 MySQL 5.5 实例
    [root@localhost-m(252) /r2/bak_sql]# /etc/init.d/mysqld stop
    Shutting down MySQL.. SUCCESS!
    
    [root@localhost-m(252) /r2/bak_sql]# ps -ef |grep mysqld
    root     105281  82231  0 18:19 pts/4    00:00:00 grep --color=auto mysqld
    • 3.4、通过物理备份数据库(升级失败回退时使用
    -- 在实例关闭之后,通过 cp datadir 目录的方式来备份。
    [root@localhost-m(252) /r2/bak_sql]# cd /r2/
    
    [root@localhost-m(252) /r2]# mv mysqldata mysqldatabak

四、升级MySQL版本

  • 1、安装 MySQL 5.7

    [root@localhost-m(252) /usr/local/src]# pwd
    /usr/local/src
    
    [root@localhost-m(252) /usr/local/src]# ll |grep mysql
    -rw-r--r--  1 root  root  654430368 12月  1 16:05 mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
    drwxr-xr-x 14 mysql mysql      4096 7月  31 17:03 zabbix-3.2.7
    
    [root@localhost-m(252) /usr/local/src]# mv  /usr/local/mysql  /usr/local/mysql5.5
    
    [root@localhost-m(252) /usr/local/src]# md5sum  mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz  --校验tar包MD5与官网MD5对比是否正确
    ebc8cbdaa9c356255ef82bd989b07cfb  mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
    
    
    [root@localhost-m(252) /usr/local/src]# tar zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
    mysql-5.7.18-linux-glibc2.5-x86_64/bin/myisam_ftdump
    mysql-5.7.18-linux-glibc2.5-x86_64/bin/myisamchk
    ......
    mysql-5.7.18-linux-glibc2.5-x86_64/docs/INFO_SRC
    
    [root@localhost-m(252) /usr/local]# ll |grep mysql
    drwxr-xr-x. 13 root   root    4096 3月   9  2016 mysql5.5
    drwxr-xr-x   9 root   root    4096 12月  4 09:18 mysql-5.7.18-linux-glibc2.5-x86_64
    
    [root@localhost-m(252) /usr/local]# ln -s mysql-5.7.18-linux-glibc2.5-x86_64 mysql
    
    [root@localhost-m(252) /usr/local]# ll |grep mysql
    lrwxrwxrwx   1 root   root      34 12月  4 09:20 mysql -> mysql-5.7.18-linux-glibc2.5-x86_64
    drwxr-xr-x. 13 root   root    4096 3月   9  2016 mysql5.5
    drwxr-xr-x   9 root   root    4096 12月  4 09:18 mysql-5.7.18-linux-glibc2.5-x86_64
  • 2、修改相关目录的权限

    [root@localhost-m(252) /usr/local]# chown -R mysql.mysql mysql
    
    [root@localhost-m(252) /usr/local]# chown -R mysql.mysql mysql-5.7.18-linux-glibc2.5-x86_64
  • 3、检查改环境变量

    [root@localhost-m(252) /r2/mysqldata]# which mysql
    /usr/local/mysql/bin/mysql
    
    --如何没有,在/etc/profile 文件中的添加export PATH=/usr/local/mysql/bin:$PATH
    使用 source /etc/profile 刷新配置
    [root@localhost-m(252) /r2/mysqldata]# source /etc/profile
    
    [root@localhost-m(252) /r2/mysqldata]# which mysql
    /usr/local/mysql/bin/mysql

五、修改 my.cnf 配置文件并启动 MySQL 5.7

  • 1、修改 my.cnf 配置文件

    • 1.1、备份 my.cnf 配置文件
    [root@localhost-m(252) /usr/local]# cp /etc/my.cnf /r2/mysqldatabak/my_5.5.33.cnf     --备份配置文件
    • 1.2、参照模板修改my.cnf配置文件
      • 模板配置文件如下:
    # line:        V1.4
    # mail:         gczheng@139.com    
    # data:         2017-12-04
    # file_name:    my.cnf
    
    [client]
    port    = 3306
    socket  = /r2/mysqldata/mysql.sock
    #=======================================================================
    # # MySQL客户端配置
    #=======================================================================
    [mysql]
    prompt="(\u@\h) \\R:\\m:\\s [\d]> "
    no-auto-rehash
    #=======================================================================
    # MySQL服务器全局配置
    #=======================================================================
    [mysqld]
    user = mysql
    port = 3306
    server-id = 48168
    tmpdir = /r2/mysqldata
    datadir = /r2/mysqldata
    socket  = /r2/mysqldata/mysql.sock
    wait_timeout = 31536000
    #interactive_timeout = 600
    sql_mode =
    #sql_mode 配置为空值
    skip_name_resolve = 1
    lower_case_table_names = 0
    character-set-server = utf8
    #auto_increment_increment = 1
    #auto_increment_offset = 1
    log_timestamps = SYSTEM
    init_connect=‘SET NAMES utf8‘
    ######################### 性能参数 ####################
    open_files_limit = 1024000
    max_connections = 10000
    max_user_connections=9990
    max_connect_errors = 100000
    table_open_cache = 1024
    max_allowed_packet = 128M
    thread_cache_size = 64
    max_heap_table_size = 32M
    query_cache_type = 0
    ###global cache ###
    key_buffer_size = 1G
    query_cache_size = 0
    ###session cache ###
    sort_buffer_size = 8M       #排序缓冲
    join_buffer_size = 4M       #表连接缓冲
    read_buffer_size = 8M       #顺序读缓冲
    read_rnd_buffer_size = 8M   #随机读缓冲
    tmp_table_size = 32M        #内存临时表
    binlog_cache_size = 4M      #二进制日志缓冲
    thread_stack = 256KB        #线程的堆栈的大小
    ######################### binlog设置 #####################
    binlog_format = MIXED
    log_bin = /r2/mysqldata/binlog
    max_binlog_cache_size = 1G
    max_binlog_size = 1G
    expire_logs_days = 30
    sync_binlog =1
    ######################### 复制设置 ########################
    log_slave_updates=1
    #replicate-do-db=User
    #binlog-ignore-db = test
    #slave-skip-errors=1146,1032,1062
    ### GTID 配置 ###
    gtid_mode=ON
    enforce-gtid-consistency=true
    ### 开启并行复制(从库) ####
    #slave-parallel-type=LOGICAL_CLOCK     #基于组提交的并行复制方式
    #slave-parallel-workers=24             #并行的SQL线程数量
    #master-info_repository=TABLE          #master信息以表的形式保存
    #relay_log_info_repository=TABLE       #slave信息以表的形式保存
    #relay_log_recovery=ON                 #relay_log自我修复
    ######################### innodb ##########################
    default_storage_engine = InnoDB
    innodb_data_file_path = ibdata1:1G:autoextend
    innodb_buffer_pool_size = 32G         #系统内存50%
    innodb_open_files = 100000
    innodb_flush_log_at_trx_commit = 1
    innodb_file_per_table = 1
    innodb_lock_wait_timeout = 5
    #根据服务器IOPS能力适当调整innodb_io_capacity,一般配普通SSD盘的话,可以调整到 10000 - 20000
    innodb_io_capacity = 10000
    innodb_io_capacity_max = 20000
    innodb_flush_method = O_DIRECT
    innodb_log_file_size = 2G
    innodb_log_files_in_group = 2
    innodb_large_prefix = 0
    innodb_thread_concurrency = 64
    innodb_strict_mode = OFF
    innodb_sort_buffer_size = 4194304
    ### undolog设置 ###
    innodb_undo_directory = /r2/undolog              #undolog日志目录
    innodb_undo_tablespaces = 3                      #undolog日志文件个数
    innodb_undo_logs = 128                           #回滚段的数量, 至少大于等于35,默认128。
    innodb_max_undo_log_size = 1G                    #当超过这个阀值(默认是1G),会触发truncate回收(收缩)动作,truncate后空间缩小到10M。
    innodb_purge_rseg_truncate_frequency = 128       #控制回收(收缩)undolog的频率
    innodb_undo_log_truncate = 1                     #即开启在线回收undolog日志文件
    ######################### log 设置 #####################
    log_error = /r2/mysqldata/error.log
    slow_query_log = 1
    long_query_time = 10
    slow_query_log_file = /r2/mysqldata/slow.log
    #=======================================================================
    # MySQL mysqldump配置
    #=======================================================================
    [mysqldump]
    quick
    max_allowed_packet = 32M
    #=======================================================================
    # MySQL mysqld_safe配置
    #=======================================================================
    [mysqld_safe]
    log_error = /r2/mysqldata/error.log
    pid_file = /r2/mysqldata/mysqldb.pid
  • 2、配置MySQL目录和权限

    • 2.1、配置MySQL目录和权限
    
    [root@localhost-m(252) /usr/local]# mkdir -p /r2/mysqldata
    
    [root@localhost-m(252) /usr/local]# cd /r2/mysqldata
    
    [root@localhost-m(252) /r2/mysqldata]# mkdir -p /r2/undolog
    
    [root@localhost-m(252) /r2/mysqldata]# chown -R mysql.mysql /r2/undolog
    
    [root@localhost-m(252) /r2/mysqldata]# chown -R mysql.mysql /r2/mysqldata
    
    [root@localhost-m(252) /r2/mysqldata]# ll /r2 |grep undolog
    drwxr-xr-x  2 mysql mysql  4096 12月  4 10:57 undolog
  • 3、初始化MySQL和启动

    • 3.1、初始化MySQL5.7,获取随机密码
    [root@localhost-m(252) /r2/mysqldata]# /usr/local/mysql/bin/mysqld --initialize --user=mysql
    
    --获取随机密码
    [root@localhost-m(252) /r2/mysqldata]# grep password /r2/mysqldata/error.log
    2017-12-04T11:00:12.112903+08:00 1 [Note] A temporary password is generated for root@localhost: ;YV-le1Y5UYa    
    • 3.2添加MySQL服务到启动项,并启动mysql5.7
    [root@localhost-m(252) /r2/mysqldata]# cat > /usr/lib/systemd/system/mysql.service <EOF
    [Unit]
    Description=Mysql
    After=syslog.target network.target remote-fs.target nss-lookup.target
    [Service]
    Type=forking
    #PIDFile=/r2/mysqldata/mysqldb.pid
    ExecStart=/usr/local/mysql/support-files/mysql.server start
    ExecReload=/usr/local/mysql/support-files/mysql.server restart
    ExecStop=/usr/local/mysql/support-files/mysql.server stop
    LimitNOFILE = 65535
    PrivateTmp=false
    [Install]
    WantedBy=multi-user.target
    EOF
    
    [root@localhost-m(252) /r2/mysqldata]# systemctl daemon-reload
    
    [root@localhost-m(252) /r2/mysqldata]# systemctl start mysql
    
    --确认是否启动成功
    [root@localhost-m(252) /r2/mysqldata]# systemctl status mysql
    ● mysql.service - Mysql
       Loaded: loaded (/usr/lib/systemd/system/mysql.service; disabled; vendor preset: disabled)
       Active: active (running) since 一 2017-12-04 11:32:29 CST; 6s ago
      Process: 65996 ExecStart=/usr/local/mysql/support-files/mysql.server start (code=exited, status=0/SUCCESS)
     Main PID: 66004 (mysqld_safe)
       CGroup: /system.slice/mysql.service
               ├─66004 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/r2/mysqldata --pid-file=/r2/mysqldata/localhost.localdomain.pid
               └─66894 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/r2/mysqldata --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/r2/mysqldata/error.lo...
    
    12月 04 11:32:26 localhost.localdomain systemd[1]: Starting Mysql...
    12月 04 11:32:29 localhost.localdomain mysql.server[65996]: Starting MySQL... SUCCESS!
    12月 04 11:32:29 localhost.localdomain systemd[1]: Started Mysql.
    
    [root@localhost-m(252) /r2/mysqldata]# ps -ef | grep mysqld | grep -v grep
    root      66004      1  0 11:32 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/r2/mysqldata --pid-file=/r2/mysqldata/localhost.localdomain.pid
    mysql     66894  66004  1 11:32 ?        00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/r2/mysqldata --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/r2/mysqldata/error.log --open-files-limit=1024000 --pid-file=/r2/mysqldata/localhost.localdomain.pid --socket=/r2/mysqldata/mysql.sock --port=3306
  • 4、添加MySQL自启动服务

    [root@localhost-m(252) /r2/mysqldata]# systemctl enable mysql
    Created symlink from /etc/systemd/system/multi-user.target.wants/mysql.service to /usr/lib/systemd/system/mysql.service.
    
    [root@localhost-m(252) /r2/mysqldata]# systemctl list-unit-files |grep mysql
    mysql.service                               enabled
    pmm-mysql-metrics-42002.service             enabled
    pmm-mysql-queries-0.service                 enabled
    
    [root@localhost-m(252) /r2/mysqldata]#

六、导入备份的权限和数据

  • 1、导入权限sql

    --直接导入会报错,需要先修改root@localhost密码,然后再导入
    [root@localhost-m(252) /r2/mysqldata]# mysql -uroot -p
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 784
    Server version: 5.7.18-log MySQL Community Server (GPL)
    Copyright (c) 2000, 2017, 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.
    
    Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
    
    root@ipanel 11:45:  [(none)]> source /usr/local/src/grants.sql   
    ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.    --提示用alter user更改密码
    
    root@ipanel 11:47:  [(none)]>  alter user root@localhost identified by ‘123456‘;   --更改随机密码
    Query OK, 0 rows affected (0.00 sec)
    
    root@ipanel 11:48:  [(none)]> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    root@ipanel 11:48:  [(none)]> source /usr/local/src/grants.sql  --导入原来的账号密码,其中包括原来root账号密码
    Query OK, 0 rows affected, 2 warnings (0.00 sec)
    
    Query OK, 0 rows affected, 2 warnings (0.00 sec)
    
    Query OK, 0 rows affected, 2 warnings (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected, 2 warnings (0.00 sec)
    
    Query OK, 0 rows affected, 2 warnings (0.00 sec)
    
    Query OK, 0 rows affected, 2 warnings (0.00 sec)
    
    Query OK, 0 rows affected, 3 warnings (0.00 sec)
    
    Query OK, 0 rows affected, 3 warnings (0.00 sec)
    
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    
    root@ipanel 11:48:  [(none)]> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    root@ipanel 11:49:  [(none)]> select user,host,authentication_string from mysql.user;  
    +-----------+-----------+-------------------------------------------+
    | user      | host      | authentication_string                     |
    +-----------+-----------+-------------------------------------------+
    | root      | localhost | *xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |  
    ······
    | operator  | localhost | **xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
    +-----------+-----------+-------------------------------------------+
    9 rows in set (0.00 sec)
  • 2、导入主库备份文件

    [root@localhost-m(252) /r2/mysqldata]# mysql -uroot -pxxxxx </r2/bak_sql/all_20171201.sql
  • 3、确认升级是否成功
    • 检查error,是否有报错
    • 重启数据库验证是否有无报错
    • 验证账号是否可用(业务部门确认)
    
    [root@localhost-m(252) /r2/mysqldata]# cat /r2/mysqldata/error.log |grep error
    
    [root@localhost-m(252) /r2/mysqldata]# systemctl stop mysql
    [root@localhost-m(252) /r2/mysqldata]# systemctl status mysql
    ● mysql.service - Mysql
       Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; vendor preset: disabled)
       Active: inactive (dead) since 一 2017-12-04 14:58:57 CST; 10s ago
      Process: 80083 ExecStop=/usr/local/mysql/support-files/mysql.server stop (code=exited, status=0/SUCCESS)
     Main PID: 66004 (code=exited, status=0/SUCCESS)
    
    12月 04 11:32:26 localhost.localdomain systemd[1]: Starting Mysql...
    12月 04 11:32:29 localhost.localdomain mysql.server[65996]: Starting MySQL... SUCCESS!
    12月 04 11:32:29 localhost.localdomain systemd[1]: Started Mysql.
    12月 04 14:58:44 localhost.localdomain systemd[1]: Stopping Mysql...
    12月 04 14:58:57 localhost.localdomain mysql.server[80083]: Shutting down MySQL............. SUCCESS!
    12月 04 14:58:57 localhost.localdomain systemd[1]: Stopped Mysql.
    [root@localhost-m(252) /r2/mysqldata]# systemctl start mysql
    [root@localhost-m(252) /r2/mysqldata]# systemctl status mysql
    ● mysql.service - Mysql
       Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; vendor preset: disabled)
       Active: active (running) since 一 2017-12-04 14:59:18 CST; 1s ago
      Process: 80083 ExecStop=/usr/local/mysql/support-files/mysql.server stop (code=exited, status=0/SUCCESS)
      Process: 80207 ExecStart=/usr/local/mysql/support-files/mysql.server start (code=exited, status=0/SUCCESS)
     Main PID: 80215 (mysqld_safe)
       CGroup: /system.slice/mysql.service
               ├─80215 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/r2/mysqldata --pid-file=/r2/mysqldata/localhost.localdomain.pid
               └─81105 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/r2/mysqldata --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/r2/mysqldata/error.lo...
    
    12月 04 14:59:15 localhost.localdomain systemd[1]: Starting Mysql...
    12月 04 14:59:18 localhost.localdomain mysql.server[80207]: Starting MySQL... SUCCESS!
    12月 04 14:59:18 localhost.localdomain systemd[1]: Started Mysql.
  • 4、升级完成,清除权限文件后,请跳过步骤II(回滚),直接进入步骤III

    [root@localhost-m(252) /r2/mysqldata]# rm /usr/local/src/grants.sql
    rm: remove regular file ‘/usr/local/src/grants.sql’? Y

    II、升级失败回滚


一、升级回退到mysql5.5

  • 1、关闭数据库

    [root@localhost-m(252) /r2/mysqldata]# systemctl stop mysql
    [root@localhost-m(252) /r2/mysqldata]# ps -ef |grep mysqld
    root      85807  64413  0 16:13 pts/0    00:00:00 grep --color=auto mysqld
  • 2、恢复5.5.33数据目录


[root@localhost-m(252) /r2]# pwd
/r2
[root@localhost-m(252) /r2]# mv mysqldata mysqldata57bak
[root@localhost-m(252) /r2]# mv mysqldatabak mysqldata
  • 2、恢复5.5.33配置文件
[root@localhost-m(252) /r2]# cp /etc/my.cnf mysqldata57bak/my.cnf
[root@localhost-m(252) /r2]# cp mysqldata/my.cnf  /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? y
  • 3、恢复mysql5.5.33 软件
[root@localhost-m(252) /r2]# cd /usr/local/
[root@localhost-m(252) /usr/local]# ll |grep mysql
lrwxrwxrwx   1 mysql  mysql     34 12月  4 09:20 mysql -> mysql-5.7.18-linux-glibc2.5-x86_64  --mysql链接5.7
drwxr-xr-x. 13 root   root    4096 3月   9  2016 mysql5.5
drwxr-xr-x   9 mysql  mysql   4096 12月  4 09:18 mysql-5.7.18-linux-glibc2.5-x86_64

-- 删除5.7 mysql链接
[root@localhost-m(252) /usr/local]# rm mysql
rm: remove symbolic link ‘mysql’? y

[root@localhost-m(252) /usr/local]# ll |grep mysql
drwxr-xr-x. 13 root   root    4096 3月   9  2016 mysql5.5
drwxr-xr-x   9 mysql  mysql   4096 12月  4 09:18 mysql-5.7.18-linux-glibc2.5-x86_64

--重建5.5.33 链接
[root@localhost-m(252) /usr/local]# ln -s mysql5.5 mysql

[root@localhost-m(252) /usr/local]# ll |grep mysql
lrwxrwxrwx   1 root   root       8 12月  4 16:19 mysql -> mysql5.5    --mysql链接到5.5
drwxr-xr-x. 13 root   root    4096 3月   9  2016 mysql5.5
drwxr-xr-x   9 mysql  mysql   4096 12月  4 09:18 mysql-5.7.18-linux-glibc2.5-x86_64

--重新授权软件目录权限
[root@localhost-m(252) /usr/local]# chown -R mysql.mysql /usr/local/mysql

[root@localhost-m(252) /usr/local]# chown -R mysql.mysql /usr/local/mysql5.5

[root@localhost-m(252) /usr/local]# ll |grep mysql
lrwxrwxrwx   1 mysql  mysql      8 12月  4 16:19 mysql -> mysql5.5
drwxr-xr-x. 13 mysql  mysql   4096 3月   9  2016 mysql5.5
drwxr-xr-x   9 mysql  mysql   4096 12月  4 09:18 mysql-5.7.18-linux-glibc2.5-x86_64

--重新授权数据目录权限
[root@localhost-m(252) /usr/local]# chown -R mysql.mysql /r2/mysqldata

[root@localhost-m(252) /usr/local]# ll /r2/ |grep mysqldata
drwxr-xr-x 64 mysql mysql  4096 12月  4 16:28 mysqldata
drwxr-xr-x 53 mysql mysql 12288 12月  4 16:16 mysqldata57bak
  • 4、启动mysql5.5实例

[root@localhost-m(252) /usr/local]# systemctl start mysql
[root@localhost-m(252) /usr/local]# systemctl status mysql
● mysql.service - Mysql
   Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2017-12-04 16:28:15 CST; 5s ago
  Process: 89752 ExecStart=/usr/local/mysql/support-files/mysql.server start (code=exited, status=0/SUCCESS)
 Main PID: 89764 (mysqld_safe)
   CGroup: /system.slice/mysql.service
           ├─89764 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/r2/mysqldata --pid-file=/r2/mysqldata/localhost.localdomain.pid
           └─90233 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/r2/mysqldata --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/r2/mysqldata/localhos...

12月 04 16:28:13 localhost.localdomain systemd[1]: Starting Mysql...
12月 04 16:28:15 localhost.localdomain mysql.server[89752]: Starting MySQL.. SUCCESS!
12月 04 16:28:15 localhost.localdomain systemd[1]: Started Mysql.

[root@localhost-m(252) /usr/local]# ps -ef |grep mysqld |grep -v grep
root      89764      1  0 16:28 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/r2/mysqldata --pid-file=/r2/mysqldata/localhost.localdomain.pid
mysql     90233  89764  0 16:28 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/r2/mysqldata --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/r2/mysqldata/localhost.localdomain.err --pid-file=/r2/mysqldata/localhost.localdomain.pid --socket=/r2/mysqldata/mysql.sock --port=3306
  • 5、登录验证mysql5.5实例
[root@localhost-m(252) /usr/local]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.33-log Source distribution

Copyright (c) 2000, 2013, 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.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql>

III、配置主从复制


一、配置基于GTID的并行复制

  • 例如:主库(192.68.48.168)和从库(192.168.24.180)

  • 1、从库(192.168.24.180)

    • 从库my.cnf添加并行复制参数,修改server-id=2

      ### 开启并行复制(从库) ####
      slave-parallel-type=LOGICAL_CLOCK     #基于组提交的并行复制方式
      slave-parallel-workers=24             #并行的SQL线程数量
      master-info_repository=TABLE          #master信息以表的形式保存
      relay_log_info_repository=TABLE       #slave信息以表的形式保存
      relay_log_recovery=ON                 #relay_log自我修复
  • 2、建立主从复制

    • 2.1.从原主库复制备份文件,从库导入原主库备份文件
    [root@proxy r2]# cd bak_sql/
    [root@proxy bak_sql]# scp 192.168.48.168:/r2/bak_sql/all_20171201.sql ./
    • 2.2.建立GTID主从复制
    [root@proxy ~]# mysql -uroot -p
    (root@localhost) 19:15:50 [(none)]> source /r2/bak_sql/all_20171201.sql
    (root@localhost) 20:01:14 [(none)]> change master to
    -> master_host=‘192.168.48.168‘,
    -> master_user=‘repl‘,
    -> master_password=‘repl‘,
    -> master_auto_position = 1;
    Query OK, 0 rows affected, 2 warnings (0.02 sec)
    (root@localhost) 20:04:49 [(none)]> start slave;
    Query OK, 0 rows affected (0.09 sec)
    
    (root@localhost) 20:04:55 [(none)]> show slave status \G;
    *************************** 1. row ***************************
                   Slave_IO_State: Queueing master event to the relay log
                      Master_Host: 192.168.48.168
                      Master_User: repl
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: binlog.000002
              Read_Master_Log_Pos: 139295376
                   Relay_Log_File: proxy-relay-bin.000002
                    Relay_Log_Pos: 81035047
            Relay_Master_Log_File: binlog.000002
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB:
              Replicate_Ignore_DB:
               Replicate_Do_Table:
           Replicate_Ignore_Table:
          Replicate_Wild_Do_Table:
      Replicate_Wild_Ignore_Table:
                       Last_Errno: 0
                       Last_Error:
                     Skip_Counter: 0
              Exec_Master_Log_Pos: 81034840
                  Relay_Log_Space: 139295790
                  Until_Condition: None
                   Until_Log_File:
                    Until_Log_Pos: 0
               Master_SSL_Allowed: No
               Master_SSL_CA_File:
               Master_SSL_CA_Path:
                  Master_SSL_Cert:
                Master_SSL_Cipher:
                   Master_SSL_Key:
            Seconds_Behind_Master: 10279
    Master_SSL_Verify_Server_Cert: No
                    Last_IO_Errno: 0
                    Last_IO_Error:
                   Last_SQL_Errno: 0
                   Last_SQL_Error:
      Replicate_Ignore_Server_Ids:
                 Master_Server_Id: 1
                      Master_UUID: 3eae5854-d89f-11e7-ab20-246e960a8d84
                 Master_Info_File: mysql.slave_master_info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: Waiting for dependent transaction to commit
               Master_Retry_Count: 86400
                      Master_Bind:
          Last_IO_Error_Timestamp:
         Last_SQL_Error_Timestamp:
                   Master_SSL_Crl:
               Master_SSL_Crlpath:
               Retrieved_Gtid_Set: 3eae5854-d89f-11e7-ab20-246e960a8d84:1-2476
                Executed_Gtid_Set: 3eae5854-d89f-11e7-ab20-246e960a8d84:1-1408,
    9dc847d8-bf72-11e7-9ec4-000c2998e4f1:1-2845713
                    Auto_Position: 1
             Replicate_Rewrite_DB:
                     Channel_Name:
               Master_TLS_Version:
    1 row in set (0.01 sec)
    
    ERROR:
    No query specified
    • 2.3 检查多线程复制
    --slave启动了24个线程(Waiting for an event from Coordinator)
    (root@localhost) 15:34:15 [(none)]> show processlist;
    +----+-------------+-----------+------+---------+---------+--------------------------------------------------------+------------------+
    | Id | User        | Host      | db   | Command | Time    | State                                                  | Info             |
    +----+-------------+-----------+------+---------+---------+--------------------------------------------------------+------------------+
    |  4 | system user |           | NULL | Connect | 1366166 | Waiting for master to send event                       | NULL             |
    |  5 | system user |           | NULL | Connect | 1364215 | Slave has read all relay log; waiting for more updates | NULL             |
    |  6 | system user |           | NULL | Connect |     843 | Waiting for an event from Coordinator                  | NULL             |
    |  7 | system user |           | NULL | Connect |   12366 | Waiting for an event from Coordinator                  | NULL             |
    |  8 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    |  9 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 10 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 11 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 12 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 13 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 14 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 15 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 16 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 17 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 18 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 19 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 20 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 21 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 22 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 23 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 24 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 25 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 26 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 27 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 28 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 29 | system user |           | NULL | Connect | 1366166 | Waiting for an event from Coordinator                  | NULL             |
    | 49 | root        | localhost | NULL | Query   |       0 | starting                                               | show processlist |
    +----+-------------+-----------+------+---------+---------+--------------------------------------------------------+------------------+
    27 rows in set (0.00 sec)
    • 2.4、验证主从复制(增删改查)
    --主库
    mysql> create database tttt;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> create table tttt.aa(a int);
    Query OK, 0 rows affected (0.05 sec)
    
    mysql> insert into tttt.aa values(1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into tttt.aa values(2);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from tttt.aa;
    +------+
    | a    |
    +------+
    |    1 |
    |    2 |
    +------+
    2 rows in set (0.00 sec)
    
    mysql> update tttt.aa set a=10 where a =1;
    Query OK, 1 row affected (0.03 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> delete from  tttt.aa where a =2;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from tttt.aa;
    +------+
    | a    |
    +------+
    |   10 |
    +------+
    1 row in set (0.00 sec)
    --从库
    (root@localhost) 15:39:28 [(none)]> select * from tttt.aa;
    +------+
    | a    |
    +------+
    |   10 |
    +------+
    1 row in set (0.00 sec)
    • 2.5、 drop 测试库

      mysql> drop database tttt;
      Query OK, 1 row affected (0.06 sec)

      IV、确认服务正常


一、业务开启,观察数据,确认从库是否正常接收到数据

  • 主库

    (root@localhost) 10:04:08 [(none)]> show master status \G;
    *************************** 1. row ***************************
                 File: binlog.000004
             Position: 38701778
         Binlog_Do_DB:
     Binlog_Ignore_DB:
    Executed_Gtid_Set: 3eae5854-d89f-11e7-ab20-246e960a8d84:1-7887
    1 row in set (0.00 sec)
    
    ERROR:
    No query specified
    
    (root@localhost) 10:04:08 [(none)]> show master status \G;
    *************************** 1. row ***************************
                 File: binlog.000004
             Position: 41768570                                         --当数据写入binlog pos点一直在变
         Binlog_Do_DB:
     Binlog_Ignore_DB:
    Executed_Gtid_Set: 3eae5854-d89f-11e7-ab20-246e960a8d84:1-7890      --GTID值也在变化
    1 row in set (0.00 sec)
    
    ERROR:
    No query specified
    
    (root@localhost) 10:04:21 [(none)]> show master status \G;
    *************************** 1. row ***************************
                 File: binlog.000004
             Position: 43813112
         Binlog_Do_DB:
     Binlog_Ignore_DB:
    Executed_Gtid_Set: 3eae5854-d89f-11e7-ab20-246e960a8d84:1-7892
    1 row in set (0.00 sec)
    
    
  • 从库

    (root@localhost) 12:10:37 [(none)]> show slave status \G;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.48.168
                      Master_User: repl
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: binlog.000004
              Read_Master_Log_Pos: 105168419
                   Relay_Log_File: proxy-relay-bin.000004
                    Relay_Log_Pos: 105168472
            Relay_Master_Log_File: binlog.000004
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB:
              Replicate_Ignore_DB:
               Replicate_Do_Table:
           Replicate_Ignore_Table:
          Replicate_Wild_Do_Table:
      Replicate_Wild_Ignore_Table:
                       Last_Errno: 0
                       Last_Error:
                     Skip_Counter: 0
              Exec_Master_Log_Pos: 105168265
                  Relay_Log_Space: 105170645
                  Until_Condition: None
                   Until_Log_File:
                    Until_Log_Pos: 0
               Master_SSL_Allowed: No
               Master_SSL_CA_File:
               Master_SSL_CA_Path:
                  Master_SSL_Cert:
                Master_SSL_Cipher:
                   Master_SSL_Key:
            Seconds_Behind_Master: 0
    Master_SSL_Verify_Server_Cert: No
                    Last_IO_Errno: 0
                    Last_IO_Error:
                   Last_SQL_Errno: 0
                   Last_SQL_Error:
      Replicate_Ignore_Server_Ids:
                 Master_Server_Id: 1
                      Master_UUID: 3eae5854-d89f-11e7-ab20-246e960a8d84
                 Master_Info_File: mysql.slave_master_info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
               Master_Retry_Count: 86400
                      Master_Bind:
          Last_IO_Error_Timestamp:
         Last_SQL_Error_Timestamp:
                   Master_SSL_Crl:
               Master_SSL_Crlpath:
               Retrieved_Gtid_Set: 3eae5854-d89f-11e7-ab20-246e960a8d84:1-7952
                Executed_Gtid_Set: 3eae5854-d89f-11e7-ab20-246e960a8d84:1-7952,
    9dc847d8-bf72-11e7-9ec4-000c2998e4f1:1-2845713
                    Auto_Position: 1
             Replicate_Rewrite_DB:
                     Channel_Name:
               Master_TLS_Version:
    1 row in set (0.00 sec)
    
    ERROR:
    No query specified
    
    (root@localhost) 12:10:48 [(none)]> show slave status \G;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.48.168
                      Master_User: repl
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: binlog.000004
              Read_Master_Log_Pos: 112328518
                   Relay_Log_File: proxy-relay-bin.000004
                    Relay_Log_Pos: 111305709
            Relay_Master_Log_File: binlog.000004
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB:
              Replicate_Ignore_DB:
               Replicate_Do_Table:
           Replicate_Ignore_Table:
          Replicate_Wild_Do_Table:
      Replicate_Wild_Ignore_Table:
                       Last_Errno: 0
                       Last_Error:
                     Skip_Counter: 0
              Exec_Master_Log_Pos: 111305502          --执行接受主库pos点
                  Relay_Log_Space: 112330744         
                  Until_Condition: None
                   Until_Log_File:
                    Until_Log_Pos: 0
               Master_SSL_Allowed: No
               Master_SSL_CA_File:
               Master_SSL_CA_Path:
                  Master_SSL_Cert:
                Master_SSL_Cipher:
                   Master_SSL_Key:
            Seconds_Behind_Master: 0
    Master_SSL_Verify_Server_Cert: No
                    Last_IO_Errno: 0
                    Last_IO_Error:
                   Last_SQL_Errno: 0
                   Last_SQL_Error:
      Replicate_Ignore_Server_Ids:
                 Master_Server_Id: 1
                      Master_UUID: 3eae5854-d89f-11e7-ab20-246e960a8d84
                 Master_Info_File: mysql.slave_master_info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: Waiting for dependent transaction to commit
               Master_Retry_Count: 86400
                      Master_Bind:
          Last_IO_Error_Timestamp:
         Last_SQL_Error_Timestamp:
                   Master_SSL_Crl:
               Master_SSL_Crlpath: 
               Retrieved_Gtid_Set: 3eae5854-d89f-11e7-ab20-246e960a8d84:1-7959  --接受主库GTID值也在变化
                Executed_Gtid_Set: 3eae5854-d89f-11e7-ab20-246e960a8d84:1-7958,  
    9dc847d8-bf72-11e7-9ec4-000c2998e4f1:1-2845713
                    Auto_Position: 1
             Replicate_Rewrite_DB:
                     Channel_Name:
               Master_TLS_Version:
    1 row in set (0.00 sec)
    
    ERROR:
    No query specified
    
    (root@localhost) 12:10:50 [(none)]>

二、备份从库

  • 主从正常,从库执行备份
    • 备份命令 : mysqldump -uroot -p --single-transaction --master-data=2 -E -R --flush-logs -A >/r2/bak_sql/all_XXXXXXX.sql

      [root@proxy bak_sql]# mysqldump -uroot -p --single-transaction --master-data=2 -E -R  --flush-logs -A >/r2/bak_sql/all_`date +"%Y%m%d"`.sql
      Enter password:
      [root@proxy bak_sql]# ll
      total 269124
      -rw-r--r-- 1 root root 275582147 Dec  5 12:38 all_20171205.sql

V、清理升级过程产生的数据


一、清理数据

  • 清理备份文件夹/r2/mysqldatabak
  • 清理备份文件夹/usr/local/mysql5.5
  • 清理权限文件/usr/local/src/grants.sql

MySQL升级指南

标签:load   read   x86_64   ldo   登录   调整   ash   hang   mysql5.7   

原文地址:http://www.cnblogs.com/gczheng/p/7993406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!