简体   繁体   English

mysql中的master到slave复制

[英]master to slave replication in mysql

I need master to slave replication in mysql. 我需要mysql中的master到slave复制。

so I am creating this procedure to change the master dynamically by procedure 所以我创建此过程以通过过程动态更改主服务器

delimiter //

CREATE PROCEDURE change_master( in host_ip varchar(50))

begin

stop slave;

CHANGE MASTER TO MASTER_HOST = host_ip, MASTER_PORT=3306, MASTER_USER='replication', MASTER_PASSWORD='slave';

start slave;

end;
//

but I am getting a error. 但是我收到了一个错误。

ERROR 1064 (42000): You have an error in your SQL syntax; 错误1064(42000):您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'host_ip, MASTER_PORT=3306, MASTER_USER='replication', MASTER_PASSWORD='slave'; 检查与MySQL服务器版本对应的手册,以便在'host_ip,MASTER_PORT = 3306,MASTER_USER ='复制',MASTER_PASSWORD ='slave'附近使用正确的语法; s' at line 4 在第4行

If I left it blank then fine 如果我把它留空,那就好了

eg. 例如。

CHANGE MASTER TO MASTER_HOST = '', MASTER_PORT=3306, MASTER_USER='replication', MASTER_PASSWORD='slave';

I tried many time but in this query I am not able to use any variable why? 我尝试了很多次,但在这个查询中我无法使用任何变量为什么?

If you know help me. 如果你知道帮助我。

thanks . 谢谢 。

The host value must be enclosed in quotes. 主机值必须用引号括起来。 I think this cannot be possible unless you use a prepared statement. 我认为除非你使用准备好的声明,否则这是不可能的。

delimiter //

CREATE PROCEDURE change_master( in host_ip varchar(50))

begin

set @ssql:=concat("CHANGE MASTER TO MASTER_HOST = '",host_ip,"', MASTER_PORT=3306, MASTER_USER='replication', MASTER_PASSWORD='slave'");

stop slave;

prepare sql_stm from @ssql;
execute sql_stm;
deallocate prepare sql_stm;

start slave;

end//
delimiter ;

Regards! 问候! Tinel Barb Tinel Barb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM