将电脑上的sqlserver数据库迁移到另一台电脑

*近在做一个课程设计,我一个同学负责数据库创建。他在他电脑上建完我要拿过来使用,可是怎么导入到我的电脑上呢,我再网上查了好多,找到了一个简单粗暴的方法。
操作如下:
1.右键你需要的数据库–>任务–>分离

%title插图%num

%title插图%num

2.找到你的sqlserver安装文件夹

%title插图%num

这里把你需要的数据库(如jjj)的两个相关文件拷贝到另一台电脑上,这里注意,你*好还是要放到sqlserver的这个文件夹,放别的地方可能会报错,因为文件夹的读写属性还有用户权限不一样,反正修改起来特别麻烦。

3.到另一台电脑上,右键–>附加–>选择你的mdf文件,确定,大功告成,数据库成功导入,简单吧!

%title插图%num

我试了直接把文件复制过去,导入也成功了,所以不是很明白为什么要分离,我在网上查了一下:
数据库分离是指将数据库文件从数据库服务器实例中分离出来,相当于关闭了数据库。数据库分离后,应用程序不能连接到该数据库,数据库文件可以被其它进程访问。通常分离数据库用于快速地将数据库迁移到另一个SQLSERVER实例。
其实我还是不明白,嘻嘻。

SQL Server 数据库的迁移

不清楚各位大大是怎么做数据迁移的,反正俺大老粗的做法是,先把SQL Server的数据从企业管理中备份出来然后再恢复。

在恢复的过程中经常出现的一个问题就是,数据库的原来的用户跟新的数据库系统上的登录对不上号了,以前我曾经试过多次,但是经常都忘记,总是找一个*快速的方法来恢复数据库的用户,作此记录以慰昨日之辛苦:

SQL Server 数据库恢复用户之葵花点穴手, 以下部分copy from SQL Server Books Online.

sp_change_users_login
Changes the relationship between a Microsoft® SQL Server™ login and a SQL Server user in the current database.

Syntax
sp_change_users_login [ @Action = ] ‘action’
[ , [ @UserNamePattern = ] ‘user’ ]
[ , [ @LoginName = ] ‘login’ ]

Arguments
[@Action =] ‘action’

Describes the action to be performed by the procedure. action is varchar(10), and can be one of these values.

Value Description
Auto_Fix Links user entries in the sysusers table in the current database to logins of the same name in syslogins. It is recommended that the result from the Auto_Fix statement be checked to confirm that the links made are the intended outcome. Avoid using Auto_Fix in security-sensitive situations. Auto_Fix makes best estimates on links, possibly allowing a user more access permissions than intended.
user must be a valid user in the current database, and login must be NULL, a zero-length string (”), or not specified.

Report Lists the users, and their corresponding security identifiers (SID), that are in the current database, not linked to any login.
user and login must be NULL, a zero-length string (”), or not specified.

Update_One Links the specified user in the current database to login. login must already exist. user and login must be specified.

[@UserNamePattern =] ‘user’

Is the name of a SQL Server user in the current database. user is sysname, with a default of NULL. sp_change_users_login can be used only with the security accounts of SQL Server logins and users; it cannot be used with Microsoft Windows NT® users.

[@LoginName =] ‘login’

Is the name of a SQL Server login. login is sysname, with a default of NULL.

Return Code Values
0 (success) or 1 (failure)

Result Sets
Column name Data type Description
UserName sysname Login name.
UserSID varbinary(85) Login security identifier.

Remarks
Use this procedure to link the security account for a user in the current database with a different login. If the login for a user has changed, use sp_change_users_login to link the user to the new login without losing the user’s permissions.

login cannot be sa, and user cannot be the dbo, guest, or INFORMATION_SCHEMA users.

sp_change_users_login cannot be executed within a user-defined transaction.

Permissions
Any member of the public role can execute sp_change_users_login with the Report option. Only members of the sysadmin fixed server role can specify the Auto_Fix option. Only members of the sysadmin or db_owner roles can specify the Update_One option.

Examples
A. Show a report of the current user to login mappings
This example produces a report of the users in the current database and their security identifiers.

EXEC sp_change_users_login ‘Report’
B. Change the login for a user
This example changes the link between user Mary in the pubs database and the existing login, to the new login NewMary (added with sp_addlogin).

–Add the new login.
USE master
go
EXEC sp_addlogin ‘NewMary’
go

–Change the user account to link with the ‘NewMary’ login.
USE pubs
go
EXEC sp_change_users_login ‘Update_One’, ‘Mary’, ‘NewMary’
See Also
sp_addlogin

sp_adduser

sp_helplogins

MYsql:数据库内容完整复制到另一数据库

数据库内容完整复制到另一数据库
1.        双击打开数据库1

2.        右击数据库1,在弹出的属性菜单内选择:转储SQL文件…

3.        在弹出页面内,选择保存到本地的位置,点击保存   会将数据库1内的所有信息保存到本地(类似备份功能)

4.        双击打开另一数据库2

5.        右击数据库2,在弹出的属性菜单内选择:运行SQL文件…

6.        打开运行sql文件页面

7.        在【文件】选择之前转储的 数据库1 文件

8.        点击【开始】可以将数据库1 完全的运行复制到数据库2

mysql数据库从一台服务器迁移到另一台服务器上

一、应用场景
由于要把测试服务器上的数据库迁移到正式服务器上,因此需要做数据库的迁移。这里记录一下。

二、数据库迁移
1、新建数据库test

create database test;
1
2、进入数据库,创建数据表test

create table test1(id int(10) primary key, name varchar(30), age int(10));
1
3、生成.sql文件

(1)这里生成.sql文件,需要用到mysqldump,如果找到该命令的话,建立一个软连接到/usr/bin 下。例如,你的mysql 路径为:/usr/local/mysql,则执行
ln -fs /usr/local/mysql /usr/bin (PS:这里不需要进入到mysql执行命令)

(2)执行mysqldump命令

1)为某个数据库生成.sql文件

mysqldump -u 用户名 -p 数据库>你想要生成.sql文件的路径

2)只要数据库的表结构

mysqldump -u 用户名 -p -d 数据库>你想要生成.sql文件的路径

3)生成某个数据库下面对应某个表的表结构

mysqldump -u 用户名 -p -d 数据库名 表名>你想要生成.sql文件的路径

4)生成某个数据库下面对应某个表的表结构+表数据

mysqldump -u 用户名 -p -d 数据库名>你想要生成.sql文件的路径

我这里执行的命令是:

mysqldump -u xx -p pzsh>/home/pzsh/test.sql
1
然后去/home/pzsh下面看看,就会发现已经生成.sql文件了

4、使用ftp工具,把生成的.sql文件放到新服务器上去

(1)使用FTP工具下载.sql文件
(2)使用FTP工具把.sql文件上传到指定路径
(3)我这边是把.sql文件上传到新服务器的/home/pzsh目录下

5、导入到服务器

(1)进入到新服务器的mysql中

mysql -u 用户名 -p

(2)先创建数据库,按照.sql文件中的数据库名字。

create database test;

//进入到创建的数据库中
use test;
//查看当下的数据表
show tables;
//此时数据库中是没有任何表的

(3)执行source命令,导入.sql文件

source /home/pzsh/test.sql

(4)查看test数据库中的表

show tables;

此时发现我们已经把表都转移过来了。

6、如果两台服务器的数据库版本不兼容,则在root用户下执行下面的命令

mysql_upgrade -u root -p

输入密码即可。

end