# MySQL 复制搭建(附mysql自动搭建脚本)

1. 用自动化脚本拉起一个mysql数据库,作为主库

`[root@upright92 mysql]# ./install_mysql.py 3306`

2.  连接到主库上,创建复制用户repl

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

Query OK, 0 rows affected (0.00 sec)

mysql> create user repl identified by '123456';

Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to repl;

Query OK, 0 rows affected (0.00 sec)

```

3.  在主库上加载数据

```

[root@upright92 data]# mysqlslap --user=root --socket=/tmp/mysql3306.sock  --concurrency=10 --number-int-cols=5 --number-char-cols=20 --auto-generate-sql -p --no-drop

```

4. 在主库上备份数据

```

[root@upright92 backup]# mysqldump -uroot -p -S /tmp/mysql3306.sock -A --single-transaction --master-data=2 > full_backup_3306_1351.sql

[root@upright92 backup]# ls -ltr

-rw-r--r-- 1 root root 1201471 Jul 11 13:52 full_backup_3306_1351.sql

```

5. 在从库上拉起mysql数据库,导入主库上的备份文件

```

[root@upright91 mysql]# ./install_mysql.py 3307

[root@upright91 data]# cat error.log |grep pass

2019-07-11T05:56:35.409964Z 1 [Note] A temporary password is generated for root@localhost: NTh1GAby;*_W

[root@upright91 data]# mysql -S /tmp/mysql3307.sock -uroot -p

root@localhost [(none)]>alter user user() identified by '';

root@localhost [(none)]>reset master;  --一定要注意, 需要在从库上reset master

root@localhost [(none)]>source full_backup_3306_1351.sql

```

6. 设置复制

```

change master to  \

master_host='192.168.0.92', \

master_user='repl' , \

master_password='123456', \

master_port=3306 ,\

master_auto_position=1;

start slave;  --启动复制

```

7. 确认复制状态

```

root@localhost [mc]>show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.0.92

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000002

Read_Master_Log_Pos: 2261804

Relay_Log_File: relay-bin.000002

Relay_Log_Pos: 998

Relay_Master_Log_File: mysql-bin.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: 2261804

Relay_Log_Space: 1199

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: 923306

Master_UUID: 6f3cc46a-a38b-11e9-874a-6c92bf2b6cf6

Master_Info_File: /data/mysql/mysql3307/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: 6f3cc46a-a38b-11e9-874a-6c92bf2b6cf6:797-799

Executed_Gtid_Set: 6f3cc46a-a38b-11e9-874a-6c92bf2b6cf6:1-799

Auto_Position: 1

Replicate_Rewrite_DB:

Channel_Name:

Master_TLS_Version:

1 row in set (0.00 sec)

ERROR:

No query specified

```

## 主库config文件

```

#my.cnf

[client]

port = 3306

socket = /tmp/mysql.sock

[mysql]

prompt="\\u@\\h [\\d]>"

no-auto-rehash

[mysqld]

user = mysql

basedir = /usr/local/mysql

datadir = /data/mysql/mysql3306/data

port = 3306

socket = /tmp/mysql3306.sock

event_scheduler = 0

tmpdir = /data/mysql/mysql3306/tmp

#timeout

interactive_timeout = 300

wait_timeout = 300

#character set

character-set-server = utf8

open_files_limit = 65535

max_connections = 100

max_connect_errors = 100000

#lower_case_table_names =1

#logs

log-output=file

slow_query_log = 1

slow_query_log_file = slow.log

log-error = error.log

log_warnings = 2

pid-file = mysql.pid

long_query_time = 1

log-slow-slave-statements = 1

binlog_format = row

server-id = 923306

log-bin = /data/mysql/mysql3306/logs/mysql-bin

binlog_cache_size = 4M

max_binlog_size = 256M

max_binlog_cache_size = 1M

sync_binlog = 0

expire_logs_days = 10

#procedure

log_bin_trust_function_creators=1

#

gtid-mode = on

enforce_gtid_consistency=on

#relay log

skip_slave_start = 1

max_relay_log_size = 128M

relay_log_purge = 1

relay_log_recovery = 1

relay-log=relay-bin

relay-log-index=relay-bin.index

log_slave_updates

#slave-skip-errors=1032,1053,1062

#skip-grant-tables

#buffers & cache

table_open_cache = 2048

table_definition_cache = 2048

table_open_cache = 2048

max_heap_table_size = 96M

sort_buffer_size = 128K

join_buffer_size = 128K

thread_cache_size = 200

query_cache_size = 0

query_cache_type = 0

query_cache_limit = 256K

query_cache_min_res_unit = 512

thread_stack = 192K

tmp_table_size = 96M

key_buffer_size = 8M

read_buffer_size = 2M

read_rnd_buffer_size = 16M

bulk_insert_buffer_size = 32M

#myisam

myisam_sort_buffer_size = 128M

myisam_max_sort_file_size = 10G

myisam_repair_threads = 1

#innodb

innodb_buffer_pool_size = 100M

innodb_buffer_pool_instances = 1

innodb_data_file_path = ibdata1:100M:autoextend

innodb_flush_log_at_trx_commit = 2

innodb_log_buffer_size = 8M

innodb_log_file_size = 100M

innodb_log_files_in_group = 3

innodb_max_dirty_pages_pct = 50

innodb_file_per_table = 1

innodb_rollback_on_timeout

innodb_status_file = 1

innodb_io_capacity = 2000

transaction_isolation = READ-COMMITTED

innodb_flush_method = O_DIRECT

```

## 从库配置文件

```

#my.cnf

[client]

port = 3307

socket = /tmp/mysql.sock

[mysql]

prompt="\\u@\\h [\\d]>"

no-auto-rehash

[mysqld]

user = mysql

basedir = /usr/local/mysql

datadir = /data/mysql/mysql3307/data

port = 3307

socket = /tmp/mysql3307.sock

event_scheduler = 0

tmpdir = /data/mysql/mysql3307/tmp

#timeout

interactive_timeout = 300

wait_timeout = 300

#character set

character-set-server = utf8

open_files_limit = 65535

max_connections = 100

max_connect_errors = 100000

#lower_case_table_names =1

#logs

log-output=file

slow_query_log = 1

slow_query_log_file = slow.log

log-error = error.log

log_warnings = 2

pid-file = mysql.pid

long_query_time = 1

log-slow-slave-statements = 1

binlog_format = row

server-id = 123307

log-bin = /data/mysql/mysql3307/logs/mysql-bin

binlog_cache_size = 4M

max_binlog_size = 256M

max_binlog_cache_size = 1M

sync_binlog = 0

expire_logs_days = 10

#procedure

log_bin_trust_function_creators=1

#

gtid-mode = on

enforce_gtid_consistency=on

#relay log

skip_slave_start = 1

max_relay_log_size = 128M

relay_log_purge = 1

relay_log_recovery = 1

relay-log=relay-bin

relay-log-index=relay-bin.index

log_slave_updates

#slave-skip-errors=1032,1053,1062

#skip-grant-tables

#buffers & cache

table_open_cache = 2048

table_definition_cache = 2048

table_open_cache = 2048

max_heap_table_size = 96M

sort_buffer_size = 128K

join_buffer_size = 128K

thread_cache_size = 200

query_cache_size = 0

query_cache_type = 0

query_cache_limit = 256K

query_cache_min_res_unit = 512

thread_stack = 192K

tmp_table_size = 96M

key_buffer_size = 8M

read_buffer_size = 2M

read_rnd_buffer_size = 16M

bulk_insert_buffer_size = 32M

#myisam

myisam_sort_buffer_size = 128M

myisam_max_sort_file_size = 10G

myisam_repair_threads = 1

#innodb

innodb_buffer_pool_size = 100M

innodb_buffer_pool_instances = 1

innodb_data_file_path = ibdata1:100M:autoextend

innodb_flush_log_at_trx_commit = 2

innodb_log_buffer_size = 8M

innodb_log_file_size = 100M

innodb_log_files_in_group = 3

innodb_max_dirty_pages_pct = 50

innodb_file_per_table = 1

innodb_rollback_on_timeout

innodb_status_file = 1

innodb_io_capacity = 2000

transaction_isolation = READ-COMMITTED

innodb_flush_method = O_DIRECT

```

## 自动化安装脚本 install_mysql.py

安装路径: /data/mysql/mysql${port}/{tmp,logs,data}

mysql安装包路径: /user/local/mysql

脚本调用方法: ./install_mysql.py ${port}

```

[root@upright92 mysql]# cat install_mysql.py

#!/usr/bin/python

import os

import os.path

import sys

import socket

PORT=sys.argv[1]

IP=socket.gethostbyname(socket.gethostname())

IP=IP[IP.rfind('.')+1:]

SERVER_ID=IP+PORT

##auto start mysql

### mysql configure file

