MySQL 将mysql数据库从一台计算机复制到另一台计算机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22447651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copying mysql databases from one computer to another
提问by Homen
I want to copy my mysql database from my computer to another computer. How can I do this?
我想将我的 mysql 数据库从我的计算机复制到另一台计算机。我怎样才能做到这一点?
采纳答案by jmail
How to copy Mysql database from one Computer to another / backup database using mysqldump
如何使用mysqldump将Mysql数据库从一台计算机复制到另一台/备份数据库
- We can transfer a MySQL database from one PC to another PC using mysqldump command.
We have to create dump file of database to transfer database from one PC to another PC.
MySQL database is not portable database i.e. we cannot transfer it from one PC to another PC by copying and pasting it.
We can use following method to transfer database.
Creating a dumpfile from database/ Taking backup of MySQL database:
Open command prompt.
Execute following commands to change directory
c: “press enter”
cd program files/MySQL/MySQL Server 5.1/ bin “press enter”
mysqldump -u root -p database_name > database_name.sql “press enter”
Enter password: password of MySQL
- 我们可以使用 mysqldump 命令将 MySQL 数据库从一台 PC 传输到另一台 PC。
我们必须创建数据库的转储文件以将数据库从一台 PC 传输到另一台 PC。
MySQL 数据库不是便携式数据库,即我们不能通过复制和粘贴将它从一台 PC 转移到另一台 PC。
我们可以使用以下方法来传输数据库。
从数据库创建转储文件/备份 MySQL 数据库:
打开命令提示符。
执行以下命令更改目录
c:“按回车”
cd program files/MySQL/MySQL Server 5.1/bin “回车”
mysqldump -u root -p database_name > database_name.sql “按回车”
输入密码:MySQL的密码
Copy sql file and paste it in PC where you want to transfer database.
复制sql文件并将其粘贴到要传输数据库的PC中。
2. Dumping sql file into database:-
- Open MySQL command line client command prompt.
- Execute following command to create database.
create database database_name;
创建数据库database_name;
“press enter” Database name is must as that of your database_name.
“press enter” 数据库名称必须与您的database_name 相同。
Copy that sql file into location “c:/program files/MySQL/MySQL Server 5.1/bin”
将该 sql 文件复制到位置“c:/program files/MySQL/MySQL Server 5.1/bin”
*- Now open command prompt and execute following commands.*
>C: “press enter”
>cd program files/MySQL/MySQL Server5.1/bin “press enter”
>mysql –u root –p database_name < database_name.sql “press enter”
Your database is created on PC.
Now in MySQL command prompt check your database.
Another one:1
另一个:1
This best and the easy way is to use a db tools(SQLyog)
这个最好和最简单的方法是使用数据库工具(SQLyog)
http://www.webyog.com/product/downloads
http://www.webyog.com/product/downloads
With this tools you can connect the 2 databases servers and just copy one database on server a to server b.
使用此工具,您可以连接 2 个数据库服务器,只需将服务器 a 上的一个数据库复制到服务器 b。
For more info
欲了解更多信息
http://faq.webyog.com/content/12/32/en/mysql-5-objects-are-greyed-out-in-copy-db-to-other-host-dialogue.htmlenterimage description here
Another one:2
另一个:2
For a database named "lbry", try this:
对于名为“lbry”的数据库,试试这个:
mysqldump -u root -p lbry > dump-lbry.sql
Create a database of the same name ("lbry" in this example) on the computer to which you wish to copy the database contents
在您希望将数据库内容复制到的计算机上创建一个同名的数据库(在本例中为“lbry”)
Then import it:
然后导入:
mysql -u root -p lbry < dump-lbry.sql
回答by Abhilab-Systematix
You can do by this process step-by-step using MySQL WorkBench.
您可以使用 MySQL WorkBench 逐步完成此过程。
- Install MySQL Workbench
- Connect to existing Database
- Go to Navigator -> Management -> Data Export. (this will dump queries of tables one by one in a separate folder, Workbench uses the same folder to import)
- Create Database on target PC.
- Connect to Target Database (would consist of 0 tables in DB)
- Go to Navigator -> Management -> Data Import/Restore. (this will use the dump folder and create tables in your target Database).
- 安装 MySQL 工作台
- 连接到现有数据库
- 转到导航器 -> 管理 -> 数据导出。(这会将表的查询一一转储到单独的文件夹中,Workbench 使用同一文件夹导入)
- 在目标 PC 上创建数据库。
- 连接到目标数据库(将由 DB 中的 0 个表组成)
- 转到导航器 -> 管理 -> 数据导入/恢复。(这将使用转储文件夹并在目标数据库中创建表)。
Hope this helps.
希望这可以帮助。
回答by cvkline
The only SAFE way to copy databases from one machine to another is to first quiesce the database (make sure no clients are modifying it), then use the mysqldump
command to create a text representation of your schema and the contents of your tables. Then copy that text file over to the other machine and read it in by specifying it as the input to the mysql
command.
将数据库从一台机器复制到另一台机器的唯一安全方法是首先停顿数据库(确保没有客户端正在修改它),然后使用该mysqldump
命令创建模式和表内容的文本表示。然后将该文本文件复制到另一台机器上,并通过将其指定为mysql
命令的输入来读取它。
Attempting to copy the actual mysql data directories over is asking for trouble, since they are dependent on the architecture of the machine that mysql is running on and likely on the version of mysql and whatever storage engine is in use.
尝试复制实际的 mysql 数据目录是自找麻烦,因为它们取决于运行 mysql 的机器的架构,并且可能取决于 mysql 的版本和正在使用的任何存储引擎。
回答by amarjit singh
- This tutorial is in Ubuntu but will work on Redhat, Centos, Fedora, Suse
- We can dump database, transfer it to another server, and restore it
- It will show how to take care of things like modified credentials as a result and moving debain.cnf file 4 dump restore will slow down the serverHow it works
- 本教程使用的是 Ubuntu,但适用于 Redhat、Centos、Fedora、Suse
- 我们可以转储数据库,将其转移到另一台服务器,然后恢复它
- 它将展示如何处理诸如修改凭据之类的事情以及移动 debain.cnf 文件 4 转储还原会减慢服务器的工作原理
4.1 Run mysqldump on source server:this builds a MySQL executable script for the destination server. During this time the MySQL server will queue queries 4.2 Copy dump file to the destination server 4.3 Empty destination server 4.4 Execute dump file on the destintion server
4.1 在源服务器上运行 mysqldump:这将为目标服务器构建一个 MySQL 可执行脚本。在此期间,MySQL 服务器将对查询进行排队 4.2 将转储文件复制到目标服务器 4.3 清空目标服务器 4.4 在目标服务器上执行转储文件
Server A(Source Server) Server B (Destination Server)
服务器 A(源服务器) 服务器 B(目标服务器)
Case 1:Server A
案例1:服务器A
root@source$ mysql --defaults-file=/etc/mysql/debain.cnf
mysql>show databases;
mysql>use testdb;(The database to dump)
mysql>show tables;(To Check the tables)
mysql>^c
-- now dump the databses
-- 现在转储数据库
root@surce$ mysql --defaults-file=/etc/mysql/debain.cnf --all-databses | gzip -c > dump.sql.gz
root@surce$ gzip -dc dump.sql.gz
To copy the files create a ssh key on the source server
要复制文件,请在源服务器上创建 ssh 密钥
root@surce$ ssh-keygen
root@surce$ cat /root/.ssh/id_rsa.pub
select and copy all the ssh key string
root@surce$ scp dump.sql.gz ubuntu@destination:
goto destination server
转到目标服务器
last step copy the contents of debain.cnf file
最后一步复制debain.cnf文件的内容
root@surce$ cat /etc/mysql/debain.cnf
[client]
host = localhost
user = debain-sys-maint
password = mysecret
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debain-sys-maint
password = mysecret
socket = /var/run/mysqld/mysqld.sock
basedir = /usr
select all and copy this file to detination server.
全选并将此文件复制到目标服务器。
Note: The sockey path can be different in your machine .use locate command to find the exact path
注意:sockey 路径在您的机器中可能不同。使用 locate 命令查找确切路径
Case 2. Server B drop all databses
案例 2. 服务器 B 删除所有数据库
root@destination$ echo show databases | mysql --defaults-file=/etc/mysql/debian.cnf --skip-column-names | awk '{print "drop database "";"}'
if this command doesnot drop databses use it with -force option
如果此命令不删除数据库,请将其与 -force 选项一起使用
root@destination$ echo show databases | mysql --defaults-file=/etc/mysql/debian.cnf --skip-column-names | awk '{print "drop database "";"}' | mysql --defaults-file=/etc/mysql/debian.cnf -f
copy the ssh key on the destination server
复制目标服务器上的 ssh 密钥
root@destination$ echo "paste the key here" >> /home/ubuntu/.ssh/authorised_keys
root@destination$ echo "将密钥粘贴在这里" >> /home/ubuntu/.ssh/authorised_keys
goto source Server and use scp command to move the dump on the destination server
转到源服务器并使用 scp 命令移动目标服务器上的转储
(inject the file)
(注入文件)
root@destination$ gzip -dc /home/ubuntu/dump.sql.gz | mysql --defaults-file=/etc/mysql/debain.cnf
root@destination$ > /etc/mysql/debain.cnf
root@destination$ nano /etc/mysql/debain.cnf
paste the contents of .cnf file from source server here and save the file :x
将来自源服务器的 .cnf 文件的内容粘贴到此处并保存文件 :x
root@destination$ mysql --defaults-file= /etc/mysql/debain.cnf
if you get the mysql prompt then everything should be working file
如果你得到 mysql 提示,那么一切都应该是工作文件
mysql>
回答by Casivio
I was able to restore a backup that was shared with me following this thread, specifically @jmail's answer, but, I thought that I could provide a bit more concise answer for future users. I received a dump file with a .sql extension, not a .dump extension as I would have expected.
我能够恢复在此线程后与我共享的备份,特别是@jmail 的答案,但是,我认为我可以为未来的用户提供更简洁的答案。我收到了一个带有 .sql 扩展名的转储文件,而不是我预期的 .dump 扩展名。
I tried to place it in my project folder and restore it but I got error 22, referring to access privileges. I moved it to “c:/program files/MySQL/MySQL Server 5.1/bin” and then ran it by:
我试图将它放在我的项目文件夹中并恢复它,但出现错误 22,指的是访问权限。我将它移动到“c:/program files/MySQL/MySQL Server 5.1/bin”,然后通过以下方式运行它:
1) Starting MySQL in the command prompt.
1) 在命令提示符下启动 MySQL。
2) Creating the new database that I wanted to restore to
2)创建我想恢复到的新数据库
3) Switching to the database
3)切换到数据库
USE new_DB;
4) Running
4) 跑步
source c:/program files/MySQL/MySQL Server 5.1/bin/backup.sql
I'm not sure how the backup.sql file was created but this worked for restoring it on my Windows 10 system.
我不确定 backup.sql 文件是如何创建的,但这适用于在我的 Windows 10 系统上恢复它。
回答by Rajat
mysqldump --databases dbname -hsource_server_ip -usource_server_userName -psource_server_passcode | mysql
-udest_server_user_name -pdest_server_user_passcode &
mysqldump --databases dbname -hsource_server_ip -usource_server_userName -psource_server_passcode | mysql
-udest_server_user_name -pdest_server_user_passcode &
There are three general ways to invoke mysqldump:
调用mysqldump的一般方法有3种:
shell> mysqldump [options] db_name [tbl_name ...]
shell> mysqldump [options] --databases db_name ...
shell> mysqldump [options] --all-databases
If you do not name any tables following db_name or if you use the --databases or --all-databases option, entire databases are dumped.
如果您没有在 db_name 之后命名任何表,或者如果您使用 --databases 或 --all-databases 选项,则整个数据库都将被转储。
mysqldump does not dump the INFORMATION_SCHEMAdatabase by default. MariaDB dumps the INFORMATION_SCHEMAif you name it explicitly on the command line, although currently you must also use the --skip-lock-tables option.
默认情况下,mysqldump 不会转储INFORMATION_SCHEMA数据库。如果您在命令行中明确命名,MariaDB 会转储INFORMATION_SCHEMA,但目前您还必须使用 --skip-lock-tables 选项。
To see a list of the options your version of mysqldumpsupports, execute mysqldump --help.
要查看您的mysqldump版本支持的选项列表,请执行 mysqldump --help。