mysql学习笔记4---mysql 复制---源代码
mysql:
c:底层
C++:相对上层
主备复制:主库通知备库来取 MYSQL复制源代码代码:SQL文件夹 int start_slave_thread(
#ifdef HAVE_PSI_INTERFACE
PSI_thread_key thread_key,
#endif
pthread_handler h_func, mysql_mutex_t *start_lock,
mysql_mutex_t *cond_lock,
mysql_cond_t *start_cond,
volatile uint *slave_running,
volatile ulong *slave_run_id,
Master_info* mi)
{
pthread_t th;
ulong start_id;
int error;
DBUG_ENTER("start_slave_thread"); if (start_lock)
mysql_mutex_lock(start_lock);
if (!server_id)
{
if (start_cond)
mysql_cond_broadcast(start_cond);
if (start_lock)
mysql_mutex_unlock(start_lock);
sql_print_error("Server id not set, will not start slave");
DBUG_RETURN(ER_BAD_SLAVE);
} if (*slave_running)
{
if (start_cond)
mysql_cond_broadcast(start_cond);
if (start_lock)
mysql_mutex_unlock(start_lock);
DBUG_RETURN(ER_SLAVE_MUST_STOP);
}
start_id= *slave_run_id;
DBUG_PRINT("info",("Creating new slave thread"));
if ((error= mysql_thread_create(thread_key,
&th, &connection_attrib, h_func, (void*)mi)))
{
sql_print_error("Can't create slave thread (errno= %d).", error);
if (start_lock)
mysql_mutex_unlock(start_lock);
DBUG_RETURN(ER_SLAVE_THREAD);
}
if (start_cond && cond_lock) // caller has cond_lock
{
THD* thd = current_thd;
while (start_id == *slave_run_id && thd != NULL)
{
DBUG_PRINT("sleep",("Waiting for slave thread to start"));
PSI_stage_info saved_stage= {, "", };
thd->ENTER_COND(start_cond, cond_lock,
& stage_waiting_for_slave_thread_to_start,
& saved_stage);
/*
It is not sufficient to test this at loop bottom. We must test
it after registering the mutex in enter_cond(). If the kill
happens after testing of thd->killed and before the mutex is
registered, we could otherwise go waiting though thd->killed is
set.
*/
if (!thd->killed)
mysql_cond_wait(start_cond, cond_lock);
thd->EXIT_COND(& saved_stage);
mysql_mutex_lock(cond_lock); // re-acquire it as exit_cond() released
if (thd->killed)
{
if (start_lock)
mysql_mutex_unlock(start_lock);
DBUG_RETURN(thd->killed_errno());
}
}
}
if (start_lock)
mysql_mutex_unlock(start_lock);
DBUG_RETURN();
} /**
Slave IO thread entry point. @param arg Pointer to Master_info struct that holds information for
the IO thread. @return Always 0.
*/ pthread_handler_t handle_slave_io(void *arg)
{
THD *thd= NULL; // needs to be first for thread_stack
bool thd_added= false;
MYSQL *mysql;
Master_info *mi = (Master_info*)arg;
Relay_log_info *rli= mi->rli;
char llbuff[];
uint retry_count;
bool suppress_warnings;
int ret;
int binlog_version;
#ifndef DBUG_OFF
uint retry_count_reg= , retry_count_dump= , retry_count_event= ;
#endif
// needs to call my_thread_init(), otherwise we get a coredump in DBUG_ stuff
my_thread_init();
DBUG_ENTER("handle_slave_io"); ..
} /**
Slave SQL thread entry point. @param arg Pointer to Relay_log_info object that holds information
for the SQL thread. @return Always 0.
*/
pthread_handler_t handle_slave_sql(void *arg)
{
THD *thd; /* needs to be first for thread_stack */
bool thd_added= false;
char llbuff[],llbuff1[];
char saved_log_name[FN_REFLEN];
char saved_master_log_name[FN_REFLEN];
my_off_t saved_log_pos= ;
my_off_t saved_master_log_pos= ;
my_off_t saved_skip= if (exec_relay_log_event(thd,rli)) ......................................
} mysql_execute_command(THD *thd)
{ 。。。。。。。。。。。。 case SQLCOM_SLAVE_START:
{
mysql_mutex_lock(&LOCK_active_mi);
if (active_mi != NULL)
res= start_slave(thd, active_mi, /* net report*/);
else
my_message(ER_SLAVE_CONFIGURATION, ER(ER_SLAVE_CONFIGURATION),
MYF());
mysql_mutex_unlock(&LOCK_active_mi);
break; 。。。。。。。。。。。。。。。。。。。
} /**
Top-level function for executing the next event in the relay log.
This is called from the SQL thread. This function reads the event from the relay log, executes it, and
advances the relay log position. It also handles errors, etc. This function may fail to apply the event for the following reasons: - The position specfied by the UNTIL condition of the START SLAVE
command is reached. - It was not possible to read the event from the log. - The slave is killed. - An error occurred when applying the event, and the event has been
tried slave_trans_retries times. If the event has been retried
fewer times, 0 is returned. - init_info or init_relay_log_pos failed. (These are called
if a failure occurs when applying the event.) - An error occurred when updating the binlog position. @retval 0 The event was applied. @retval 1 The event was not applied.
*/
static int exec_relay_log_event(THD* thd, Relay_log_info* rli)
{
DBUG_ENTER("exec_relay_log_event"); /*
We acquire this mutex since we need it for all operations except
event execution. But we will release it in places where we will
wait for something for example inside of next_event().
*/
mysql_mutex_lock(&rli->data_lock); /*
UNTIL_SQL_AFTER_GTIDS requires special handling since we have to check
whether the until_condition is satisfied *before* the SQL threads goes on
a wait inside next_event() for the re 。。。。。。。。。。。。
} */
exec_res= apply_event_and_update_pos(ptr_ev, thd, rli); apply_event_and_update_pos(Log_event** ptr_ev, THD* thd, Relay_log_info* rli) class Query_log_event: public Log_event
{
LEX_STRING user;
LEX_STRING host;
protected:
Log_event::Byte* data_buf;
public:
const char* query;
const char* catalog;
const char* db;
/*
If we already know the length of the query string
we pass it with q_len, so we would not have to call strlen()
otherwise, set it to 0, in which case, we compute it with strlen()
*/
uint32 q_len;
uint32 db_len;
uint16 error_code;
ulong thread_id;
/*
For events created by Query_log_event::do_apply_event (and
Load_log_event::do_apply_event()) we need the *original* thread
id, to be able to log the event with the original (=master's)
thread id (fix for BUG#1686).
*/
ulong slave_proxy_id; /*
Binlog format 3 and 4 start to differ (as far as class members are
concerned) from here.
*/ uint catalog_len; // <= 255 char; 0 means uninited /*
We want to be able to store a variable number of N-bit status vars:
(generally N=32; but N=64 for SQL_MODE) a user may want to log the number
of affected rows (for debugging) while another does not want to lose 4
bytes in this.
The storage on disk is the following:
status_vars_len is part of the post-header,
status_vars are in the variable-length part, after the post-header, before
the db & query.
status_vars on disk is a sequence of pairs (code, value) where 'code' means
'sql_mode', 'affected' etc. Sometimes 'value' must be a short string, so
its first byte is its length. For now the order of status vars is:
flags2 - sql_mode - catalog - autoinc - charset
We should add the same thing to Load_log_event, but in fact
LOAD DATA INFILE is going to be logged with a new type of event (logging of
the plain text query), so Load_log_event would be frozen, so no need. The
new way of logging LOAD DATA INFILE would use a derived class of
Query_log_event, so automatically benefit from the work already done for
status variables in Query_log_event.
*/
uint16 status_vars_len; /*
'flags2' is a second set of flags (on top of those in Log_event), for
session variables. These are thd->options which is & against a mask
(OPTIONS_WRITTEN_TO_BIN_LOG).
flags2_inited helps make
。。 } */ 主库/备库 挂了,数据不一致
:主健冲突,备库大于主库数据,,主库少了数据 ,主库挂了
:备库比主库数据少 set session sql_log_bin=;

semi-sync安装---master master 安装过程:
install plugin rpl_semi_sync_master soname 'semisync_master.so'
show variables like 'rpl_semi_sync_master_enabled'
值为ON,表示开启;否则检查失败原因 set global rpl_semi_sync_master_timeout=(利于观察)
set global rpl_semi_sync_master_wait_no_slave=
(是默认值,表示即使没有slave 也会以等待过期时间结束)
mysql学习笔记4---mysql 复制---源代码的更多相关文章
- MySQL学习笔记十七:复制特性
一.MySQL的复制是将主数据库(master)的数据复制到从(slave)数据库上,专业一点讲就是将主数据库DDL和DML操作的二进制日志传到从库上,然后从库对这些二进制日志进行重做,使得主数据库与 ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 安装
所有平台的 MySQL 下载地址为: MySQL 下载:https://dev.mysql.com/downloads/mysql/ 注意:安装过程我们需要通过开启管理员权限来安装,否则会由于权限不足 ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 运算符
要介绍 MySQL 的运算符及运算符的优先级. MySQL 主要有以下几种运算符: 算术运算符 比较运算符 逻辑运算符 位运算符 算术运算符 MySQL 支持的算术运算符包括: 在除法运算和模运算中, ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 导入数据
1.mysql 命令导入 使用 mysql 命令导入语法格式为: mysql -u用户名 -p密码 < 要导入的数据库数据(runoob.sql) 实例: # mysql -uroot -p12 ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 处理重复数据
有些 MySQL 数据表中可能存在重复的记录,有些情况允许重复数据的存在,但有时候我们也需要删除这些重复的数据. 防止表中出现重复数据 可以在 MySQL 数据表中设置指定的字段为 PRIMARY K ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 临时表
MySQL 临时表在我们需要保存一些临时数据时是非常有用的.临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间. MySQL临时表只在当前连接可见,如果使用PHP脚本来创建My ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 事务
MySQL 事务主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数据库操作语句就构成 ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL GROUP BY 语句
GROUP BY 语句根据一个或多个列对结果集进行分组. 在分组的列上我们可以使用 COUNT, SUM, AVG,等函数. GROUP BY 语法 SELECT column_name, funct ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL UNION 操作符
MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中.多个 SELECT 语句会删除重复的数据. 语法 MySQL UNION 操作符语法格式: SELECT ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 连接
使用mysql二进制方式连接 您可以使用MySQL二进制方式进入到mysql命令提示符下来连接MySQL数据库. 实例 以下是从命令行中连接mysql服务器的简单实例: [root@host]# my ...
随机推荐
- JS实现精确加减乘除
说明:项目中要使用 JS 实现自动计算的功能,进行一些浮点数运算时,计算结果却是一长串的值,这里提供一个解决方法,问题基本上可以解决. 具体代码如下: //加法函数 function accAdd(a ...
- [转]HTTP Error 400. The request hostname is invalid.
一般看到网站提示Bad Request(Invalid Hostname)错误我们都会说是iis,apache出问题了,iis出现这种问题解决办法大概是:IIS> 默认网站> > 属 ...
- 第 11 章 桥梁模式【Bridge Pattern】
以下内容出自:<<24种设计模式介绍与6大设计原则>> 今天我要说说我自己,梦想中的我自己,我身价过亿,有两个大公司,一个是房地产公司,一个是服装制造业,这两个公司都很赚钱,天 ...
- bzoj 3052: [wc2013]糖果公园 带修改莫队
3052: [wc2013]糖果公园 Time Limit: 250 Sec Memory Limit: 512 MBSubmit: 506 Solved: 189[Submit][Status] ...
- Visual Studio快捷键小结
工欲善其事必先利其器,这句话相信大家都听说过.利其器,就是我们先得有个神器,神器就是VS(号称宇宙第一IDE),有了神奇不会用也是白搭,就像你有了倚天剑和屠龙刀你不会使,它也就是废铁(假设它们是铁做的 ...
- POJ 2409 Let it Bead(Polya定理)
点我看题目 题意 :给你c种颜色的n个珠子,问你可以组成多少种形式. 思路 :polya定理的应用,与1286差不多一样,代码一改就可以交....POJ 1286题解 #include <std ...
- nyist 740 “炫舞家“ST(动态规划)
dp[i][j][k]:表示第i次踩踏后两脚的位置j,k 先固定一只脚的位置j,第i次踩踏后,状态为dp[i][j][a[i]]或者dp[i][a[i]][j],其中a[i]表示第i个输入的元素,则有 ...
- postMan 使用
Postman功能(https://www.getpostman.com/features) 主要用于模拟网络请求包 快速创建请求 回放.管理请求 快速设置网络代理 安装 下载地址:https://w ...
- Learning WCF Chapter2 Messaging Protocols
In Chapter 1,you were introduced to fundamental WCF concepts, 在章节1中,学习了wcf中的基础概念including how t ...
- c#中使用SESSION需要注意的几个问题
C#的SESSION和其它程序中的SESSSION可能有一点的不同,下面讲下哪飞网程序员遇到的一个地方使用SESSION的问题.希望对大家有所帮助 一.在页面中用SESSION,存值session[& ...