如何将远程服务器MYSQL数据库导出导入
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将远程服务器MYSQL数据库导出导入相关的知识,希望对你有一定的参考价值。
参考技术A mysql数据库导出第一种利用空间商提供的phpMYADMIN管理工具导出,这种较为简单,大家查一下资料这里就不多讲了.第二种利用MYSQL安装时自带的mysqldump管理工具导出:1)左下角开始菜单-运行-CMD进入DOS命令行状态2)输入:CD D:/Program Files/MySQL/MySQL Server 5.0/binD:/Program Files/MySQL/MySQL Server 5.0/bin为你安装的MYSQL安装目录,/bin为mysqldump管理工具所有在的目录;3)导出语法为:mysqldump -h[hosname] -u[user_name] -p[password] --default-character-set=[char_set_name] [db_name] [save_path]例:然后输入:mysqldump -h119.12.12.11 -umysql-pmysql123--default-character-set=utf8 aspchina --skip-lock-tables d:/aspchina_net.sql119.12.12.11为远程服务器IP,-umysql mysql为数据库用户名,-pmysql123 mysql123 为用户密码,set=utf8为导出MYSQL的编码格式,aspchina为要导出的数据库名,d:/aspchina_net.sql 为导入到你本地的存放路径,aspchina_net.sql你可以自由命名!MYSQL数据库导入如果MYSQL数据库小于2MB可以用mysqldump管理工具导入,如果大小2MB就不行了,因为空间商提供的PHPMYADMIN管理工具一般只能导入小于2MB的数据,这令一些使用MYSQL数据库的站长郁闷了!1)左下角开始菜单-运行-CMD进入DOS命令行状态2)D:/Program Files/MySQL/MySQL Server 5.0/bin为你安装的MYSQL安装目录,/bin为mysqldump管理工具所有在的目录;3)然后输入:mysql -h119.12.12.11 -uaspchina -paspchina123456 aspchina< d:/aspchina_net.sql注释:aspchina_net.sql,如果用户没有创建数据库的权限将不能导入aspchina_net.sql数据库,否则spchina_net.sql只能是多张表不然会出错,这点切记!如何通过网络将sql server数据库从sql server导出或导入mysql server
【中文标题】如何通过网络将sql server数据库从sql server导出或导入mysql server【英文标题】:how to export or import sql server database from sql server to mysql server through network 【发布时间】:2013-06-08 04:11:51 【问题描述】:我想通过网络从 mssql 服务器系统连接 mysql 服务器系统,因为我想将 sql 服务器数据库从 mssql 服务器系统导入或导出到 mysql 服务器系统。
谁能告诉我该怎么做?
谢谢
【问题讨论】:
下面的链接会有帮助 [如何将 MS SQL 数据库导出到 MYSQL?][1] [1]:***.com/questions/3917081/… *** 中已经有答案***.com/questions/3917081/… 【参考方案1】:尝试使用此脚本,它允许您导入数据。您必须指定表名并在您的 MySQL 服务器上运行生成的代码。
DDL:
CREATE TABLE Labour.Employee
(
EmployeeID INT IDENTITY (1, 1) PRIMARY KEY NOT NULL
, IsMainWorkPlace BIT NOT NULL
, TabelNumber VARCHAR(6) NULL
, PersonID INT NOT NULL
, DepartmentUID UNIQUEIDENTIFIER NOT NULL
, DateIn DATETIME NOT NULL
, DateOut DATETIME NULL
, PositionID INT NULL
, DatePension DATETIME NULL
, IsLabourUnionMember BIT NOT NULL
, FiringReasonID INT NULL
)
查询:
DECLARE
@Table NVARCHAR(50)
, @Schema NVARCHAR(50)
, @ObjectID INT
, @IsImportIdentity BIT = 1
, @IsCreateTable BIT = 1
DECLARE [tables] CURSOR READ_ONLY FAST_FORWARD LOCAL FOR
SELECT
t.name
, s.name
, t.[object_id]
FROM (
SELECT DISTINCT
t.[schema_id]
, t.[object_id]
, t.name
FROM sys.tables t WITH (NOWAIT)
JOIN sys.partitions p WITH (NOWAIT) ON p.[object_id] = t.[object_id]
WHERE p.[rows] > 0
) t
JOIN sys.schemas s WITH (NOWAIT) ON t.[schema_id] = s.[schema_id]
WHERE t.name IN ('<your table>')
OPEN [tables]
FETCH NEXT FROM [tables] INTO
@Table
, @Schema
, @ObjectID
DECLARE
@Name SYSNAME = N''
, @NewName SYSNAME = N''
, @SQLCreate NVARCHAR(MAX) = N''
, @SQLInsert NVARCHAR(MAX) = N''
, @SQLColumns NVARCHAR(MAX) = N''
, @SQLTinyColumns NVARCHAR(MAX) = N''
WHILE @@FETCH_STATUS = 0 BEGIN
SELECT
@Name = @Schema + N'.' + @Table
, @NewName = N'`' + LOWER(@Schema) + N'`.`' + LOWER(@Table) + N'`'
, @SQLCreate = N''
, @SQLInsert = N''
, @SQLColumns = N''
, @SQLTinyColumns = N''
SELECT
@SQLCreate = N'DROP TABLE IF EXISTS ' + @NewName + N';' + CHAR(13) +
N'CREATE TABLE ' + @NewName + CHAR(13) + N'(' + CHAR(13) + STUFF((
SELECT CHAR(9) + N', `' +
c.name + N'` ' + CASE WHEN t.name = N'uniqueidentifier' THEN 'VARCHAR(36)' ELSE UPPER(t.name) END +
CASE WHEN t.name IN (N'nvarchar', N'varchar', N'nchar', N'char', N'varbinary', N'binary')
THEN N'(' + CASE WHEN c.max_length = -1 THEN N'MAX' ELSE CAST(c.max_length AS NVARCHAR(5)) END + N')'
WHEN t.name IN (N'datetime2', N'time2', N'datetimeoffset')
THEN N'(' + CAST(c.scale AS NVARCHAR(5)) + N')'
WHEN t.name = N'decimal'
THEN N'(' + CAST(c.[precision] AS NVARCHAR(5)) + N',' + CAST(c.scale AS NVARCHAR(5)) + N')'
ELSE N''
END + N' ' +
CASE WHEN c.is_nullable = 0 THEN N'NOT NULL' ELSE N'DEFAULT NULL' END + N' ' +
CASE WHEN c.is_identity = 1 THEN N'AUTO_INCREMENT PRIMARY KEY' ELSE N'' END
+ CHAR(13)
FROM sys.columns c
JOIN sys.types t ON c.system_type_id = t.system_type_id AND c.system_type_id = t.user_type_id
WHERE c.[object_id] = o.[object_id]
ORDER BY c.column_id
FOR XML PATH(N''), TYPE, ROOT).value(N'root[1]', N'NVARCHAR(MAX)'), 1, 2, CHAR(9) + N' ') + N');'
, @SQLInsert = N'INSERT INTO ' + @NewName + N' (' + STUFF((
SELECT N', `' + c.name + '`'
FROM sys.columns c
WHERE c.[object_id] = o.[object_id]
AND (c.is_identity = 0 OR @IsImportIdentity = 1)
ORDER BY c.column_id
FOR XML PATH(N''), TYPE, ROOT).value(N'root[1]', N'NVARCHAR(MAX)'), 1, 2, N'') + N')'
, @SQLTinyColumns = STUFF((
SELECT N', ' + c.name
FROM sys.columns c
WHERE c.[object_id] = o.[object_id]
ORDER BY c.column_id
FOR XML PATH(N''), TYPE, ROOT).value(N'root[1]', N'NVARCHAR(MAX)'), 1, 2, N'')
, @SQLColumns = STUFF((SELECT + CHAR(13) +
CASE
WHEN t.name = N'uniqueidentifier'
THEN ' + '', '' + ISNULL('''''''' + CAST([' + c.name + '] AS VARCHAR(MAX)) + '''''''', ''NULL'')'
WHEN t.name IN (N'nvarchar', N'varchar', N'nchar', N'char', N'varbinary', N'binary')
THEN ' + '', '' + ISNULL('''''''' + CAST(REPLACE([' + c.name + '], '''''''', '''''''''''' ) AS NVARCHAR(MAX)) + '''''''', ''NULL'')'
WHEN t.name = N'datetime'
THEN ' + '', '' + ISNULL('''''''' + CONVERT(VARCHAR, [' + c.name + '], 120) + '''''''', ''NULL'')'
ELSE
N' + '', '' + ISNULL(CAST([' + c.name + '] AS NVARCHAR(MAX)), N''NULL'')'
END
FROM sys.columns c
JOIN sys.types t ON c.system_type_id = t.system_type_id AND c.system_type_id = t.user_type_id
WHERE c.[object_id] = o.[object_id]
AND (c.is_identity = 0 OR @IsImportIdentity = 1)
ORDER BY c.column_id
FOR XML PATH(N''), TYPE, ROOT).value(N'root[1]', N'NVARCHAR(MAX)'), 1, 10, N'CHAR(13) + '', ('' +')
FROM sys.objects o
WHERE o.[type] IN (N'V', N'U')
AND o.object_id = @ObjectID
IF @IsCreateTable = 1
PRINT @SQLCreate
ELSE
PRINT N'TRUNCATE TABLE ' + @NewName + N';'
DECLARE @SQL NVARCHAR(MAX) = N'
SET NOCOUNT ON;
DECLARE
@SQL NVARCHAR(MAX) = N''''
, @x INT = 1
, @count INT = (SELECT COUNT(1) FROM ' + @Name + ')
IF EXISTS(
SELECT 1
FROM tempdb.dbo.sysobjects
WHERE ID = OBJECT_ID(''tempdb..#import'')
)
DROP TABLE #import;
SELECT *, ''RowNumber'' = ROW_NUMBER() OVER (ORDER BY ' + @SQLTinyColumns + N')
INTO #import
FROM ' + @Name + N'
WHILE @x < @count BEGIN
SELECT @SQL = ''VALUES '' + STUFF((
SELECT ' + @SQLColumns + N' + '')''' + N'
FROM #import
WHERE RowNumber BETWEEN @x AND @x + 9
FOR XML PATH(N''''), TYPE, ROOT).value(N''root[1]'', N''NVARCHAR(MAX)''), 1, 2, CHAR(13) + '' '') + '';''
PRINT(''' + @SQLInsert + ''')
PRINT(@SQL)
SELECT @x = @x + 10
END'
EXEC sys.sp_executesql @SQL
FETCH NEXT FROM [tables] INTO
@Table
, @Schema
, @ObjectID
END
CLOSE [tables]
DEALLOCATE [tables]
输出:
DROP TABLE IF EXISTS `labour`.`employee`;
CREATE TABLE `labour`.`employee`
(
`EmployeeID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY
, `IsMainWorkPlace` BIT NOT NULL
, `TabelNumber` VARCHAR(6) DEFAULT NULL
, `PersonID` INT NOT NULL
, `DepartmentUID` VARCHAR(36) NOT NULL
, `DateIn` DATETIME NOT NULL
, `DateOut` DATETIME DEFAULT NULL
, `PositionID` INT DEFAULT NULL
, `DatePension` DATETIME DEFAULT NULL
, `IsLabourUnionMember` BIT NOT NULL
, `FiringReasonID` INT DEFAULT NULL
);
INSERT INTO `labour`.`employee` (`EmployeeID`, `IsMainWorkPlace`, `TabelNumber`, `PersonID`, `DepartmentUID`, `DateIn`, `DateOut`, `PositionID`, `DatePension`, `IsLabourUnionMember`, `FiringReasonID`)
VALUES
(165515, 0, '65515', 183994, '9DA9EEC9-F2EC-4FC7-8F21-DF646FDF397E', '1994-03-06 00:00:00', NULL, 35125, '2001-05-01 00:00:00', 1, NULL)
, (165516, 1, '65516', 184184, '9DA9EEC9-F2EC-4FC7-8F21-DF646FDF397E', '1994-12-09 00:00:00', NULL, 35125, NULL, 1, NULL)
, (165517, 1, '65517', 185214, '0BF3F7C4-2590-40D0-880D-923C8D8339A7', '1953-11-02 00:00:00', '2002-10-05 00:00:00', 35125, '1974-08-06 00:00:00', 1, NULL)
, (165518, 1, '65518', 185536, '2CE8E026-FE5B-4BDD-9330-43AB43CE3DE0', '2004-07-12 00:00:00', '2004-12-01 00:00:00', 35129, '2003-08-06 00:00:00', 0, NULL)
, (165519, 1, '65519', 185596, '2CE8E026-FE5B-4BDD-9330-43AB43CE3DE0', '2004-11-17 00:00:00', NULL, 35129, NULL, 0, NULL)
, (165520, 1, '65520', 184912, '0BF3F7C4-2590-40D0-880D-923C8D8339A7', '1988-10-22 00:00:00', '2003-03-05 00:00:00', 35120, NULL, 1, NULL)
, (165521, 1, '65521', 184984, '1D07CF00-A7FA-489D-9D58-286C7E24A7A2', '2001-04-06 00:00:00', NULL, 35120, NULL, 1, NULL)
, (165522, 1, '65522', 185008, '896681F3-B452-410E-AD4A-97DCF4FE6722', '1994-05-11 00:00:00', NULL, 35120, NULL, 1, NULL)
, (165523, 0, '65523', 183993, 'DC5106A4-3649-4C12-ABF5-03CE21EC1679', '1994-05-06 00:00:00', NULL, 35124, NULL, 1, NULL)
, (165524, 1, '65524', 184146, 'A603E966-6F85-4FC1-BA06-40E391C7218E', '1995-03-22 00:00:00', NULL, 35111, NULL, 1, NULL);
【讨论】:
@user2338369:这对你有帮助吗?【参考方案2】:你可以使用Data Loader来做到这一点
【讨论】:
此工具的试用版在您导出数据时有一些限制。以上是关于如何将远程服务器MYSQL数据库导出导入的主要内容,如果未能解决你的问题,请参考以下文章
SqlServer 如何通过命令窗口向远程链接服务器数据库表导入数据,或者用其他方法导入数据