简体   繁体   English

将表从数据库从其他服务器上的MySQL Server复制到我的计算机

[英]Copying tables from database from MySQL Server on different server to my computer

I need to use a table of a database on a university server for test data for a project. 我需要使用大学服务器上的数据库表来获取项目的测试数据。 I'm pretty new to databases and MySQL. 我对数据库和MySQL很陌生。 My professor has send me username and password for the server. 我的教授已将服务器的用户名和密码发送给我。 And an extra username&password for the MySQL server. 以及MySQL服务器的额外用户名和密码。 It took me a while but in the end I was able to connect to the server over ssh and then managed to navigate to $cd / $cd usr/bin/MySQL then logged in and found the data/sentences in a table in one of the databases. 我花了一段时间,但最终我能够通过ssh连接到服务器,然后设法导航到$ cd / $ cd usr / bin / MySQL,然后登录并在其中一个表的表中找到了数据/句子。数据库。

Now there is the question: How do I get the data on my computer? 现在有一个问题:如何在计算机上获取数据? I thought about a python script. 我想到了一个python脚本。 But I cannot write a script what is logging in on a different server and then navigates to the MySQL folder to log in there to copy somehow the sentences in the table to a txt file I can use? 但是我无法编写脚本,这是什么意思在另一台服务器上登录,然后导航到MySQL文件夹以在那里登录以某种方式将表中的句子复制到我可以使用的txt文件中?

You could use the mysqldump program (if it is available on your machine) to get a dump of the table you need. 您可以使用mysqldump程序(如果您的计算机上有此程序)来获取所需表的转储。

if you would like to have the dump as SQL so you could insert it on your own database, you would call it with 如果您希望将转储作为SQL使用,以便可以将其插入到自己的数据库中,则可以使用

mysqldump -u "USERNAME" -p "PASSWORD" -h "HOSTNAME" "DATABASE" "TABLE" > dump.sql

Omitting the password is strongly preferred so your password isn't saved in the shell history. 强烈建议您省略密码,这样您的密码就不会保存在Shell历史记录中。 You'll be prompted for it during execution. 在执行过程中将提示您输入。

and you would get a file with all SQL-Queries to recreate this table. 并且您将获得一个包含所有SQL查询的文件以重新创建该表。 You can as well export data from that table as CSV or XML, depending on the parameters used at call time. 您还可以根据呼叫时使用的参数,以CSV或XML格式从该表中导出数据。

if the database server would only be accessible from the second server you mentioned, but you can access this one via ssh, then you could try calling 如果只能从您提到的第二台服务器访问该数据库服务器,但是可以通过ssh访问该服务器,则可以尝试调用

ssh USER@HOST 'mysqldump --version'

which hopefully presents you with an output similar to this: 希望能为您提供类似于以下的输出:

mysqldump  Ver xx.yy Distrib x.y.z, for debian-linux-gnu (x86_64)

if so, you would know there is this program installed and you could use the following line 如果是这样,您将知道已安装此程序,可以使用以下行

ssh USER@HOST 'mysqldump -u "USERNAME" -p "PASSWORD" -h "HOSTNAME" "DATABASE" "TABLE"' | cat > dump.sql

which should present you the dump on your local machine. 这将向您显示本地计算机上的转储。

You might not need to ssh into the remote server; 您可能不需要SSH到远程服务器。 depending how their server and database are set up you may be able to connect a mysql client on your local machine to the database server. 根据服务器和数据库的设置方式,您也许可以将本地计算机上的mysql客户端连接到数据库服务器。 While there are security advantages to limiting where connections are permitted from, accessing a database at localhost is actually just a special case. 虽然限制从何处允许连接具有安全优势,但实际上访问localhost的数据库只是一种特殊情况。

You might not even need a python script. 您甚至可能不需要python脚本。 You can export directly from mysql to a text file, or your client may have a feature to copy data directly from the remote server into a local database. 您可以直接从mysql导出到文本文件,或者您的客户端可能具有直接将数据从远程服务器复制到本地数据库的功能。

I would guess that something like this would work for you, although getting the output into the format you want can be tricky: 我想这样的事情对您来说很有效,尽管将输出转换成您想要的格式可能很棘手:
mysql -h "host address" -u "username" -p -e "SELECT * FROM `table`" > localFile.txt

If you wanted to do it with a python script running on the server as you're describing, you'll want to use the ssh credentials to do FTP over SSH to get the files back and forth. 如果您要使用您所描述的在服务器上运行的python脚本来执行此操作,则需要使用ssh凭据通过SSH进行FTP来回传输文件。 Your FTP client will certainly support that. 您的FTP客户端肯定会支持。

暂无
暂无

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

相关问题 从服务器复制文件 - Copying a file from a server 如何从另一台计算机访问我的数据库 mysql - how access my database mysql from another computer 尝试从我的本地服务器访问数据库 - Trying to access database from my local server 使用 Python 在 SQL Server 中将表从一个数据库复制到另一个数据库 - Copy tables from one database to another in SQL Server, using Python 如何从本地计算机连接到部署在计算引擎上的mysql服务器? - How to connect to mysql-server deployed on compute engine from local computer? 将文件从用户计算机传输到其他服务器,而无需通过GAE - Transferring files from user's computer to different server without going through GAE 通过 python/SQLAlchemy 从具有 MySQL 8 的计算机与 MySQL 5.1 数据库进行交互 - Interacting with MySQL 5.1 database from computer with MySQL 8 via python/SQLAlchemy 如何使用python脚本从树莓派访问Windows桌面上wamp服务器中的外部mysql数据库? - How to access external mysql database in wamp server on my windows desktop from my raspberry pi using python script? 从 Azure 外部访问 Azure Database for MySQL 单一服务器 - Accessing an Azure Database for MySQL Single Server from outside Azure 如何从我的本地计算机(client.py)发送一个简单的 UDP 消息到远程服务器 pythonanywhere(server.py) - How to send a simple UDP message from my local computer(client.py) to a remote server pythonanywhere(server.py)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM