MySQL主从备份搭建
主库:
用以下命令覆盖 my.cnf文件
[client]
port = 3306
default-character-set = utf8mb4
[mysqld]
port = 3306
user = mysql
bind-address = 0.0.0.0
server-id = 1
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
skip-name-resolve
skip-external-locking
#skip-networking
back_log = 300
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = master_log
binlog_format = mixed
expire_logs_days = 10
slow_query_log = 1
long_query_time = 1
performance_schema = 0
explicit_defaults_for_timestamp
lower_case_table_names = 1
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
重启mysql:service mysqld restart
查看状态是否重启成功:service mysqld status
创建用户
在MySQL数据库中,为不存在的用户授权,就是同步创建用户并授权.
此用户是从库访问主库使用的用户
ip地址不能写为%. 因为主从备份中,当前创建的用户,是给从库Slave访问主库Master使用的.用户必须有指定的访问地址.不能是通用地址.
grant all privileges on *.* to '要授权的用户名'@'要授权的IP地址' identified by '密码' with grant option;
flush privileges;
主库配置完成!
查看master信息:show master status;
查看master信息
从库:
和主库一样把配置文件替换到从库中,但是server-id需要修改 因为maser是id=1,不能重复修改成自然数不重复就行,如果是虚拟机复制的话,需要/var/lib/mysql/auto.cnf中的UUID,改任意一个字符就行。
重启从库的MySQL:
service mysqld restart
重启完成后进入slave的MySQL停止slave:
stop slave;
配置主库信息:
需要修改的数据是依据Master信息修改的. ip是Master所在物理机IP. 用户名和密码是Master提供的Slave访问用户名和密码. 日志文件是在Master中查看的主库信息提供的.在Master中使用命令show master status查看日志文件名称.
change master to master_host=’ip’, master_user=’username’, master_password=’password’, master_log_file=’log_file_name’;
change master to master_host='192.168.56.210', master_user='slave', master_password='123456', master_log_file='master_log.000002';
执行成功之后启动slave:
start slave;
查看主从配置是否成功:
show slave status \G;
出现以下信息则搭建成功!
主从配置搭建完成