my_conf="""

#my.cnf

[client]

port = 3306

socket = /tmp/mysql.sock

[mysql]

prompt="\\\\u@\\\\h [\\\\d]>"

no-auto-rehash

[mysqld]

user = mysql

basedir = /usr/local/mysql

datadir = /data/mysql/mysql3306/data

port = 3306

socket = /tmp/mysql3306.sock

event_scheduler = 0

tmpdir = /data/mysql/mysql3306/tmp

#timeout

interactive_timeout = 300

wait_timeout = 300

#character set

character-set-server = utf8

open_files_limit = 65535

max_connections = 100

max_connect_errors = 100000

#lower_case_table_names =1

#logs

log-output=file

slow_query_log = 1

slow_query_log_file = slow.log

log-error = error.log

log_warnings = 2

pid-file = mysql.pid

long_query_time = 1

log-slow-slave-statements = 1

binlog_format = row

server-id = 123306

log-bin = /data/mysql/mysql3306/logs/mysql-bin

binlog_cache_size = 4M

max_binlog_size = 256M

max_binlog_cache_size = 1M

sync_binlog = 0

expire_logs_days = 10

#procedure

log_bin_trust_function_creators=1

#

gtid-mode = on

enforce_gtid_consistency=on

#relay log

skip_slave_start = 1

max_relay_log_size = 128M

relay_log_purge = 1

relay_log_recovery = 1

relay-log=relay-bin

relay-log-index=relay-bin.index

log_slave_updates

#slave-skip-errors=1032,1053,1062

#skip-grant-tables

#buffers & cache

table_open_cache = 2048

table_definition_cache = 2048

table_open_cache = 2048

max_heap_table_size = 96M

sort_buffer_size = 128K

join_buffer_size = 128K

thread_cache_size = 200

query_cache_size = 0

query_cache_type = 0

query_cache_limit = 256K

query_cache_min_res_unit = 512

thread_stack = 192K

tmp_table_size = 96M

key_buffer_size = 8M

read_buffer_size = 2M

read_rnd_buffer_size = 16M

bulk_insert_buffer_size = 32M

#myisam

myisam_sort_buffer_size = 128M

myisam_max_sort_file_size = 10G

myisam_repair_threads = 1

#innodb

innodb_buffer_pool_size = 100M

innodb_buffer_pool_instances = 1

innodb_data_file_path = ibdata1:100M:autoextend

innodb_flush_log_at_trx_commit = 2

innodb_log_buffer_size = 8M

innodb_log_file_size = 100M

innodb_log_files_in_group = 3

innodb_max_dirty_pages_pct = 50

innodb_file_per_table = 1

innodb_rollback_on_timeout

innodb_status_file = 1

innodb_io_capacity = 2000

transaction_isolation = READ-COMMITTED

innodb_flush_method = O_DIRECT

"""

BASE_DIR="/data/mysql/mysql"+str(PORT)

DATA_DIR="/data/mysql/mysql"+str(PORT)+"/data"

LOG_DIR="/data/mysql/mysql"+str(PORT)+"/logs"

TMP_DIR="/data/mysql/mysql"+str(PORT)+"/tmp"

parameter="/data/mysql/mysql"+str(PORT)+"/my"+str(PORT)+".cnf"

my_conf=my_conf.replace('3306',PORT)

my_conf=my_conf.replace('123306',SERVER_ID)

#if data dir exsits

if os.path.isdir(BASE_DIR):

print(str(PORT) + " exits, please change")

else:

os.mkdir(BASE_DIR)

os.mkdir(DATA_DIR)

os.mkdir(LOG_DIR)

os.mkdir(TMP_DIR)

os.system("chown -R mysql.mysql "+BASE_DIR)

with open(parameter,'w') as f:

f.writelines(my_conf)

os.system("mysqld --defaults-file="+parameter+" --initialize")

os.system("mysqld --defaults-file="+parameter+" &")

