zoukankan      html  css  js  c++  java
  • 转 mysql 自动安装部署

    ## 如下是脚本部署,一共分为7个section ,

    ###准备部署

    ##方法是通过主机 推送软件到 机器,然后机器使用 本地仓库 部署。使用软件tar包和 data tar 包来安装。
    ##同时使用use db data home clone tar to install data
    ##不使用用 mysqld --initialize to init data
    ##注意这里 固定了my.cnf 如下base dir 如下参数,如果参数发生变化,就无法标准化安装
    --10.10.10.187:/dbsoft/mysql/my.cnf
    --10.10.10.187:/dbsoft/mysql/mysql-advanced-5.7.24-linux-glibc2.12-x86_64.tar.gz

    --defaults-file=/db/mysql/data/3306/my.cnf (mysql 配置文件目录)
    --basedir=/db/mysql/app/ (mysql 软件安装目录)
    --datadir=/db/mysql/data/3306/mydata
    --plugin-dir=/db/mysql/app/lib/plugin/
    --log-error=/db/mysql/data/3306/mydata/mysql-error.log (/db/mysql/data/3306/mydata 数据文件目录)
    --pid-file=/db/mysql/data/3306/mydata/mysql.pid
    --socket=/db/mysql/data/3306/mysqltmp/mysql.sock
    --os 参数如下:
    --"mysql hard nofile 65535"
    --"mysql soft nofile 65535" /etc/security/limits.conf
    --

    ###begin to install
    ###perepare for scp expect
    yum install -y expect


    ##section 1 local get software (in vcs standby should also running step1 and step2 to create mysql user)

    sleep 30;
    echo "it is section 1 ,it is use to get all software from repository"

    cd /tmp/
    mkdir soft
    cd /tmp/soft
    chmod 777 /tmp/soft

    ##scp user@10.10.10.187:/dbsoft/mysql/mysql-advanced-5.7.24-linux-glibc2.12-x86_64.tar.gz .
    ##change scp to expect for auto get file from 187
    expect <<- EOF
    set timeout 5
    spawn sftp -P 22 user@10.10.10.187

    expect {
    "(yes/no)?" {send "yes "; expect_continue }
    "*assword:" {send "CR@zh123 "}
    }
    expect "sftp>"
    send "cd /dbsoft/mysql "
    expect "sftp>"
    send "lcd /tmp/soft "
    expect "sftp>"
    set timeout -1
    send "mget mysql-advanced-5.7.24-linux-glibc2.12-x86_64.tar.gz "
    expect "sftp>"
    send "mget my.cnf "
    expect "sftp>"
    send "mget .mylogin.cnf "
    expect "sftp>"
    send "mget mydata_5.7_data.tar.gz "
    expect "sftp>"
    send "mget db_user_grant.sql "
    expect "sftp>"
    send "mget mysql_mon.tar "
    expect "sftp>"
    send "bye "
    EOF


    chmod -R 777 /tmp/soft


    ##section 2 add user mysql and config mysql user env and config .mylogin.cnf

    sleep 30;
    echo "it is section 2 ,it is create user and config user mysql env"

    groupadd mysql
    useradd -g mysql mysql
    mkdir /db/mysql/app
    mkdir /db/mysql/data/
    chown -R mysql:mysql /db/mysql/app
    chown -R mysql:mysql /db/mysql/data


    ##su - mysql
    #vi.bash_profile


    echo "export MYSQL_HOME=/db/mysql/app/mysql" >> /home/mysql/.bash_profile
    echo "export PATH=$MYSQL_HOME/bin:$PATH" >> /home/mysql/.bash_profile
    echo "alias mysqlroot='mysql --login-path=root' " >> /home/mysql/.bash_profile

    ##make sure mysql client veresion is 5.7 to let mysql --login-path=root useful
    echo "export MYSQL_HOME=/db/mysql/app/mysql" >> /home/user/.bash_profile
    echo "export PATH=$PATH:$MYSQL_HOME/bin" >> /home/user/.bash_profile

    cp /tmp/soft/.mylogin.cnf /home/mysql
    chown mysql:mysql /home/mysql/.mylogin.cnf
    chmod 600 /home/mysql/.mylogin.cnf

    cp /home/mysql/.mylogin.cnf /home/user/.mylogin.cnf


    ##section 3 create directory and install mysql soft

    sleep 30;
    echo "it is section 3 ,the section is create directory and install mysql software"
    su - mysql

    cp /tmp/soft/mysql-advanced-5.7.24-linux-glibc2.12-x86_64.tar.gz /db/mysql/app/
    cd /db/mysql/app/
    tar -zxvf mysql-advanced-5.7.24-linux-glibc2.12-x86_64.tar.gz
    mv mysql-advanced-5.7.24-linux-glibc2.12-x86_64 mysql


    mkdir -p /db/mysql/data/3306/mysqltmp
    mkdir -p /db/mysql/data/3306/mydata
    mkdir -p /db/mysql/app/lib/plugin/

    ##(,because meet 0 [ERROR] --initialize specified but the data directory has files in it. Aborting,rm it ).
    mkdir -p /db/mysql/data/3306/mydata/tmp
    rm -rf /db/mysql/data/3306/mydata/tmp


    ###section 4.:(use db hoem clone tar to install mysql isntance )

    echo "it is section 4 ,the section is create mysql instance"
    cd /db/mysql/data/3306/mydata
    cp /tmp/soft/mydata_5.7_data.tar.gz .
    tar -zxvf mydata_5.7_data.tar.gz


    cd /db/mysql/data/3306/mydata
    rm auto.cnf

    ##section 5: edit my.cnf with
    ##customer vip info into my.cnf and restart mysql
    ##change my.cnf follwoing:local ip 192.168.12.45

    echo "###"
    echo "#####"
    sleep 20;
    echo "it is section 5 it is used to edit my.cnf"

    ##prompt vip and echo the info to my.cnf
    echo "Please input your vip: [such as 10.10.10.10]"

    read opreation
    echo $opreation

    echo "select INET_ATON('$opreation');" > /tmp/soft/my_ip.sql

    #get uniq server_id from yumserver
    mysql -udba -pdb1234DBA -h10.10.10.187 -P 3306 < /tmp/soft/my_ip.sql > /tmp/soft/my_ip.log
    server_id=`tail -1 /tmp/soft/my_ip.log`

    ##use sed to replace the file

    sed "s/#bind-address=10.10.10.187/bind-address=$

    opreation/g" /tmp/soft/my.cnf > /tmp/soft/my.cnf.new
    mv /tmp/soft/my.cnf.new /tmp/soft/my.cnf
    sed "s/server_id=1/server_id=$server_id/g" /tmp/soft/my.cnf > /tmp/soft/my.cnf.new
    mv /tmp/soft/my.cnf.new /tmp/soft/my.cnf

    cp /tmp/soft/my.cnf.new /db/mysql/data/3306/my.cnf
    #cp /tmp/soft/my.cnf /db/mysql/data/3306/my.cnf


    /db/mysql/app/mysql/bin/mysqld_safe --defaults-file=/db/mysql/data/3306/my.cnf &


    complete=`ps -ef|grep mysqld_safe |grep -v "grep" |wc -l`
    if [ $complete -eq 1 ]
    then
    echo "**********mysql is up (^_^) ************"
    fi

    ####section 6 (restart instance)

    echo "it is section 6 and it use restart mysql instance"

    /db/mysql/app/mysql/bin/mysqladmin -uroot -pmysql shutdown --socket=/db/mysql/data/3306/mysqltmp/mysql.sock


    /db/mysql/app/mysql/bin/mysqld_safe --defaults-file=/db/mysql/data/3306/my.cnf &

    tail -3 /db/mysql/data/3306/my.cnf


    ##section 7

    ##mysql_config_editor set --login-path=root --user=root -p --socket=/db/mysql/data/3306/mysqltmp/mysql.sock

    ##mysql --login-path=root

    #########完成部署

    ####开始部署mysql 监控

    #1.固定文件部分,打包的zabbix 的客户端部署程序里。
    ##2. 监控用户配置,需要在这个文件里执行。
    ### 因为产生的文件是<vip>-mysql_cacti_stats.txt, 所以主机在发生切换时候,只要VIP 地址存在,就不需要在VCS 备机 重配zabbxi agent ,只需要在双机配置一台机器即可
    ##0.前期准备工作监控用户 dbmonopr
    ###for application user
    ##mysql添加监控前数据库准备工作

    ##local server is begin deploy
    ### section 1 ,creae user for monistor user ,add 监控 vip网段 (仅仅包括本地和容灾库库所在 网段)and 本地 login 权限,保证客户端登录安全
    ##in root user
    ##mysql --login-path=root </tmp/soft/t.sql

    mysql --login-path=root < /tmp/soft/db_user_grant.sql
    ##cat /tmp/soft/db_user_grant.sql

    #GRANT SUPER,PROCESS,REPLICATION CLIENT ON *.* TO 'dbmgr'@'%' IDENTIFIED BY 'db1234DBA';
    #GRANT SUPER,PROCESS,REPLICATION CLIENT ON *.* TO 'dbmgr'@'localhost' IDENTIFIED BY 'db1234DBA';

    ##监控用户权限 调整到10.198 和 58.2 这2个网段
    #GRANT SUPER,PROCESS,REPLICATION CLIENT ON *.* TO 'dbmonopr'@'10.198.%' identified by 'dbmon_123';
    #GRANT SUPER,PROCESS,REPLICATION CLIENT ON *.* TO 'dbmonopr'@'58.2.%' identified by 'dbmon_123';
    #GRANT SUPER,PROCESS,REPLICATION CLIENT ON *.* TO 'dbmonopr'@'localhost' IDENTIFIED BY 'dbmon_123';
    #flush privileges;

    ##remove passwd column
    #select user,host from mysql.user;


    ##section 2.确保主机 有 php 安装软件包
    which php

    #如果没有使用yum 安装
    yum list
    yum install percona-zabbix-templates php php-mysql -y
    yum -y install perl-DBI
    yum -y install perl-DBD-MySQL
    yum -y install perl-IO-Socket-SSL.noarch
    yum -y install perl-Time-HiRes
    yum -y install perl-TermReadKey
    yum -y install perl-ExtUtils-MakeMaker


    ##link /usr/bin/mysql to new mysql client
    ln -s /db/mysql/app/mysql/bin/mysql /usr/bin/mysql


    ##3.验证密码


    #1,部署方法, (以下部署方法是假设这些文件没有通过zabbix agent 分发出来的,需要手工部署)

    ##脚本(将脚本放在/tmp/soft): 传可执行文件到各个目录下

    ##section 3: begin to deploy montior script to zabbix agent
    ##begin to explore
    ##cd /tmp/dba
    cd /tmp/soft
    tar xvf mysql_mon.tar
    chown zabbix:zabbix *.sh *.php
    ##for zabbix scripts
    cp mysql_low_discovery.sh /usr/local/zabbix/bin/mysql_low_discovery.sh
    cp get_mysql_stats_wrapper.sh /usr/local/zabbix/bin/get_mysql_stats_wrapper.sh
    cp ss_get_mysql_stats.php /usr/local/zabbix/bin/ss_get_mysql_stats.php

    chown zabbix:zabbix /usr/local/zabbix/bin/mysql_low_discovery.sh
    chown zabbix:zabbix /usr/local/zabbix/bin/get_mysql_stats_wrapper.sh
    chown zabbix:zabbix /usr/local/zabbix/bin/ss_get_mysql_stats.php

    chmod 755 /usr/local/zabbix/bin/mysql_low_discovery.sh
    chmod 755 /usr/local/zabbix/bin/get_mysql_stats_wrapper.sh
    chmod 755 /usr/local/zabbix/bin/ss_get_mysql_stats.php
    ## for zabbix config file
    cp userparameter_percona_mysql.conf /usr/local/zabbix/etc/zabbix_agentd.conf.d/userparameter_percona_mysql.conf
    chown zabbix:zabbix /usr/local/zabbix/etc/zabbix_agentd.conf.d/userparameter_percona_mysql.conf
    chmod 755 /usr/local/zabbix/etc/zabbix_agentd.conf.d/userparameter_percona_mysql.conf

    ##for /etc/sudoers files
    echo 'zabbix ALL=(ALL) NOPASSWD:/usr/sbin/ss'>>/etc/sudoers
    echo 'zabbix ALL=(ALL) NOPASSWD:/bin/netstat'>>/etc/sudoers

    ##for mysql_slow_query
    rpm -ivh /tmp/soft/percona-toolkit-3.1.0-2.el6.x86_64.rpm
    rpm -ivh /tmp/soft/percona-toolkit-3.2.0-1.el7.x86_64.rpm
    mkdir /backup/script/mysql
    mkdir /backup/script/mysql/sh
    chmod 755 mysql_slowquery.sh
    cp mysql_slowquery.sh /backup/script/mysql

    ##for crontab mysql ,3306 is mysql port
    ##echo "#*/15 * * * * /bin/bash /backup/script/mysql/mysql_slowquery.sh 3306 > /dev/null 2>&1" >> /var/spool/cron/root


    ##make sure the permmsion is zabbix
    ls -tlr /usr/local/zabbix/bin/mysql_low_discovery.sh
    ls -ltr /usr/local/zabbix/bin/ss_get_mysql_stats.php
    ls -tlr /usr/local/zabbix/etc/zabbix_agentd.conf.d/userparameter_percona_mysql.conf
    ls -tlr /usr/local/zabbix/bin/get_mysql_stats_wrapper.sh

    ###section 4 modify get_mysql_stats_wrapper.sh of actural mysql vip info
    #最后编辑该文件 /usr/local/zabbix/bin/get_mysql_stats_wrapper.sh 第14行
    #HOST=58.20.96.75 改成正确的VIP
    #HOST=58.2.106.18
    sleep 30;
    echo "Please input your db vip: [such as 10.10.10.10]"

    read opreation
    echo $

    opreation

    sed "s/#HOST=58.20.96.75/HOST=$

    opreation/g" /usr/local/zabbix/bin/get_mysql_stats_wrapper.sh > /tmp/soft/get_mysql_stats_wrapper.sh.new
    mv /tmp/soft/get_mysql_stats_wrapper.sh.new /tmp/soft/get_mysql_stats_wrapper.sh
    cp /tmp/soft/get_mysql_stats_wrapper.sh /usr/local/zabbix/bin/get_mysql_stats_wrapper.sh
    chmod 755 /usr/local/zabbix/bin/get_mysql_stats_wrapper.sh
    chown zabbix:zabbix /usr/local/zabbix/bin/get_mysql_stats_wrapper.sh


    ##section 5 restart zabbix agent
    pkill zabbix
    /usr/local/zabbix/sbin/zabbix_agentd

    ##local server is end deploy

    ###监控mysql 完成配置

    ###################

    ##参考文档和碰到的问题如下:

    ########### 参考文档如下 :感谢

    I tested these commands against a CentOS 7.5 instance on Google Cloud. Once the script finishes executing, you should be able to log in to the database server with the new password.

    #!/bin/bash
    # Description: Set up MySQL Community Release 5.7

    # Get the repo RPM and install it.
    wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
    yum -y install ./mysql57-community-release-el7-7.noarch.rpm

    # Install the server and start it
    yum -y install mysql-community-server
    systemctl start mysqld

    # Get the temporary password
    temp_password=$(grep password /var/log/mysqld.log | awk '{print $NF}')

    # Set up a batch file with the SQL commands
    echo "ALTER USER 'root'@'localhost' IDENTIFIED BY 'Newhakase-labs123@'; flush privileges;" > reset_pass.sql

    # Log in to the server with the temporary password, and pass the SQL file to it.
    mysql -u root --password="$temp_password" --connect-expired-password < reset_pass.sql


    ###############2

    https://www.jb51.net/article/51757.htm


    ####3

    ##tar mydata.tar
    su - mysql
    cd /db/mysql/data/3306/mydata
    tar -czvf mydata_5.7.tar.gz *

    tar -zxvf mydata_5.7.tar.gz


    ##
    mysql_config_editor — MySQL Configuration Utility
    The unobfuscated format of the .mylogin.cnf
    login path file consists of option groups, similar to other option files. Each option group in .mylogin.cnf is called a “login path,” which is a group that permits only certain options: host, user, password, port and socket. Think of a login path option group as a set of options that specify which MySQL server to connect to and which account to authenticate as. Here is an unobfuscated example:


    #######问题1,主从库 slave io 一直异常之问题分析,error in 汕尾


    2020-06-13T10:33:27.237296Z 6 [Note] Slave I/O thread for channel '': connected to master 'resync@10.10.227.213:3306',replication started in log 'FIRST' at position 4

    mysql> GRANT REPLICATION SLAVE ON *.* TO 'resync'@'%' IDENTIFIED BY 'Resync$123';
    Query OK, 0 rows affected, 1 warning (0.00 sec)


    mysql> flush privileges;
    Query OK, 0 rows affected (0.01 sec)

    mysql>
    CHANGE MASTER TO MASTER_HOST='10.10.227.213', MASTER_USER='resync',Master_Port=3306, MASTER_PASSWORD='Resync$123', MASTER_AUTO_POSITION=1;


    mysql -uresync -p -h 10.10.227.213 -P 3306

    2020-06-13T12:34:18.152867Z 8 [Note] Slave I/O thread for channel '': connected to master 'resync@10.10.227.213:3306',replication started in log 'mysql-bin.000010' at position 194
    2020-06-13T12:34:18.175361Z 8 [ERROR] Slave I/O for channel '': , Error_code: 1593
    2020-06-13T12:34:18.175400Z 8 [Note] Slave I/O thread exiting for channel '', read up to log 'mysql-bin.000010'


    fix:

    ##/db/mysql/app/mysql/bin/mysqldump --all-databases --single-transaction --master-data=2 --set-gtid-purged=off -F --triggers --routines --events --user=root -p --socket=/db/mysql/data/3306/mysqltmp/mysql.sock > all111_0423.sql

    ##use mysqldump 仍然报错
    mysqldump --all-databases --single-transaction --triggers --routines --events --host=localhost --port=3306 --user=root --password=mysql --socket=/db/mysql/data/3306/mysqltmp/mysql.sock >/tmp/alldb.sql
    grep GTID_PURGED /tmp/alldb.sql


    create table t1(id int,ename varchar(20));
    insert into t1 values(1,'leshami');

    mysqldump --all-databases --single-transaction --triggers --routines --events --host=localhost --port=3306 --user=root --password=mysql --socket=/db/mysql/data/3306/mysqltmp/mysql.sock >/tmp/alldb.sql
    grep GTID_PURGED /tmp/alldb.sql


    scp /tmp/alldb.sql user@10.10102.213:/tmp

    --从服务器上执行
    -- 执行reset master,重置从服务器上的binlog
    Slave> reset master;
    Query OK, 0 rows affected (0.03 sec)

    Slave> source /tmp/alldb.sql

    Slave>
    CHANGE MASTER TO MASTER_HOST='10.10.227.213', MASTER_USER='resync',Master_Port=3306, MASTER_PASSWORD='Resync$123', MASTER_AUTO_POSITION=1;

    start slave;


    #### 指定位置,仍然报错:
    change master to master_auto_position=0;
    CHANGE MASTER TO master_host='10.10.227.213',master_port=3306,master_user='resync',master_password='Resync$123',master_log_file='mysql-bin.000010',master_log_pos=194;

    https://www.cnblogs.com/lazyball/p/8927556.html

    ######为了验证以上问题是网络问题,主库仍然在10.10.227.211, 在10.10.227.212 搭建一个从库,测试。目录是/db/mysql, 使用的是本地盘/

    mysql -uroot -pmysql --socket=/db/mysql/data/3306/mysqltmp/mysql.sock

    证明不是网路问题。


    ####### 最终问题,个人感觉出在server_id 这个环境变量一定要在【mysqld】目录下,不然就无法生效
    use mysqlbackup to 搭建从库

    expect <<- EOF
    set timeout 5
    spawn sftp -P 22 user@10.10.10.187

    expect {
    "(yes/no)?" {send "yes "; expect_continue }
    "*assword:" {send "CR@zh123 "}
    }
    expect "sftp>"
    send "cd /dbsoft/mysql "
    expect "sftp>"
    send "lcd /tmp/soft "
    expect "sftp>"
    set timeout -1
    send "get mysqlbackup_5.7 "
    expect "sftp>"
    send "bye "
    EOF

    cd /tmp/soft
    cp mysqlbackup_5.7 mysqlbackup
    chmod 755 mysqlbackup
    cp mysqlbackup /usr/bin/mysqlbackup

    mkdir /db/mysql/bak
    chown mysql:mysql /db/mysql/bak
    su - mysql

    mysqlbackup -uroot -p --socket=/db/mysql/data/3306/mysqltmp/mysql.sock --backup-dir=/db/mysql/bak --backup-image=/db/mysql/bak/mybackup.mbi --compress backup-to-image

    scp -rp * user@10.10102.213:/db/mysql/bak

    source done: (10).

    target bgegin: (58)

    expect <<- EOF
    set timeout 5
    spawn sftp -P 22 user@56.18.99.206

    expect {
    "(yes/no)?" {send "yes "; expect_continue }
    "*assword:" {send "CR!zh1234 "}
    }
    expect "sftp>"
    send "cd /dbsoft/mysql "
    expect "sftp>"
    send "lcd /tmp/soft "
    expect "sftp>"
    set timeout -1
    send "get mysqlbackup_5.7 "
    expect "sftp>"
    send "bye "
    EOF


    cd /tmp/soft
    cp mysqlbackup_5.7 mysqlbackup
    chmod 755 mysqlbackup
    cp mysqlbackup /usr/bin/mysqlbackup

    chown -R mysql:mysql /db/mysql/bak

    export mybackdir_full=/db/mysql/bak
    mysqlbackup --defaults-file=$mybackdir_full/server-my.cnf --datadir=/db/mysql/data/3306/mydata --backup_image=$mybackdir_full/mybackup.mbi --backup_dir=$mybackdir_full copy-back-and-apply-log --uncompress

    cp /tmp/soft/my.cnf /db/mysql/data/3306/my.cnf


    运行$mybackdir_full/meta/backup_gtid_executed.sql重置gtid_purged

    --@/db/rtcs/mysql/bak/meta/backup_gtid_executed.sql

    SET @@GLOBAL.GTID_PURGED='52714855-1870-11e8-bce2-005056bd438d:1-3';


    注意:如果碰到 在mysql用sql文件导入数据库时,提示ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.
    fix:
    mysql> reset master;

    mysql> CHANGE MASTER TO MASTER_HOST='10.10.227.213', MASTER_USER='resync',Master_Port=3306, MASTER_PASSWORD='Resync$123', MASTER_AUTO_POSITION=1;
    Query OK, 0 rows affected, 2 warnings (0.02 sec)

    mysql> START SLAVE;
    Query OK, 0 rows affected (0.00 sec)

    mysql> show slave status G;


    SOUCE: (10)
    create table t2(id int,ename varchar(20));
    insert into t2 values(1,'leshami');

    ###问题2


    #######
    碰到问题1: 以下是在使用percona template 结合 zabbix 部署监控时候,发现的问题。


    添加 percona mysql server multiport template 这个模板后,发现在
    http://10.10.10.217/zabbix/host_discovery.php?hostid=12503&g_hostdruleid[]=426601&action=discoveryrule.massenable

    自动发现规则 的 键值 "MySQL.discovery"是 不支持的

    原因:

    检查1 正常
    [zabbix@sesbdb06 tmp]$ sh /usr/local/zabbix/bin/mysql_low_discovery.sh
    {
    "data":[
    {
    "{#MYSQLPORT}":"3306"}
    ]
    }

    检查2 有问题:
    zabbix 用户下执行有问题
    [zabbix@sesbdb06 tmp]$ /usr/local/zabbix/bin/get_mysql_stats_wrapper.sh kt 3306
    PHP Notice: Undefined offset: 9 in /usr/local/zabbix/bin/ss_get_mysql_stats.php on line 1047
    24667


    检查 /usr/local/zabbix/bin/ss_get_mysql_stats.php 文件

    elseif (strpos($line, 'ibuf aio reads') === 0 ) {
    # ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
    $results['pending_ibuf_aio_reads'] = to_int($row[3]);
    $results['pending_aio_log_ios'] = to_int($row[6]);
    -line 1047》 $results['pending_aio_sync_ios'] = to_int($row[9]);
    }


    ##调试过程:

    有问题机器:
    su - zabbix
    cd /tmp
    rm10.10102.213-mysql_cacti_stats.txt

    然后屏幕输出日志到一个windows 桌免文件以日志,再次执行以下脚本,就会打印很多调试信息
    /usr/bin/php -q /usr/local/zabbix/bin/ss_get_mysql_stats.php --host10.10102.213 --items gg --port 3306

    Pending normal aio reads: [0, 0, 0, 0] , aio writes: [0, 0, 0, 0] ,
    ibuf aio reads:, log i/o's:, sync i/o's:

    找到这一段:
    -》 ibuf aio reads:, log i/o's:, sync i/o's:

    -> 找到一个bug ,就是mysql 5.7 版本以后 SHOW ENGINE INNODB STATUSG, 都会找不到这个选项
    -》https://bugs.mysql.com/bug.php?id=94441 Bug #94441 empty ibuf aio reads in innodb status Version: 5.7.25, 8.0.15


    ##sh -x /usr/local/zabbix/bin/get_mysql_stats_wrapper.sh kt 3306

    ##感谢noname,libove 提供的帮助
    ##https://forums.cacti.net/viewtopic.php?t=46418

    #修改1 /usr/local/zabbix/sbin/zabbix_agentd
    maark屏蔽 1045 to 1047 : add belows;
    $results['pending_ibuf_aio_reads'] = 0;
    $results['pending_aio_log_ios'] = 0;
    $results['pending_aio_sync_ios'] = 0;

    修改2:(自动发现规则应该是重启client 10分钟后生效)
    pkill zabbix
    重启zabbix agent

    /usr/local/zabbix/sbin/zabbix_agentd


    验证1:
    ##通过zabbix app2分别连接 ip 和vip 状态是正常的。有端口号显示,应该问题就不大。
    zabbix_get -s10.10102.212 -p 10050 -k "MySQL.discovery"
    zabbix_get -s10.10102.213 -p 10050 -k "MySQL.discovery"
    "{#MYSQLPORT}":"3306"}

    ####

    以下是通过脚本tar 包 克隆mysql 所碰到的问题。

    #################
    问题1:server_id, bind_address 插入到my.cnf 最尾巴部,确发现该参数修改 无法生效。

    原因如下:
    my.cnf 分为如下几项:[mysqld] [mysql] [client] 3个部分。服务器部分放[mysqld],客户端部分放 [mysql] [client]
    MySQL配置文件,例如/etc/my.cnf,具有许多不同的节标题,包括[mysql],[mysqld],[mysqld_safe]。确保将正确的变量放入正确的部分非常重要。不然
    my.cnf配置文件将无法按用户期望的方式运行。

    确保了解您的my.cnf [部分]
    2010年1月26日由 感谢http://ronaldbradford.com/罗纳德


    例如,以下my.cnf配置文件将无法按用户期望的方式运行。

    [mysqld]
    ...
    log-bin=mysql-bin
    server-id=1
    query_cache_size = 100M
    query_cache_type = 1

    ...

    [mysqld_safe]
    ...
    key_buffer_size=600M
    skip-innodb
    ...


    在此示例中,此配置没有为您提供600M的MyISAM密钥缓冲区,实际上是默认值8M。

    mysql> show global variables like 'key_buffer_size';
    +-----------------+---------+
    | Variable_name | Value |
    +-----------------+---------+
    | key_buffer_size | 8388600 |
    +-----------------+---------+
    + ----------------- + --------- +
    确保将正确的选项添加到[mysqld]部分。

    直到昨天我才知道某些程序是从多个组读取的。从5.1.2开始。服务器命令选项 MySQL参考手册页。在帮助读者描述问题的过程中,我实际上学到了一些新知识。


    mysqld从[mysqld]和[server]组中读取选项。mysqld_safe从[mysqld],[server],[mysqld_safe]和[safe_mysqld]组中读取选项。
    mysql.server从[mysqld]和[mysql.server]组读取选项。
    例如,我总是将log-error放在[mysqld_safe]和[mysql] d部分中,因为这两个都写不同的错误。似乎没有必要。


    我们启动mysql 命令如下: mysqld_safe ,所以读取文件为 mysqld_safe从[mysqld],[server],[mysqld_safe]和[safe_mysqld]组中读取选项。
    /db/mysql/app/mysql/bin/mysqld_safe --defaults-file=/db/mysql/data/3306/my.cnf &


    ##感谢Derek Jan
    The [client] option group already sets options for all MySQL clients that read the my.cnf file.
    The [mysql] group sets options for just the "mysql" client binary, specifically.
    If you want to set a mysql option that does not apply to other clients, using this group is appropriate,
    but setting the same option value in both locations is redundant. dev.mysql.com/doc/refman/5.6/en/option-files.html

    – Derek Jan 13 '15 at 14:39

    #################
    问题2:修改参数 max_connections 都没有生效是因为操作系统如下参数没有调整


    原因:
    mysql hard nofile 65535
    mysql soft nofile 65535


    解决办法:
    --os 参数如下:
    --"mysql hard nofile 65535"
    --"mysql soft nofile 65535" /etc/security/limits.conf


    #################
    问题3:clone 出来的mysql data 目录,启动时候没问题,但是同步有问题,

    cp /tmp/soft/mydata_5.7_data.tar.gz .
    tar -zxvf mydata_5.7_data.tar.gz


    原因如下:

    server_uuid clone 出来,主库和备库一样,导致该问题
    mysql> show variables like '%UU%';
    +---------------+--------------------------------------+
    | Variable_name | Value |
    +---------------+--------------------------------------+
    | server_uuid | 3fb79513-ad27-11ea-9c1f-b496915975e4 |
    +---------------+--------------------------------------+
    1 row in set (0.00 sec)


    解决办法,
    清理掉--datadir 下的auto.cnf,再次启动mysql 会重新生成该文件
    cd /db/mysql/data/3306/mydata
    rm auto.cnf


    #################
    问题4: 搭建Mysql 从库时候,启动从库时候一直报错如下:

    Slave_IO_Running: No
    Slave_SQL_Running: Yes

    检查error log 如下:
    2020-06-13T10:33:27.237296Z 6 [Note] Slave I/O thread for channel '': connected to master 'resync@10.198.227.213:3306',replication started in log 'FIRST' at position 4

    原因:

    问题未定,可能出在如下几个方面

    最主要的原因如下:

    1.主库和备库的 server_id 一样,
    mysql> show variables like '%SERVER_ID%';
    +----------------+------------+
    | Variable_name | Value |
    +----------------+------------+
    | server_id | 4294967295 |

    ##不能echo server_id 到文件尾巴,只能sed 替换,否则数据库数据库无法启动成功 或者不能正确读取这个配置文件,并且这个参数必须在{mysqld} 子目录下
    ###echo "server_id=$server_id" >> /db/mysql/data/3306/my.cnf


    2.在这里,重复下,我们搭建过程,

    因为这次使用的是自动化脚本部署,而不是普通的安装。

    2.1 首先我们是按照空库搭建了一个主库,然后clone 主库到备库(copy 文件的方式),然后建议同步用户,开启同步。报错Slave_IO_Running: No
    (感觉这种方法应该也可以,参考官方文档 https://dev.mysql.com/doc/refman/8.0/en/replication-setup-slaves.html#replication-howto-newservers)


    2.2 然后我们怀疑这种搭建方式有问题,参考Leshami 的文档 基于mysqldump搭建gtid主从 https://blog.csdn.net/leshami/article/details/52755472
    使用mysqldump 方式搭建一个从库,还是仍然报错。,启动从库,报错Slave_IO_Running: No


    2.3 接着怀疑是网路问题,在本地同城机房,搭建了一个从库,启动从库,仍然报错Slave_IO_Running: No


    2.4 最后使用之前mysqlbackup 手工备份,恢复,一步步来,测试,发现问题 可能出现在 主库和备库的 server_id 一样,没生效有关,


    解决办法:

    生成一个新的server_id 到 my.cnf 的[mysqld]下,再次重启生效,就可以了继续下面搭建工作,没有报错了。


    ############
    问题5:

    注意:如果碰到 在mysql用sql文件导入数据库时,提示ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.

    fix:
    mysql> reset master;

    #######
    问题6 show slave statuS 报错 Error 'Unknown error 1146' on query. Default database:

    mysql> show slave statuSG
    *************************** 1. row ***************************
    Slave_IO_State: Waiting for master to send event
    Master_Host: 10.198.227.213
    Master_User: resync
    Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: mysql-bin.000013
    Read_Master_Log_Pos: 173918456
    Relay_Log_File: sesbdb06-relay-bin.000005
    Relay_Log_Pos: 125990063
    Relay_Master_Log_File: mysql-bin.000013
    Slave_IO_Running: Yes
    Slave_SQL_Running: No
    Replicate_Do_DB:
    Replicate_Ignore_DB:
    Replicate_Do_Table:
    Replicate_Ignore_Table:
    Replicate_Wild_Do_Table:
    Replicate_Wild_Ignore_Table:
    Last_Errno: 1146
    Last_Error: Error 'Unknown error 1146' on query. Default database: 'esbmondb'. Query: 'insert into his_esb_rsm_status_ms (serv_name,tm_status,mon_time) values ('ESB003','run','202006161033')'

    原因:
    在容灾库没有打开只读选项。
    从库设置为read-only
    show variables like 'read_only';
    调整前状态
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | read_only | OFF |
    +---------------+-------+

    调整为只读
    set global read_only=on;

    fxied:


    restart slave

    select count(*) from esbmondb.his_esb_rsm_status_ms;


    select * from esbmondb.his_esb_rsm_status_ms where mon_time='202006161033';

    in master:
    mysql> select count(*) from esbmondb.his_esb_rsm_status_ms;
    +----------+
    | count(*) |
    +----------+
    | 16827 |
    +----------+
    1 row in set (0.00 sec)

    in standby:
    mysql> select count(*) from esbmondb.his_esb_rsm_status_ms;
    +----------+
    | count(*) |
    +----------+
    | 8961 |
    +----------+
    1 row in set (0.00 sec)


    ##############
    问题6:
    mysql5.7日志时间与系统时间不一致

    估计要按照这篇文档介绍的一样,重启数据库才可以。

    fix:
    -〉因此需要在mysql的配置文件中[mysqld]中增加一条log_timestamps的配置

    log_timestamps=SYSTEM


    参考文档:

    https://forums.percona.com/discussion/11842/backup-stopped-working-slave-sql-running-no
    Home› MySQL & MariaDB› Percona XtraBackup
    Backup stopped working !! Slave_SQL_Running: No
    systemalisystemali Mentor October 2013
    Hello everyone,

    My latest issues are just piling up and i am not able to get them all sorted :(

    To add to my chaos, now my backup server too is giving me issues.

    When i run the command :-
    TheSlave|mysql> SHOW SLAVE STATUS G
    ...
    Slave_IO_Running: Yes
    Slave_SQL_Running: No

    see that "Slave_SQL_Running" is "NO" !!

    i tried to stop and start the replication service, but in vein :(

    My log files are throwing up this error :-
    tail -f /var/log/messages

    Oct 8 13:32:37 DatabackupSRV xinetd[3396]: EXIT: nrpe status=0 pid=3295 duration=0(sec)
    Oct 8 13:34:19 DatabackupSRV xinetd[3396]: START: nrpe pid=3300 from=10.222.32.22
    Oct 8 13:34:19 DatabackupSRV xinetd[3396]: EXIT: nrpe status=0 pid=3300 duration=0(sec)
    Oct 8 13:34:29 DatabackupSRV xinetd[3396]: START: nrpe pid=3305 from=10.222.32.22
    Oct 8 13:34:29 DatabackupSRV xinetd[3396]: EXIT: nrpe status=0 pid=3305 duration=0(sec)

    It was working all this while, But i guess the recent start / stop of mysql services and the server of the master is causing this issue.

    Any suggestions to rectify this would be of great help.

    Thank you
    Comments
    systemalisystemali Mentor October 2013
    Thought for a moment and decided why not post the whole output of the command : SHOW SLAVE STATUS G
    mysql> SHOW SLAVE STATUS G
    *************************** 1. row ***************************
    Slave_IO_State: Waiting for master to send event
    Master_Host: 10.222.1.218
    Master_User: root
    Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: newcrmdb1-bin.000061
    Read_Master_Log_Pos: 315098143
    Relay_Log_File: DatabackupSRV-relay-bin.000088
    Relay_Log_Pos: 667796113
    Relay_Master_Log_File: newcrmdb1-bin.000054
    Slave_IO_Running: Yes
    Slave_SQL_Running: No
    Replicate_Do_DB:
    Replicate_Ignore_DB:
    Replicate_Do_Table:
    Replicate_Ignore_Table:
    Replicate_Wild_Do_Table:
    Replicate_Wild_Ignore_Table:
    Last_Errno: 1146
    Last_Error: Error 'Table 'asteriskcdr.bpleadcf' doesn't exist' on query. Default database: '
    newcrmdb'. Query: 'INSERT INTO `newcrmdb`.`bpleadcf` SELECT * FROM `asteriskcdr`.`bpleadcf`'
    Skip_Counter: 0
    Exec_Master_Log_Pos: 667795964
    Relay_Log_Space: 1134162270
    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: NULL
    Master_SSL_Verify_Server_Cert: No
    Last_IO_Errno: 0
    Last_IO_Error:
    Last_SQL_Errno: 1146
    Last_SQL_Error: Error 'Table 'asteriskcdr.bpleadcf' doesn't exist' on query. Default database: '
    newcrmdb'. Query: 'INSERT INTO `newcrmdb`.`bpleadcf` SELECT * FROM `asteriskcdr`.`bpleadcf`'
    1 row in set (0.00 sec)


    Thank you everyone for your assistance.
    systemalisystemali Mentor October 2013
    Ahhh...

    With regards to my similar post earlier, I tried to trouble shoot the issue, but in vein...

    Now when i run the command :- "show slave status G"
    mysql> show slave status G
    *************************** 1. row ***************************
    Slave_IO_State:
    Master_Host: 10.222.1.218
    Master_User: root
    Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: mysql-bin.000055
    Read_Master_Log_Pos: 315098143
    Relay_Log_File: DatabackupSRV-relay-bin.000001
    Relay_Log_Pos: 4
    Relay_Master_Log_File: mysql-bin.000055
    Slave_IO_Running: No
    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: 315098143
    Relay_Log_Space: 106
    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: NULL
    Master_SSL_Verify_Server_Cert: No
    Last_IO_Errno: 1236
    Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not f
    ind first log file name in binary log index file'
    Last_SQL_Errno: 0
    Last_SQL_Error:
    1 row in set (0.00 sec)


    I'll let everyone know, what i did :-

    This is the first change i did :-
    mysql> change master to MASTER_LOG_FILE='mysql-bin.000055', Master_Log_Pos=4;
    Later i changed to this :-
    mysql> change master to MASTER_LOG_FILE='mysql-bin.000055', Master_Log_Pos=315098143;

    Those are the 2 chnages i have done and now i get the status as shown above :(

    i hope i have not messed it up too much !!


    Thank you
    scott.nemesscott.nemes Advisor October 2013
    Yeah you stuck it to yourself on this one. ;)

    The first issue is that you changed the replication position instead of fixing the error, and used an incorrect binlog file name format (you likely just used the one from that post you linked I'd guess). To get back to where you started, you need to find the binlog file and position that the slave sql_thread stopped at. Based on your slave status output, it looks like the slave is reading from a new binlog file (you can see that the Read_Master_Log_Pos value is smaller than the Exec_Master_Log_Pos value, which means it has to be reading a newer binlog file than where the slave sql_thread stopped at), so you need to find the binlog file that the slave sql_thread actually failed on. So look in the error log for something like the below:
    2013-10-08 12:48:51 37545 [ERROR] Slave SQL: Error 'Table 'testdb.test2' doesn't exist' on query. Default database: 'testdb'. Query: 'insert into test1 select * from test2', Error_code: 1146
    2013-10-08 12:48:51 37545 [Warning] Slave: Table 'testdb.test2' doesn't exist Error_code: 1146
    2013-10-08 12:48:51 37545 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'mysql-bin.000001' position 3427

    This is a sample I re-created, so yours will be a bit different. Note the ERROR is similar to what you see in your slave status. So find your specific error message in the error log file, and then locate the end part where is gives you the file name and position ("We stopped at log 'mysql-bin.000001' position 3427" in my example). The position should be 315098143 based on your show slave status, as that is when it the slave sql_thread stopped executing events (Exec_Master_Log_Pos ) but the io_thread kept reading in new ones (Read_Master_Log_Pos).

    Once you find the correct binlog file name and position, re-run your change master statement on your slave using the information you located in the error log. Note that your file name should be something like "newcrmdb1-bin.XXXXXX", not mysql-bin.XXXXXX (you can see this naming convention your show slave status above).
    mysql> change master to MASTER_LOG_FILE='newcrmdb1-bin.XXXXXX', Master_Log_Pos=315098143;

    Once you get pointed back to the original replication location where the slave sql_thread failed, you need to then fix the error that it was complaining about to start with.

    The initial replication error appears to be telling you that the table `asteriskcdr`.`bpleadcf` does not exist on the slave, so the insert statement is failing when it attempts to select the data from that table. So the problem there is that your slave appears to be already out of sync with your master. If the table in question on the master is static or mostly static, you could likely solve this by exporting the data from just that table on the master using mysqldump and loading it into the slave. If that is not possible, or you do not care about that data, you could always just skip the replication statement with sql_slave_skip_counter, but then the slave would be further out of sync with the master.

    And if all else fails, you can always rebuild the slave from the master as a last resort as well. =)
    systemalisystemali Mentor October 2013
    Hello Scott,

    Phewww.....My apologies for causing you such a pain :(, but none the less the above explanation was very very useful and i did learn a few things extra :)

    But, I could not salvage the data from its last position and hence i had to redo the whole replication stuff once again, which i have just completed :D

    All thanks to your guidance ...

    Keep it up !!!

    Gnite :)
    scott.nemesscott.nemes Advisor October 2013
    Glad you got it going again anyway!

    If you want some practice, I'd recommend downloading MySQL Sandbox and setting up a quick replication environment on a test server. You can easily test situations like this, and practice recovering from them.

    I.e. in your test environment:

    1. Drop a table on the slave (not the master)
    2. Do a "insert into ... select from" statement on the master that uses the table you dropped on the slave in the FROM part (like your actual issue above)
    3. Use mysqldump to dump the missing table from the master and insert it on the slave
    4. Restart replication, which should then work now that the correct table exists again on the slave

    MySQL Sandbox:
    https://launchpad.net/mysql-sandbox/...-3.0.42.tar.gz

    Guide for installation / setup:
    http://search.cpan.org/~gmax/MySQL-S...box/Recipes.pm
    systemalisystemali Mentor October 2013
    Thank you so much for the head'sup Scott..

    You have been a life saver !!


    #######

  • 相关阅读:
    重构技巧 引入Null对象
    python yield
    todo
    Python 函数式编程学习
    Python 修饰器
    socket
    Exception、RuntimeException
    设计模式
    线程池
    VMware异常关闭后再次启动提示“以独占方式锁定此配置文件失败”!!!
  • 原文地址:https://www.cnblogs.com/feiyun8616/p/13161540.html
Copyright © 2011-2022 走看看