zoukankan      html  css  js  c++  java
  • CDH的mysql主从准备

    参考:

    https://www.cnblogs.com/yinzhengjie/p/10371899.html

    https://www.sysit.cn/blog/post/sysit/CDH6.2.0%E7%B3%BB%E7%BB%9F%E9%83%A8%E7%BD%B2%E6%89%8B%E5%86%8C

    搭建主库: CM节点

    mysql> status;

    Server version:5.7.27 MySQL Community Server (GPL)

    #配置源

    cat > /etc/yum.repos.d/mysql-community-el7.repo<<'EOF'

    [mysql57-community]

    name=MySQL 5.7 Community Server

    baseurl=http://mirror.tuna.tsinghua.edu.cn/mysql/yum/mysql57-community-el7/

    enabled=1

    gpgcheck=0

    EOF

    # 安装

    yum install mysql-community-server -y

    echo "character-set-server=utf8" >>/etc/my.cnf

    systemctl restart mysqld

    # mysqld.log中查询密码

    grep password /var/log/mysqld.log 

    2019-04-09T07:28:44.292450Z 1 [Note] A temporary password is generated for root@localhost: wg:V45ci4ljpz!r

    #注意,安装CDH最好用官方推进的my.cnf文件,见下附录.

    # 测试环境关闭强策略密码,生产环境建议开启。不关闭只支持mysql -uslave -P 3306 -h10.52.2.128 -p的登陆方式

    echo "validate-password=off" >>/etc/my.cnf

    echo "server-id=101"  >> /etc/my.cnf

    echo "log-bin=mysql-bin" >>  /etc/my.cnf                 #开启日志做主从,不然show master status时会是空的.

    # 重启mysql

    systemctl restart mysqld

    # 重置安全配置

    mysql_secure_installation

    # 登录mysql

    mysql -uroot -p

    #此时需要用到上面的密码

    #登陆后修改成自己的密码:

    mysql> alter user user() identified by 'test123456';

    #检查

    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock

    symbolic-links=0

    log-error=/var/log/mysqld.log
    #pid-file=/var/run/mysqld/mysqld.pid
    character-set-server=utf8
    validate-password=off
    server-id=101

    #查看当前服务器的server_id

    mysql> show variables like '%server_id%';

    #查bin_log是否开启

    mysql> show variables like '%log_bin%';
    +---------------------------------+-------+
    | Variable_name | Value |
    +---------------------------------+-------+
    | log_bin | ON
    ---------------------

     #查看授权情况

    select user,host from mysql.user;

    附录:cloudera官方推荐

    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    transaction-isolation = READ-COMMITTED
    # Disabling symbolic-links is recommended to prevent assorted security risks;
    # to do so, uncomment this line:
    symbolic-links = 0

    key_buffer_size = 32M
    max_allowed_packet = 32M
    thread_stack = 256K
    thread_cache_size = 64
    query_cache_limit = 8M
    query_cache_size = 64M
    query_cache_type = 1

    max_connections = 550
    #expire_logs_days = 10
    #max_binlog_size = 100M

    #log_bin should be on a disk with enough free space.
    #Replace '/var/lib/mysql/mysql_binary_log' with an appropriate path for your
    #system and chown the specified folder to the mysql user.
    log_bin=/var/lib/mysql/mysql_binary_log                 #开启bin_log

    #In later versions of MySQL, if you enable the binary log and do not set
    #a server_id, MySQL will not start. The server_id must be unique within
    #the replicating group.
    server_id=1

    binlog_format = mixed

    read_buffer_size = 2M
    read_rnd_buffer_size = 16M
    sort_buffer_size = 8M
    join_buffer_size = 8M

    # InnoDB settings
    innodb_file_per_table = 1
    innodb_flush_log_at_trx_commit = 2
    innodb_log_buffer_size = 64M
    innodb_buffer_pool_size = 4G
    innodb_thread_concurrency = 8
    innodb_flush_method = O_DIRECT
    innodb_log_file_size = 512M

    [mysqld_safe]
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid

    sql_mode=STRICT_ALL_TABLES

    在另一个节点上搭建从库:

    操作同上, server-id=102要不同

     mysql> show variables like '%server_id%';

    配置主从同步:

    #在主库上授权copy帐号用于复制,密码是fengfeng99
    [root@node106.fengfeng.org.cn ~]# mysql -uroot -p

    mysql> CREATE USER 'copy'@'195.189.142.83%' IDENTIFIED BY 'fengfeng99';
    Query OK, 0 rows affected (0.01 sec)


    mysql> GRANT REPLICATION SLAVE ON *.* TO 'copy'@'195.189.142.83%';
    Query OK, 0 rows affected (0.00 sec)

    mysql> quit

    检查:
    #在从库节点上用copy用户远程:
    mysql -u copy -pfengfeng99 -P 3306 -h195.189.142.89
    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

    mysql> show databases;
    +--------------------+
    | Database |
    +--------------------+
    | information_schema |
    +--------------------+
    1 row in set (0.00 sec)

    #获取主库的日志信息并生成主库数据镜像,主库上:

    mysql> FLUSH TABLES WITH READ LOCK;                  #对主库上所有表加锁,停止修改,即在从库复制的过程中主库不能执行UPDATA,DELETE,INSERT语句!
    Query OK, 0 rows affected (0.00 sec)

    mysql> SHOW MASTER STATUS;                    #获取主库的日志信息,file表示当前日志文件名称,position表示当前日志的位置
    mysql> SHOW MASTER STATUS;
    +------------------+----------+--------------+------------------+-------------------+
    | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +------------------+----------+--------------+------------------+-------------------+
    | mysql-bin.000001 | 154 | | | |
    +------------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)
    mysql> quit

    [root@zhep-opay-temp-1 ~]# mysqldump --all-databases --master-data -u root -pfengfeng99 -P 3306 > fengfeng-master.db  #另开一个终端生成镜像,在生成完成之前不要释放锁。
    mysqldump: [Warning] Using a password on the command line interface can be insecure.
    [root@zhep-opay-temp-1 ~]# ls
    anaconda-ks.cfg fengfeng-master.db original-ks.cfg test.log


    mysql> UNLOCK TABLES;      #主库数据生成镜像完毕后,我们需要把主库的锁释放掉,需要注意的是,在上锁这一段期间,我们无法对数据库进行写操作,比如UPDATA,DELETE,INSERT。
    Query OK, 0 rows affected (0.00 sec)

    将主库的镜像拷贝到从库服务器,让从库应用主库镜像
    [root@zhep-opay-temp-1 ~]# scp fengfeng-master.db 195.189.142.83:~
    fengfeng-master.db

    登陆从库:

    mysql> source fengfeng-master.db;
    Query OK, 0 rows affected (0.00 sec)

    Query OK, 0 rows affected (0.00 sec)

    Query OK, 0 rows affected (0.00 sec)

    Query OK, 0 rows affected (0.00 sec)

    #在从库上建立复制关系,即从库指定主库的日志信息和链接信息
    mysql>
    CHANGE MASTER TO
    MASTER_HOST='195.189.142.89',
    MASTER_PORT=3306,
    MASTER_USER='copy',
    MASTER_PASSWORD='fengfeng99',
    MASTER_LOG_FILE='mysql-bin.000001',
    MASTER_LOG_POS=154;

    #MASTER_LOG_FILE是上面SHOW MASTER STATUS里的file, POS里面的position

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

    mysql>
    mysql> SHOW SLAVE STATUSG
    *************************** 1. row ***************************
    Slave_IO_State: Waiting for master to send event
    Master_Host: node106.fengfeng.org.cn
    Master_User: copy
    Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: fengfeng-mysql-bin.000002
    Read_Master_Log_Pos: 4095
    Relay_Log_File: node107-relay-bin.000002
    Relay_Log_Pos: 332
    Relay_Master_Log_File: fengfeng-mysql-bin.000002
    Slave_IO_Running: Yes     #观察IO进程是否为yes,如果为yes说明正常,如果长时间处于"Connecting"状态就得检查你的从库指定的主库的链接信息是否正确
    Slave_SQL_Running: Yes    #观察SQL进程是否为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: 4095
    Relay_Log_Space: 541
    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   #该参数表示从库和主库有多少秒的延迟,咱们可以理解为再过多少秒数据和主库保持一致,如果为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: 106
    Master_UUID: 74227047-8b60-11e9-8cba-000c29985293
    Master_Info_File: /fengfeng/softwares/mysql/data/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:
    Executed_Gtid_Set:
    Auto_Position: 0
    Replicate_Rewrite_DB:
    Channel_Name:
    Master_TLS_Version:
    1 row in set (0.00 sec)

    #测试主从是否同步:
    主库上建一个test库
    mysql> CREATE DATABASE test DEFAULT CHARACTER SET = utf8;
    Query OK, 1 row affected (1.82 sec)


    mysql> show databases;
    +--------------------+
    | Database |
    +--------------------+
    | information_schema |
    | mysql |
    | performance_schema |
    | sys |
    | test |
    +--------------------+
    5 rows in set (0.00 sec)

    检查从库是否同步:
    mysql> show databases;
    +--------------------+
    | Database |
    +--------------------+
    | information_schema |
    | mysql |
    | performance_schema |
    | sys |
    | test |
    +--------------------+
    5 rows in set (0.00 sec)

    #也可在主库上建一张表,看是否同步:

    create table student(
    id int(4) not null AUTO_INCREMENT,
    name char(20) not null,
    age tinyint(2) NOT NULL default '0',
    dept varchar(16) default NULL,
    primary key(id),
    KEY index_name (name)
    );

    #插入数据

    insert into student(id,name) values(1,'oldboy');

    #注:

    锁库创建从库:即时在主库上拿一个完整备份,并恢复到新建的从库,优点:可以支持非事务引擎,
    缺点:较慢,影响主库使用。
    不锁库创建从库:拿主库最近一次可用的历史完整备份,先导数据,再追日志,优点:较为快速,不影响主库,缺点:不支持非事务引擎。

    排错:

    如果主从不同步.其它都不靠谱,靠谱的是在从库上stop slave; 按上面的方法重做一次.

    可以先看错误日志,在: tail -1000 /var/log/mysqld.log

    binglog的位置在my.cnf中的:log_bin=/var/lib/mysq

    mysql> SHOW BINARY LOGS;
    +-------------------------+-----------+
    | Log_name | File_size |
    +-------------------------+-----------+
    | mysql_binary_log.000001 | 181956511 |
    | mysql_binary_log.000002 | 526878457 |
    | mysql_binary_log.000003 | 2055720 |
    +-------------------------+-----------+
    3 rows in set (0.00 sec)

    #把从库设置成readonly

    mysql> show global variables like "%read_only%";
    +-----------------------+-------+
    | Variable_name | Value |
    +-----------------------+-------+
    | innodb_read_only | OFF |
    | read_only | OFF |
    | super_read_only | OFF |
    | transaction_read_only | OFF |
    | tx_read_only | OFF |
    +-----------------------+-------+
    对于需要保证master-slave主从同步的salve库,如果要设置为只读状态,需要执行的命令为:
    mysql> set global read_only=1;

    将salve库从只读状态变为读写状态,需要执行的命令是:
    mysql> set global read_only=0;

    grant all on *.* to webdev@'localhost' identified by 'Test12345@';
    grant select on *.* to webdev2@'localhost' identified by 'Test12345@';
    grant select on *.* to webdev2@'%' identified by 'Test12345@';

  • 相关阅读:
    maxsdk sample中3dsexp.rc点不开并提示specstrings.h中找不到sal.h解法
    安装max plugin wizard
    max plugin wizard,project creation faild解法
    unity, 由5.2.1f1升级到5.3.5f1,2d物理不正常解法
    maxscript,#号和$号
    maxscript,执行选中代码片段
    maxscript,MAXScript Listener下输入print "hi"为什么输出两次
    editplus查找替换的正则表达式应用
    命令行创建畸形文件夹+畸形目录管理工具(DeformityPath)
    费曼技巧——高速学习的方法
  • 原文地址:https://www.cnblogs.com/hongfeng2019/p/11279488.html
Copyright © 2011-2022 走看看