```

mysql 自动复制_MySQL 复制搭建(附mysql自动搭建脚本)相关推荐

  1. mysql 基于语句的复制_MySQL 复制 - 性能与扩展性的基石 1:概述及其原理

    1. 复制概述 MySQL 内置的复制功能是构建基于 MySQL 的大规模.高性能应用的基础,复制解决的基本问题是让一台服务器的数据与其他服务器保持同步.接下来,我们将从复制概述及原理.复制的配置.常 ...

  2. mysql行复制_MySQL复制(一)--复制概述

    (一)什么是复制 MySQL复制可以使数据从一台MySQL服务器(主服务器)复制到一台或多台MySQL服务器(从服务器),默认情况下,MySQL的复制是异步的,从服务器不需要永久连接就可以接收来自主服 ...

  3. mysql 机器复制_MySQL复制在同一台机器上

    这怎么解释MySQL在同一台机器上的复制. 你有很大的命中,数据库驱动的站点和性能,冗余,安全性现在被考虑在内. DBA现在由很多手指输入,寻求一些很好的解决方案,使幸福的脸孔.所以这里有一些单词,如 ...

  4. mysql 串行复制_MySQL 复制(四:复制原理) | 剑花烟雨江南

    一.基于语句的复制 在MySQL5.0之前的版本只支持基于语句的复制(也称为逻辑复制),这在数据库领域是很少见的.基于语句的复制模式下,主库会记录那些造城数据更改的查询,当备库读取并重放这些事件时,实 ...

  5. 客户端连接mysql 自动断开_MySql连接空闲8小时自动断开的原因及连接池配置方法...

    数据库连接超时时间查询 非交互式超时时间,如 JDBC 程序 show global variables like 'wait_timeout'; 交互式超时时间,如数据库工具 show global ...

  6. drbd实现mysql地热备_Mysql+DRBD+Heartbeat 实现mysql高可用的双机热备(mysql+heartbeat篇)...

    *************************************部署MYSQL******************************************* yum -y insta ...

  7. mysql status改变_mysql 配置详解mysql SHOW STATUS 详解

    1. back_log 指定MySQL可能的连接数量.当MySQL主线程在很短的时间内得到非常多的连接请求,该参数就起作用,之后主线程花些时间(尽管很短)检查连接并且启动一个新线程. back_log ...

  8. 修改mysql编码方式centos_CentOS下修改mysql数据库编码为UTF-8(附mysql开启远程链接和开放3306端口)...

    楼主在配置好linux云服务器的jdk,tomcat,mysql后,当要开始部署项目是,忽然意识到一个很严重的问题,那就是数据库的编码问题,自安装完成后并未修改数据库的额编码....下面就来讲说lin ...

  9. mysql 查询分析器_mysql查询分析工具|mysql查询分析器(MySQL Query Browser)下载v1.1.20 官方版_ IT猫扑网...

    mysql查询分析器又名为mysqlquerybrowser,是一款可以帮助用户快捷进行sql语句查询的工具,让你在编程的过程中更加方便的进行工作.有需要的朋友就来IT猫扑下载吧! MySQL Que ...

最新文章

  1. Odoo 学习 【二】Environment 概览
  2. 他保送北大、读完博士选择回中学任教,“做科研太枯燥,自己更适合教书”...
  3. 关于MATLAB处理大数据坐标文件2017529
  4. PMP-【第6章 项目进度管理】-2021-2-11(136页-160页)
  5. php mail 失败,php-mail()失败,但返回true
  6. 深度学习100例 -卷积神经网络(ResNet-50)鸟类识别 | 第8天
  7. HTML+CSS实战作业
  8. TCP三次握手、糊涂窗口、粘包问题
  9. java 微商城开发_Java网上商城系统可以开微信商城吗
  10. mysql可能锁的表命令_mysql 默许是表级锁一些不太常用命令
  11. 由有理函数的广义积分引入,谈谈复变函数论中的留数
  12. 计算机网络四个阶段特点,计算机网络的发展分哪四个阶段,特点?
  13. vue 对象提供的属性功能、通过axio请求数据(2)
  14. 华为路由器hilink怎么用_华为HiLink是什么?华为hilink智能联网怎么用
  15. CPU性能测试分析MIPS、DMIPS
  16. Docer中使能GPU,GDB,perf
  17. 建筑施工员日常工作内容,施工员证全国通用吗?
  18. 桌面、平板、手机和机器人操作系统(ROS)市场占有率数据和趋势分享(附引用2019版)
  19. 3DMAX动画渲染 军舰在海中航行
  20. 数据库巡检 java_美团MySQL数据库巡检系统的设计与应用

热门文章

  1. 冷读术:瞬间抓住人心和操控人心的沟通技巧
  2. 国王和100个囚犯还有1盏灯
  3. Django定时任务四种实现方法总结
  4. 【芯片识别】基于matlab GUI形态学PCB板芯片识别【含Matlab源码 1820期】
  5. 世界十大著名海盗:有一个是中国美女
  6. 在php中加css_如何使用php脚本给html中引用的js和css路径打上版本号
  7. D:\ANdriod\emulator\qemu\windows-x86_64\qemu-system-i386.exe: Could not open 'C:\Users\榛刓.android\a
  8. 2018 (天津)新能源汽车三电系统技术发展交流大会暨汽车轻量化技术开发与材料应用论坛
  9. 服务器怎么解压文件夹,怎么在云服务器上解压文件夹
  10. html动画变圆,HTML5 canvas制作圆形的万花筒动画效果