如何自动将特定的 MySQL 表从一台机器移动到另一台机器?
我有一个 MySQL 数据库,其中包含 "shard_0"
、"shard_1
"、"shard_2"
等形式的表。
这些是虚拟分片。现在我想添加另一个数据库服务器并移动偶数编号的分片("shard_0"
、"shard_2"
、"shard_4"
、. ..) 到新机器。
最好的方法是什么?有很多表,因此理想情况下我不必单独键入每个表名称,而是自动执行某些操作。也许是这样的:
# pseudo code
for i in range(n):
tablename = "shard_"+str(2*i)
# Move tablename to new machine
谢谢
I have a MySQL database with tables in the form of "shard_0"
, "shard_1
", "shard_2"
, etc.
These are virtual shards. Now I want to add another DB server and move the even-numbered shards ("shard_0"
, "shard_2"
, "shard_4"
, ...) to the new machine.
What is the best way to do that? There are many tables so ideally I wouldn't have to type out each table name individually but do something automatically. Perhaps something like:
# pseudo code
for i in range(n):
tablename = "shard_"+str(2*i)
# Move tablename to new machine
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将创建一个(或多个)mysqldump 调用,就像这样
在 shell 中运行此命令,并将转储文件移动到新机器,然后通过 mysql 在那里运行它。
然后为已移动的表生成并运行“删除表”语句。
I'd create a single (or perhaps multiple) mysqldump invocations, like so
Run this command in a shell, and move the dump file to the new machine, then run it there through mysql.
Then generate and run the "drop table" statements for the tables you have moved.
我不确定我是否看到了问题,但如果我做对了,您可以使用 Python 生成导出 SQL 脚本,并为另一台机器生成导入脚本。
这将为您省去手动操作的麻烦。至于您的代码片段,我认为将数据库从服务器迁移到另一服务器的最佳方法是使用引擎自身的功能。
I'm not sure I see the problem, but if I got it right, you can use Python to generate the export SQL script, and the import one for the other machine.
That'll save you the trouble of doing it manually. As for your code snippet, I think the best way to go about migrating a database from a server to another one is using the engine's own capabilities.