Database Migration Service 提供了一个 REST API,用于以编程方式管理您的连接配置文件和迁移作业。REST API 由与创建和管理连接配置文件和迁移作业关联的资源定义。如需详细了解所有资源及其方法,请参阅 Database Migration Service API 参考文档。
当直接向 Database Migration Service REST API 发送请求时,必须创建正确的请求形式,以经过身份验证的用户的身份授权请求,并处理返回的所有响应。本文档中的许多任务提供了使用 cURL 的 API 示例。
您必须具有使用 Database Migration Service API 所需的权限。这些权限包括:
Database Migration Admin:拥有此权限,用户可以获取用于管理连接配置文件和迁移作业的所有 Database Migration Service 资源。
Database Migration Viewer:利用此权限,用户对 Database Migration Service 中的连接配置文件和迁移作业拥有只读权限。
Cloud SQL 管理员:拥有此权限的用户可以在 Database Migration Service 中为 Cloud SQL 创建和管理连接配置文件和迁移作业。
Google Cloud 提供 Identity and Access Management (IAM),可让您授予对特定 Google Cloud 资源的更细化访问权限,并防止对其他资源进行不必要的访问。您可以使用 Google Cloud console、API 的 IAM 方法或 Google Cloud CLI 来获取和设置 Database Migration Service 的权限。如需了解详情,请参阅授予、更改和撤消对资源的访问权限。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-18。"],[[["\u003cp\u003eDatabase Migration Service offers a REST API for programmatically managing connection profiles and migration jobs, which is useful for users who prefer programmatic control.\u003c/p\u003e\n"],["\u003cp\u003eWhile direct API interaction is possible, the Google Cloud CLI and Console offer a simpler way to manage connection profiles and migration jobs, especially for users new to Database Migration Service.\u003c/p\u003e\n"],["\u003cp\u003eEnabling the Database Migration Service API requires selecting the project in the Google Cloud console, or using specific \u003ccode\u003egcloud\u003c/code\u003e commands.\u003c/p\u003e\n"],["\u003cp\u003eUsers need specific permissions like Database Migration Admin, Database Migration Viewer, or Cloud SQL Admin to utilize the Database Migration Service API effectively.\u003c/p\u003e\n"],["\u003cp\u003eFor enhanced security when using the API with cURL, sensitive data can be stored in a JSON file and referenced, instead of directly including it in the command line.\u003c/p\u003e\n"]]],[],null,["# Use the Database Migration Service API\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nMySQL \\| [PostgreSQL](/database-migration/docs/postgres/use-the-database-migration-service-api \"View this page for the PostgreSQL version of Database Migration Service.\") \\| [PostgreSQL to AlloyDB](/database-migration/docs/postgresql-to-alloydb/use-the-database-migration-service-api \"View this page for the PostgreSQL to AlloyDB version of Database Migration Service.\")\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nOverview\n--------\n\nDatabase Migration Service provides a REST API for administering your connection profiles and migration jobs programmatically. The REST API is defined by resources associated with creating and managing connection profiles and migration jobs. For details about all resources\nand their methods, see the\n[Database Migration Service API Reference](/database-migration/docs/reference/rest).\n\nWhen you send requests directly to the Database Migration Service REST API, you must create the\ncorrect form of the request, authorize the request as an authenticated user, and\nprocess any responses returned. Many of the tasks in this documentation provide\nAPI examples using [cURL](http://curl.haxx.se).\n\nFor more examples of using the APIs, see the page for the request in the\n[Database Migration Service API Reference](/database-migration/docs/reference/rest). Each page has\nexamples calling the API in several programming languages, as well as a\nrequest-specific Explorer to help you see what goes into a well-formed request\nand what to expect in the response.\n\nYou're also using the Database Migration Service API, indirectly, when you use any of the\nfollowing ways of administering instances:\n\n- [Google Cloud CLI](/sdk): A command-line interface (CLI) that you can use to work with your instances.\n\n- [Google Cloud console](https://console.cloud.google.com/): A graphical web interface.\n\nThe advantage of using these methods, especially the\nGoogle Cloud console, is that they can greatly simplify administering your\nconnection profiles and migration jobs (depending on your use case). If you're just starting out with\nDatabase Migration Service, then we recommend that you start with one of these tools first\nbefore working with the REST API directly.\n\nEnable the API\n--------------\n\nTo use the Database Migration Service API, you need to enable it: \n\n### Console\n\n1. [Enable the Database Migration Service API](https://console.cloud.google.com/apis/library/datamigration.googleapis.com).\n2. Select your project.\n3. Select **Continue**.\n\n### gcloud\n\n1. Enter the following to display the project IDs for your Google Cloud projects: \n\n ```bash\n gcloud projects list\n ```\n2. Set your default project: \n\n ```bash\n gcloud config set project YOUR_PROJECT_ID\n ```\n3. Enable the Cloud SQL Admin API: \n\n ```bash\n gcloud services enable datamigration.googleapis.com\n ```\n\nPermissions\n-----------\n\nYou must have the required permissions to use the Database Migration Service API. These permissions include:\n\n- Database Migration Admin: With this permission, a user gains access to all resources of Database Migration Service used to manage connection profiles and migration jobs.\n- Database Migration Viewer: With this permission, a user has read-only access to connection profiles and migration jobs in Database Migration Service.\n- Cloud SQL Admin: With this permission, a user can create and manage connection profiles and migration jobs for Cloud SQL in Database Migration Service.\n\nGoogle Cloud offers Identity and Access Management (IAM), which lets you give more granular access to specific Google Cloud resources and prevents unwanted access to other resources. You can get and set permissions for Database Migration Service using the Google Cloud console, the IAM methods of the API, or the Google Cloud CLI. For more information, see [Granting, changing, and revoking access to resources](/iam/docs/granting-changing-revoking-access).\n\nAPI examples\n------------\n\nFor examples of using the Database Migration Service API with cURL, see [Manage connection profiles using the API](/database-migration/docs/mysql/api-connection-profiles) and [Manage migration jobs using the API](/database-migration/docs/mysql/api-migration-jobs).\n\nProvide JSON data from a file\n-----------------------------\n\nWhen you use the API with cURL, you provide property values using the command\nline. If you're working with sensitive values such as passwords or security\nkeys, providing them on the command line poses a security risk. For increased\nsecurity, you can create a file containing the JSON data for the API call, and\nprovide the path to the file on the command line.\n\nTo provide JSON data to your cURL API call from a file:\n\n1. Create a file containing everything enclosed in single quotes for the\n `--data` field.\n\n Include the curly brackets, but don't include the single quotes.\n2. At the command line, provide the path to the file, preceded by the\n `@` character, as the `--data` parameter:\n\n ```bash\n --data @\u003cpath-to-file\u003e/\u003cfilename\u003e\n ```\n\n For example, to create a connection profile for a Cloud SQL source database, you could create a file named `request.json` with the following content: \n\n ```\n {\n \"displayName\": \"my Cloud SQL connection profile\",\n \"mysql\": {\n \"host\": \"10.11.12.13\",\n \"port\": 3306,\n \"username\": \"myusername\",\n \"password\": \"mypassword\"\n }\n }\n ```\n\n Then, you would use the following cURL command at the command line: \n\n ```bash\n curl --header \"Authorization: Bearer ${ACCESS_TOKEN}\" \\\n --header 'Content-Type: application/json' \\\n --data @./request.json \\\n -X POST \\\n https://datamigration.googleapis.com/v1/projects/myProject/locations/myLocation/connectionProfiles?connectionProfileId=myCloudSQLconnectionprofile\n ```\n\nWork with long-running API calls\n--------------------------------\n\nSome API calls, such as object creation, can take some time to complete. Because\nthe API is asynchronous, the call returns immediately, even if the operation is\nstill in progress. If a subsequent API call uses the newly created object, then you\nmust wait for the first operation to complete before proceeding.\n\nYou can use the Database Migration Service API to [manage operations](/sdk/gcloud/reference/database-migration/operations). This includes:\n\n- [Showing details about an operation](/sdk/gcloud/reference/database-migration/operations/describe)\n- [Listing operations for a region](/sdk/gcloud/reference/database-migration/operations/list)\n- [Deleting an operation](/sdk/gcloud/reference/database-migration/operations/delete)\n\nDatabase Migration Service and Google APIs Discovery Service\n------------------------------------------------------------\n\n[Google APIs Discovery Service](/discovery) is a service that you can use to\ndiscover Google APIs. For example, when you use the [Google APIs Explorer](/apis-explorer) tool, you're using the Discovery Service.\n\nIn the Discovery Service, Database Migration Service is represented as \"datamigration\" (for example:\nhttps://**datamigration**.googleapis.com/$discovery/rest?version=v1)."]]