新建slave

如有漏洞或者潜在的危险,请拍砖指出,谢谢

针对innodb引擎,不考虑其他引擎(myisam等)

【master环境】
开启了binlog
配置server-id
配置自增属性(为双主准备)auto_increment_offset=1、auto_increment_increment=2

【slave 环境】
my.cnf 配置server_id

点击(此处)折叠或打开

  1. [mysqld]
  2. # 不能和主库或其他slave重复
  3. server-id = xx

授权同步帐号、密码

点击(此处)折叠或打开

  1. CREATE USER repl_user IDENTIFIED BY \'repl_pwd\';
  2. GRANT SELECT, REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO \'repl_user\'@\'%\' IDENTIFIED BY \'repl_pwd\';
  3. FLUSH PRIVILEGES;


一: Master下没有slave, 新建slave
    A: MASTER 不可以停写 
        方案一: innobackupex全备 + slave-info
        [主库]
        # 全备. my.cnf必须配置datadir参数
        innobackupex --defaults-file=my.cnf路径 --user=用户名 --password=密码 --no-timestamp --slave-info --sleep=10 --stream=tar 备份路径 2>备份日志 1>压缩档.tar
        --slave-info 输出master status信息,备份路径下xtrabackup_binlog_info文件
        --sleep  每备份 1M 数据,过程停止拷贝多少毫秒
        --stream=tar tar压缩

         [从库]
        # 拷贝并解压
        tar -ixvf 压缩档.tar -C 解压目录(slave的datadir)
        # 设置同步信息:master host、用户、密码、端口、binlog、开始位置。binlog信息结合xtrabackup_binlog_info文件内容
        STOP SLAVE;

        change master to master_host='',master_user='',master_password='',master_port='',master_log_file='',master_log_pos='';
        START SLAVE;
                
        方案二: mysqldump全备 + single-transaction + master-data
        缺点:对服务器性能影响较大,其他连接不能有DDL语句
        [主库]
        # 备份。业务压力小的时间进行
        flush logs; # 生成新的binlog文件。方便管理,不执行也可以
        mysqldump -uxx -pxx -AER --single-transaction --master-data > bak.sql

         [从库]
        #导入备份数据
        mysql -uxx -pxx < bak.sql
        或者
        进入mysql命令行;source bak.sql

    B: MASTER 可以停写
         [主库]
        # 停止写入
        flush tables with read lock;
        # 获取binlog位置
        show master status;
        # 对mysql 的datadir目录做快照。这方面知识欠缺,希望同学们提高好方法
            tar http://linuxfun.me/?p=1513
            lvs
        # 释放锁
        unlock tables;

        # 拷贝所有数据(ibdata、表数据、表结构、redo、master info、relay info)到slave
        然后启动 slave
        STOP SLAVE;RESET SLAVE;

        change master to master_host='',master_user='',master_password='',master_port='',master_log_file='',master_log_pos='';
        START SLAVE;

二: Master下已有slave, 新建slave
        # 处理过程中old slave会停止写入。如果不能写入,参考上面"一"的处理方式
         [old slave]
        # 停止写入,但不阻止io 线程
        FLUSH TABLES WITH READ LOCK;
        # 记录Relay_Master_Log_File和Exec_Master_Log_Pos
   
     show slave status;     
        # 对mysql 的datadir目录做快照
            tar http://linuxfun.me/?p=1513
            lvs
        # 释放锁
        unlock tables;

         [new slave]
        # 拷贝old slave 上datadir目录下所有数据(ibdata、表数据、表结构、redo、master info、relay info),但不包含(注意pid和socket到new slave
        然后重新启动new slave
        STOP SLAVE;RESET SLAVE;

        change master to master_host='',master_user='',master_password='',master_port='',master_log_file='',master_log_pos='';
        START SLAVE;
    

参考
http://blog.chinaunix.net/uid-26364035-id-3845609.html

请使用浏览器的分享功能分享到微信等