发现mysql主从同上步 对于函数过程总是出错 MySQL创建函数问题分析： 根据系统提示，导致该错误的原因可能是一个安全设置方面的配置，查手册log_bin_trust_function_creators参数缺省0，是不 允许function的同步的。 一般我们在配置repliaction的时候，都忘记关注这个参数，这样在master更新funtion后，slave就会报告错误，然后slave stoped。 MySQL创建函数问题处理过程： 登陆mysql数据库 > set global log_bin_trust_function_creators ＝ 1; > start slave; 跟踪mysql的启动日志，slave正常运行，问题解决。 持续跟踪，经过一个晚上，bin-relay-log的数据全部同步完毕。 直接在my.cnf里面添加 log_bin_trust_function_creators ＝ 1 ======================================================================== 创建function时 出错信息： ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable) 原因： 这是我们开启了bin-log, 我们就必须指定我们的函数是否是 1 DETERMINISTIC 不确定的 2 NO SQL 没有SQl语句，当然也不会修改数据 3 READS SQL DATA 只是读取数据，当然也不会修改数据 4 MODIFIES SQL DATA 要修改数据 5 CONTAINS SQL 包含了SQL语句 其中在function里面，只有 DETERMINISTIC, NO SQL 和 READS SQL DATA 被支持。如果我们开启了 bin-log, 我们就必须为我们的function指定一个参数。 解决方法： SQL code mysql>show variables like '%func%'; +---------------------------------+-------+ |Variable_name |Value| +---------------------------------+-------+ |log_bin_trust_function_creators|OFF | +---------------------------------+-------+ 1 row in set (0.00sec) mysql>set global log_bin_trust_function_creators=1; Query OK,0 rows affected (0.00sec) mysql>show variables like '%func%'; +---------------------------------+-------+ |Variable_name |Value| +---------------------------------+-------+ |log_bin_trust_function_creators|ON | +---------------------------------+-------+ 1 row in set (0.00sec) mysql> 试过了，这个方法很有效的。 之前修改了my.ini文件，在mysqld下面加上了log_bin_trust_function_creators=1，都没好使。
由于主服务器异外重启, 导致从报错, 错误如下: show slave status错误: mysql> show slave status\G Master_Log_File: mysql-bin.000288 Read_Master_Log_Pos: 627806304 Relay_Log_File: mysql-relay-bin.000990 Relay_Log_Pos: 627806457 Relay_Master_Log_File: mysql-bin.000288 Slave_IO_Running: No Slave_SQL_Running: Yes Exec_Master_Log_Pos: 627806304 Relay_Log_Space: 627806663 ...... Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Client requested master to start replication from impossible position' mysql错误日志: tail /data/mysql/mysql-error.log 111010 17:35:49 [ERROR] Error reading packet from server: Client requested master to start replication from impossible position ( server_errno=1236) 111010 17:35:49 [ERROR] Slave I/O: Got fatal error 1236 from master when reading data from binary log: 'Client requested master to start replication from impossible position', Error_code: 1236 111010 17:35:49 [Note] Slave I/O thread exiting, read up to log 'mysql-bin.000288', position 627806304 按照习惯, 先尝试必改position位置. mysql> stop slave; mysql> change master to master_log_file='mysql-bin.000288',master_log_pos=627625751; mysql> start slave; 错误依旧, 接下来登陆到主服务器查看binlog日志. 先按照错误点的标记去主服务器日志中查找: [root@db1 ~]# mysqlbinlog --start-position=627655136 /data/mysql/binlog/mysql-bin.000288 /*!40019 SET @@session.max_insert_delayed_threads=0*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #111010 13:31:19 server id 4 end_log_pos 106 Start: binlog v 4, server v 5.1.45-log created 111010 13:31:19 # Warning: this binlog is either in use or was not closed properly. BINLOG ' F1aTTg8EAAAAZgAAAGoAAAABAAQANS4xLjQ1LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC '/*!*/; DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; 没有看到这个位置. [root@db1 ~]# mysqlbinlog /data/mysql/binlog/mysql-bin.000288 > test.txt less text.txt 看最后一部分 # at 627625495 #111010 16:35:46 server id 1 end_log_pos 627625631 Query thread_id=45613333 exec_time=32758 error_code=0 SET TIMESTAMP=1318289746/*!*/; delete from freeshipping_bef_update where part='AR-4006WLM' and code='' /*!*/; # at 627625631 #111010 16:35:46 server id 1 end_log_pos 627625751 Query thread_id=45613333 exec_time=32758 error_code=0 SET TIMESTAMP=1318289746/*!*/; delete from shippingFee_special where part='AR-4006WLM' /*!*/; DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; 找到最接近错误标记627655136的一个position是627625631. 再回到slave机器上change master, 将postion指向这个位置. mysql> stop slave; Query OK, 0 rows affected (0.00 sec) mysql> change master to master_log_file='mysql-bin.000288',master_log_pos=627625631; Query OK, 0 rows affected (0.06 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec) 再次查看 mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Queueing master event to the relay log Master_Host: 192.168.21.105 Master_User: rep Master_Port: 3306 Connect_Retry: 10 Master_Log_File: mysql-bin.000289 Read_Master_Log_Pos: 25433767 Relay_Log_File: mysql-relay-bin.000003 Relay_Log_Pos: 630 Relay_Master_Log_File: mysql-bin.000289 Slave_IO_Running: Yes Slave_SQL_Running: Yes 主从同步正常了, 同样的方法修复其它slave机器. mysql运行在nat环境下的报错： ``` ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 2 ``` 解决办法： ``` bind-address = 0.0.0.0 ```
#!/bin/sh unset LANG LC_ALL=C export LC_ALL PATH=$PATH:/usr/local/mysql/bin export PATH user=root password=xnjcwmysql master_host="192.168.0.250" port=3306 master_port=3306 backup_lv_name="/dev/vg/data" backup_path="/home/backup" backup_dir="mysql" backup_binlog_dir="binlog" backup_binlog_name="binlog_`date +%Y%m%d --date='1 days ago'`.tar.bz2" backup_binlog_expire="binlog_`date +%Y%m%d --date='8 days ago'`.tar.bz2" remote_backup_host="192.168.0.200" remote_backup_path="/data/databases" remote_backup_name="`date +%w --date='1 days ago'`.tar.bz2" remote_backup_binlog_name="binlog_`date +%w --date='1 days ago'`.tar.bz2" ## 刷新 master 的 binlog mysql -u$user -p$password -h$master_host -P$master_port -e 'flush logs' ## 停止 slave sleep 5 mysql -u$user -p$password -P$port -e 'slave stop' sleep 5 ## 创建 MySQL 数据 逻辑卷 的快照 modprobe dm-snapshot lvcreate -l 486 -s -n mysql_snap $backup_lv_name ## 启动 slave mysql -u$user -p$password -P$port -e 'slave start' ## 备份 binlog mkdir -p /mnt/mysql_snap mount /dev/vg/mysql_snap /mnt/mysql_snap cd /mnt/mysql_snap tar cjpf $backup_path/$backup_binlog_dir/$backup_binlog_name `ls | grep mysql-bin.[0-9]` ## 备份数据库文件 rsync -a --delete --exclude "mysql-bin.*" --exclude "*.MYI" --exclude "relay-log.info" /mnt/mysql_snap/ $backup_path/$backup_dir/ cp -af /mnt/mysql_snap/mysql/*.MYI $backup_path/$backup_dir/mysql/ #?把备的的 binlo压缩包包传送到远程备份主机scpcp $backup_path/$backup_binlog_d/r/$backup_binlog_name $remote_backup_ho:t:$remote_backup_path/$remote_backup_binlog_name ## 打包备份目录并传送到远程备份主机 cd $backup_path tar -cjpf - $backup_dir | ssh $remote_backup_host "cat > $remote_backup_path/$remote_backup_name" ## 删除快照 umount /mnt/mysql_snap lvremove -f /dev/vg/mysql_snap rm -rf /mnt/mysql_snap rmmod dm-snapshot ## 删除过期的 binlog rm -f $backup_path/$backup_binlog_dir/$backup_binlog_expire ## 清除已备份的 binlog curr_binlog=`mysql -u$user -p$password -P$port -e 'show slave status\G' | grep Master_Log_File | awk '{print $2}' | uniq` mysql -u$user -p$password -h$master_host -P$master_port -e 'purge master logs to "'$curr_binlog'"' mysql -u$user -p$password -P$port -e 'purge master logs to "'$curr_binlog'"'