Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
107 views
in Technique[技术] by (71.8m points)

python - select * from one machine insert to another

There are 2 machines

server = 192.168.10.10
slave = 192.168.10.100

they have data base named test1 also have same tables I want to select all from a table(base2) on a database(test1) on server and drop the same table(base2) on slave and insert all data to it every X min .

conMaster = pymysql.connect(host=masterIp1, user=masterUser1, password=masterPass1, 
database=dataBase1)
conSlave = pymysql.connect(host=slaveIp1, user=slaveUser1, password=slavePass1, database=dataBase1)
curMaster = conMaster.cursor()
curSlave = conSlave.cursor()
#consider we know the table name and in both servers table name is base2
masterData=curMaster.execute("select * from base2")
SlaveData=curSlave.execute("select * from base2")
#if base2 on master server has 10 more rows than slave master
#consider database name is test1 in both servers
if len(masterData)>len(SlaveData)+10:
   curSlave.execute("drop table base2"
   "create table test1.base2"
   "insert test1.base2"
   "select * from ??"
   )

since cur.slave.execute references to slave server, when I want to select * from base2 on master server I don't know how to write the code.

question from:https://stackoverflow.com/questions/65829254/select-from-one-machine-insert-to-another

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It looks like you want to replicate that table from master to slave. Maybe this helps you: Replicate just one table on slave from master in mysql


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...