《MYSQL数据库linux系统下实现mysql热备份详细步骤(mysql主从复制)》要点:
本文介绍了MYSQL数据库linux系统下实现mysql热备份详细步骤(mysql主从复制),希望对您有用。如果有疑问,可以联系我们。
MYSQL实例主从的作用:
MYSQL实例1.可以当做一种备份方式
MYSQL实例2.用来实现读写分离,缓解一个数据库的压力
MYSQL实例 MySQL主从备份原理:
MYSQL实例Mysql的主从复制至少是需要两个Mysql的服务,当然Mysql的服务是可以分布在不同的服务器上,也可以在一台服务器上启动多个服务.
MYSQL实例如果想配置成为同一台上的话,注意安装的时候,选择两个不同的prefix=路径,同时开启服务器的时候,端口不能相同.
MYSQL实例(1)首先确保主从服务器上的Mysql版本相同(做主从服务器的原则是,MYSQL版本要相同,如果不能满足,最起码从服务器的MYSQL的版本必须高于主服务器的MYSQL版本 )
MYSQL实例(2)在主服务器上,设置一个从数据库的账户,使用REPLICATION SLAVE赋予权限,如:
MYSQL实例'123456';
MYSQL实例Query OK, 0 rows affected (0.13 sec)
MYSQL实例[原理]master 上提供binlog ,
MYSQL实例slave 通过 I/O线程从 master拿取 binlog,并复制到slave的中继日志中
MYSQL实例slave 通过 SQL线程从 slave的中继日志中读取binlog ,然后解析到slave中
MYSQL实例 主从复制大前提
MYSQL实例需要master与slave同步,因为笔者的数据库数据量不大,所以无需考虑太多,直接把
MYSQL实例master上的data复制到了slave上,但是如果是大的数据量,比如像taobao这个的系统
MYSQL实例实验环境准备:
MYSQL实例OS: CentOS5.4
MYSQL实例Mysql:Mysql-5.0.41.tar.gz
辅助工具:SSH Secure Shell Client
MYSQL实例两台测试IP&服务器:
MYSQL实例 1、首先在Linux环境下分配好磁盘分区以便留足MySQL数据库的备份空间
MYSQL实例
2、MySQL数据库的安装:
MYSQL实例1>将Mysql-5.0.41.tar.gz通过SSH 工具 上传到Linux系统的home目录下
MYSQL实例2>建立MySQL使用者和群组:
MYSQL实例3>解压缩Mysql-5.0.41.tar.gz源码包
MYSQL实例4>进入源码目录编译安装
MYSQL实例5>替换/etc/my.cnf文件,进入源码包,执行命令
MYSQL实例7>设置给mysql和root用户设定访问权限 我们先进入mysql目录
MYSQL实例8>启动mysql,进入已经安装好的目录
MYSQL实例9>
修改mysql数据库超级用户root的缺省密码:
MYSQL实例关闭mysql服务器
MYSQL实例10>设定开机就启动mysql,进入源码目录下
MYSQL实例# chmod +x /etc/init.d/mysql
# chkconfig --level 345 mysql on
# service mysql restart
Shutting down MySQL. [ 确定 ]
Starting MySQL [ 确定 ]
[root@localhost mysql]#
MYSQL实例到这里MySQL就装好了.
MYSQL实例3、配置MySQL5.0的复制(Replication)功能
MYSQL实例 一.将master设置为只读.
MYSQL实例mysql> flush tables with read lock;
MYSQL实例二.用master中的data文件夹替换slave中的data文件夹
MYSQL实例比如 用 tar zcvf mysql_data.gz /media/raid10/mysql/3306/data
MYSQL实例然后 mv mysql_data.gz /media/raid10/htdocs/blog/wordpress/
MYSQL实例因为我的 /media/raid10/htdocs/blog/wordpress/ 是 Nginx 的主目录
MYSQL实例所以可以在 slave上,用wget下载这个文件,然后 解压,并覆盖slave上的data文件
MYSQL实例注意:覆盖之前最好备份源文件
MYSQL实例三.配置master的my.cnf,添加以下内容
MYSQL实例在[mysqld]配置段添加如下字段
MYSQL实例log-bin=/media/raid10/mysql/3306/binlog/binlog //这里写你的binlog绝对路径名
MYSQL实例binlog-do-db=blog //需要同步的数据库,如果没有本行,即表示同步所有的数据库
MYSQL实例binlog-ignore-db=mysql //被忽略的数据库
MYSQL实例这里给出我的my.cnf配置文件
MYSQL实例character-set-server = utf8
MYSQL实例port = 3306
MYSQL实例socket = /tmp/mysql.sock
MYSQL实例[mysqld]
MYSQL实例character-set-server = utf8
MYSQL实例replicate-ignore-db = mysql
MYSQL实例replicate-ignore-db = test
MYSQL实例replicate-ignore-db = information_schema
MYSQL实例user = mysql
MYSQL实例port = 3306
MYSQL实例socket = /tmp/mysql.sock
MYSQL实例basedir = /usr/local/webserver/mysql
MYSQL实例datadir = /media/raid10/mysql/3306/data
MYSQL实例log-error = /media/raid10/mysql/3306/mysql_error.log
MYSQL实例pid-file = /media/raid10/mysql/3306/mysql.pid
MYSQL实例open_files_limit = 10240
MYSQL实例back_log = 600
MYSQL实例max_connections = 5000
MYSQL实例max_connect_errors = 6000
MYSQL实例table_cache = 614
MYSQL实例external-locking = FALSE
MYSQL实例max_allowed_packet = 16M
MYSQL实例sort_buffer_size = 1M
MYSQL实例join_buffer_size = 1M
MYSQL实例thread_cache_size = 300
MYSQL实例#thread_concurrency = 8
MYSQL实例query_cache_size = 20M
MYSQL实例query_cache_limit = 2M
MYSQL实例query_cache_min_res_unit = 2k
MYSQL实例default-storage-engine = MyISAM
MYSQL实例thread_stack = 192K
MYSQL实例transaction_isolation = READ-COMMITTED
MYSQL实例tmp_table_size = 20M
MYSQL实例max_heap_table_size = 20M
MYSQL实例long_query_time = 3
MYSQL实例log-slave-updates
MYSQL实例log-bin = /media/raid10/mysql/3306/binlog/binlog
MYSQL实例binlog-do-db=blog
MYSQL实例binlog-ignore-db=mysql
MYSQL实例 binlog_cache_size = 4M
MYSQL实例binlog_format = MIXED
MYSQL实例max_binlog_cache_size = 8M
MYSQL实例max_binlog_size = 20M
MYSQL实例relay-log-index = /media/raid10/mysql/3306/relaylog/relaylog
MYSQL实例relay-log-info-file = /media/raid10/mysql/3306/relaylog/relaylog
MYSQL实例relay-log = /media/raid10/mysql/3306/relaylog/relaylog
MYSQL实例expire_logs_days = 30
MYSQL实例key_buffer_size = 10M
MYSQL实例read_buffer_size = 1M
MYSQL实例read_rnd_buffer_size = 6M
MYSQL实例bulk_insert_buffer_size = 4M
MYSQL实例myisam_sort_buffer_size = 8M
MYSQL实例myisam_max_sort_file_size = 20M
MYSQL实例myisam_repair_threads = 1
MYSQL实例myisam_recover
MYSQL实例 interactive_timeout = 120
MYSQL实例wait_timeout = 120
MYSQL实例 skip-name-resolve
MYSQL实例#master-connect-retry = 10
MYSQL实例slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
MYSQL实例#master-host = 192.168.1.2
MYSQL实例#master-user = username
MYSQL实例#master-password = password
MYSQL实例#master-port = 3306
MYSQL实例 server-id = 1
MYSQL实例innodb_additional_mem_pool_size = 16M
MYSQL实例innodb_buffer_pool_size = 20M
MYSQL实例innodb_data_file_path = ibdata1:56M:autoextend
MYSQL实例innodb_file_io_threads = 4
MYSQL实例innodb_thread_concurrency = 8
MYSQL实例innodb_flush_log_at_trx_commit = 2
MYSQL实例innodb_log_buffer_size = 16M
MYSQL实例innodb_log_file_size = 20M
MYSQL实例innodb_log_files_in_group = 3
MYSQL实例innodb_max_dirty_pages_pct = 90
MYSQL实例innodb_lock_wait_timeout = 120
MYSQL实例innodb_file_per_table = 0
MYSQL实例 #log-slow-queries = /media/raid10/mysql/3306/slow.log
MYSQL实例#long_query_time = 10
MYSQL实例 [mysqldump]
MYSQL实例quick
MYSQL实例max_allowed_packet = 32M
MYSQL实例四.在master机上为slave机添加一同步帐号
MYSQL实例mysql> flush privileges ;
MYSQL实例五.配置slave的my.cnf,添加以下内容
MYSQL实例注意:
MYSQL实例1.如果mysql是5.5.3-m3 的版本,只需
MYSQL实例在[mysqld]字段下添加如下内容
MYSQL实例server-id=2
MYSQL实例 2.如果是5.0x的版本,需要
MYSQL实例在[mysqld]字段下添加如下内容
MYSQL实例log-bin=mysql-bin //这是同步的binlog,具体以你的binlog为准
MYSQL实例master-host=172.29.141.112
MYSQL实例master-user=admin
MYSQL实例master-password=12345678
MYSQL实例master-port=3306
MYSQL实例master-connect-retry=60 //如果发现主服务器断线,重新连接的时间差;
MYSQL实例replicate-do-db=blog //同步的数据库,不写本行 表示 同步所有数据库
MYSQL实例replicate-ignore-db=mysql //不需要备份的数据库
MYSQL实例log-slave-update
MYSQL实例slave-skip-errors
MYSQL实例 我的mysql是5.5.3,这里给出我的slave my.cnf配置文件
MYSQL实例character-set-server = utf8
MYSQL实例port = 3306
MYSQL实例socket = /tmp/mysql.sock
MYSQL实例
MYSQL实例[mysqld]
MYSQL实例character-set-server = utf8
MYSQL实例replicate-ignore-db = mysql
MYSQL实例replicate-ignore-db = test
MYSQL实例replicate-do-db = blog
MYSQL实例replicate-ignore-db = information_schema
MYSQL实例user = mysql
MYSQL实例port = 3306
MYSQL实例socket = /tmp/mysql.sock
MYSQL实例basedir = /usr/local/webserver/mysql
MYSQL实例datadir = /media/raid10/mysql/3306/data
MYSQL实例log-error = /media/raid10/mysql/3306/mysql_error.log
MYSQL实例pid-file = /media/raid10/mysql/3306/mysql.pid
MYSQL实例open_files_limit = 10240
MYSQL实例back_log = 600
MYSQL实例max_connections = 5000
MYSQL实例max_connect_errors = 6000
MYSQL实例table_cache = 614
MYSQL实例external-locking = FALSE
MYSQL实例max_allowed_packet = 16M
MYSQL实例sort_buffer_size = 1M
MYSQL实例join_buffer_size = 1M
MYSQL实例thread_cache_size = 300
MYSQL实例#thread_concurrency = 8
MYSQL实例query_cache_size = 20M
MYSQL实例query_cache_limit = 2M
MYSQL实例query_cache_min_res_unit = 2k
MYSQL实例default-storage-engine = MyISAM
MYSQL实例thread_stack = 192K
MYSQL实例transaction_isolation = READ-COMMITTED
MYSQL实例tmp_table_size = 20M
MYSQL实例max_heap_table_size = 20M
MYSQL实例long_query_time = 3
MYSQL实例log-slave-updates
MYSQL实例log-bin = /media/raid10/mysql/3306/binlog/binlog
MYSQL实例binlog_cache_size = 4M
MYSQL实例binlog_format = MIXED
MYSQL实例max_binlog_cache_size = 8M
MYSQL实例max_binlog_size = 20M
MYSQL实例relay-log-index = /media/raid10/mysql/3306/relaylog/relaylog
MYSQL实例relay-log-info-file = /media/raid10/mysql/3306/relaylog/relaylog
MYSQL实例relay-log = /media/raid10/mysql/3306/relaylog/relaylog
MYSQL实例expire_logs_days = 30
MYSQL实例key_buffer_size = 10M
MYSQL实例read_buffer_size = 1M
MYSQL实例read_rnd_buffer_size = 6M
MYSQL实例bulk_insert_buffer_size = 4M
MYSQL实例myisam_sort_buffer_size = 8M
MYSQL实例myisam_max_sort_file_size = 20M
MYSQL实例myisam_repair_threads = 1
MYSQL实例myisam_recover
MYSQL实例interactive_timeout = 120
MYSQL实例wait_timeout = 120
MYSQL实例 skip-name-resolve
MYSQL实例#master-connect-retry = 60
MYSQL实例slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
MYSQL实例 #master-host=172.29.141.112
MYSQL实例#master-user = admin
MYSQL实例#master-password = 12345678
MYSQL实例#master-port = 3306
MYSQL实例server-id = 2
MYSQL实例innodb_additional_mem_pool_size = 16M
MYSQL实例innodb_buffer_pool_size = 20M
MYSQL实例innodb_data_file_path = ibdata1:56M:autoextend
MYSQL实例innodb_file_io_threads = 4
MYSQL实例innodb_thread_concurrency = 8
MYSQL实例innodb_flush_log_at_trx_commit = 2
MYSQL实例innodb_log_buffer_size = 16M
MYSQL实例innodb_log_file_size = 20M
MYSQL实例innodb_log_files_in_group = 3
MYSQL实例innodb_max_dirty_pages_pct = 90
MYSQL实例innodb_lock_wait_timeout = 120
MYSQL实例innodb_file_per_table = 0
MYSQL实例#log-slow-queries = /media/raid10/mysql/3306/slow.log
MYSQL实例#long_query_time = 10
MYSQL实例[mysqldump]
MYSQL实例quick
MYSQL实例max_allowed_packet = 32M
MYSQL实例 六.通过查看master的状态(在master上查看),为配置slave做准备
MYSQL实例ERROR 2006 (HY000): MySQL server has gone away
MYSQL实例No connection. Trying to reconnect...
MYSQL实例Connection id: 13
MYSQL实例Current database: blog
MYSQL实例*************************** 1. row ***************************
MYSQL实例File: binlog.000005
MYSQL实例Position: 592
MYSQL实例Binlog_Do_DB: blog
MYSQL实例Binlog_Ignore_DB: mysql
MYSQL实例1 row in set (0.01 sec)
MYSQL实例ERROR:
MYSQL实例No query specified
MYSQL实例 从上面的信息,可以看出,master现在使用的binlog是binlog.000005,position是592,那么下面的slave配置必须与这个对应.
MYSQL实例 其实binlog.000005是当前master使用的binlog日志文件
MYSQL实例position是当前master使用的binlog.000005日志文件的位置
MYSQL实例简单理解为master正在使用哪个binlog的哪个数据行(位置).
MYSQL实例
七.如果是5.5.3-m3版本mysql,需要启动slave后,配置与master相关对应的信息(在slave上配置)
MYSQL实例注意,这个与第六步相对应
MYSQL实例mysql> change master to master_host='172.29.141.112', master_user='admin', master_password='12345678', master_log_file='binlog.000005', master_log_pos=488;
mysql> CHANGE MASTER TO MASTER_CONNECT_RETRY=60;
MYSQL实例 这个与5.0的配置my.cnf作用是一样的,配置成与master相对应的内容
MYSQL实例主要是配置slave,让slave知道从master的哪个binlog上的哪个位置复制数据.所以需要知道master的ip,user_name,user_passwd,binlog,binlog_position以及多长时间连接一次master
MYSQL实例 八.开启slave
MYSQL实例 九.解除master只读限制,并做测试
MYSQL实例mysql> use blog;
MYSQL实例mysql> create longxibendi ( a int, b int );
MYSQL实例mysql> show tables;
MYSQL实例+-----------------------+
MYSQL实例| Tables_in_blog |
MYSQL实例+-----------------------+
MYSQL实例| longxibendi |
MYSQL实例| wp_commentmeta |
MYSQL实例| wp_comments |
MYSQL实例| wp_links |
MYSQL实例| wp_options |
MYSQL实例| wp_postmeta |
MYSQL实例| wp_posts |
MYSQL实例| wp_term_relationships |
MYSQL实例| wp_term_taxonomy |
MYSQL实例| wp_terms |
MYSQL实例| wp_usermeta |
MYSQL实例| wp_users |
MYSQL实例+-----------------------+
MYSQL实例12 rows in set (0.00 sec)
MYSQL实例 十一.配置过程中,可以用 show slave status/G; 在 slave上
MYSQL实例查看 slave的复制情况
MYSQL实例十二.如果出现什么问题,可能是防火墙的问题
MYSQL实例/etc/init.d/iptables stop 关闭 master 上的防火墙,或者进行相应的配置
MYSQL实例常遇到的错误与解决:
MYSQL实例1.[mysql]ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
MYSQL实例这个错误,网上有很多说法,其实直接的原因是mysql服务器没有启动
MYSQL实例之前我按照5.0x配置master-slave,然后启动slave,在连接slave,就会报这个错误
MYSQL实例后来发现原因是,mysql slave没有启动起来,然后去查错误日志,发现以下的字段
MYSQL实例110505 02:04:41 mysqld_safe Starting mysqld daemon with databases from /media/raid10/mysql/3306/data
MYSQL实例InnoDB: The InnoDB memory heap is disabled
MYSQL实例InnoDB: Mutexes and rw_locks use InnoDB's own implementation
MYSQL实例110505 2:04:41 InnoDB: highest supported file format is Barracuda.
MYSQL实例110505 2:04:41 InnoDB Plugin 1.0.6 started; log sequence number 44338
MYSQL实例110505 2:04:41 [ERROR] /usr/local/webserver/mysql/libexec/mysqld: unknown variable 'master-connect-retry=60'
MYSQL实例110505 2:04:41 [ERROR] Aborting
MYSQL实例110505 2:04:41 InnoDB: Starting shutdown...
MYSQL实例110505 2:04:43 InnoDB: Shutdown completed; log sequence number 44348
MYSQL实例110505 2:04:43 [Note] /usr/local/webserver/mysql/libexec/mysqld: Shutdown complete
MYSQL实例 110505 02:04:43 mysqld_safe mysqld from pid file /media/raid10/mysql/3306/mysql.pid ended
MYSQL实例110505 02:07:44 mysqld_safe Starting mysqld daemon with databases from /media/raid10/mysql/3306/data
MYSQL实例InnoDB: The InnoDB memory heap is disabled
MYSQL实例InnoDB: Mutexes and rw_locks use InnoDB's own implementation
MYSQL实例110505 2:07:44 InnoDB: highest supported file format is Barracuda.
MYSQL实例110505 2:07:45 InnoDB Plugin 1.0.6 started; log sequence number 44348
MYSQL实例110505 2:07:45 [ERROR] /usr/local/webserver/mysql/libexec/mysqld: unknown variable 'master-host=172.29.141.112'
MYSQL实例110505 2:07:45 [ERROR] Aborting
MYSQL实例
从上面的ERROR,知道 master-connect-retry=60这个my.cnf中的参数有问题,后来从网上搜资料,发现,mysql5.5.3-m3版本不支持这个参数,
MYSQL实例然后我把这个参数注释掉,又发现不支持这个参数master-host,从上面的ERROR字段可以看出来.后来,就知道,5.5.3-m3不能按5.0.x那样配置
MYSQL实例原来不需要从my.cnf中配置master相关信息,当然server-id是必须的.其他信息,通过 在命令行中 ,登陆 mysql服务器配置.
MYSQL实例其实server-id的作用是
MYSQL实例第一,标识,区分不同的slave,第二,防止环备份的发生
MYSQL实例
MYSQL实例2.Last_Error: Last_SQL_Error:等错误
MYSQL实例这个是从 slave上,运行 show slave status/G; 得到的.出现这个问题,最根本的原因是,slave 没有与当前的master的binlog 和binlog的position对应上
MYSQL实例也就是说,slave传输的master binlog 不与当前master正使用的binlog以及binlog的行数对应.
MYSQL实例
MYSQL实例3.[ERROR] Slave I/O: error connecting to master 'admin@172.29.141.112:3306' - retry-time: 60 retries: 86400, Error_code: 2003
MYSQL实例这个就是因为防火墙的问题,所以用 /etc/init.d/iptables stop 关闭防火墙就OK了.
MYSQL实例
MYSQL实例4.遇到ERROR 2013 (HY000): Lost connection to MySQL server during query错误.
在/ect/my.cnf的[mysqld]中添加skip-name-resolve
转载请注明本页网址:
http://www.vephp.com/jiaocheng/5393.html