环境准备
另一个部署slave服务
关闭主从节点的防火墙
master
import paramiko
def master(): ssh=paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname='192.168.253.168',port=22,username='root',password='369369ynx') stdin,stdout,stderr = ssh.exec_command("sed -i -e '12aserver_id=1' -e '13alog_bin=MysqL_bin' /etc/my.cnf.d/server.cnf") stderr,stdin,stdout=ssh.exec_command('systemctl restart mariadb') stderr,stdin,stdout=ssh.exec_command("MysqL -uroot -p1 -e 'CREATE USER 'slave'@'%' IDENTIFIED BY 'slave';") stderr,stdin,stdout = ssh.exec_command('''MysqL -uroot -p1 -e "grant replication slave on *.* to 'slave'@'%' identified by 'slave'"''') stderr,stdin,stdout=ssh.exec_command('''MysqL -uroot -p1 -e "show master status" ''') master()
在linux端查看是否配置成功。并且根据这两个变量来设置slave。
MysqL -uroot -p1 -e 此命令可以使用paramiko模块直接执行sql语句。e是edit的意思。
当然,也可以使用pymsql模块连接MysqL数据库然后利用cur游标里封装的execute方法来执行sql语句,但是可能在执行给与权限的命令时会报错,因为远程登陆并没有权限这么做。
slave
如下的file变量和port变量在master节点会变动,特别时重启数据库后。
如果查看slave status,显示:
Slave_IO_Running: No
Slave_sql_Running: No
或者一个YES一个connecting的话,这是因为主节点的master状态发生了变化或者两台主机的某一台防火墙没有关闭。
ip='192.168.253.168' file='MysqL_bin.000012' port=29809 import paramiko def slave(): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname='192.168.253.190', port=22, username='root', password='369369ynx') slave1 = ssh.exec_command("sed -i -e '12aserver_id=2' /etc/my.cnf.d/server.cnf") stderr,stdin,stdout=ssh.exec_command('systemctl restart mariadb') slave2 = ssh.exec_command('''MysqL -uroot -p1 -e "CHANGE MASTER TO MASTER_HOST=%s, MASTER_USER='slave', MASTER_PASSWORD='slave', MASTER_PASSWORD='slave', MASTER_LOG_FILE=%s, MASTER_LOG_POS=%s; "''' % (ip,file,port)) slave3=ssh.exec_command("MysqL -uroot -p1 -e 'start slave;'") slave()
结果显示:
检测:
master
slave
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。