Home >Database >Mysql Tutorial >通过SQL语句来备份,还原数据库_MySQL

通过SQL语句来备份,还原数据库_MySQL

WBOY
WBOYOriginal
2016-06-01 13:19:561136browse

bitsCN.com eg:

/*
通过SQL 语句备份数据库
*/
BACKUP DATABASE mydb
TO DISK ='C:/DBBACK/mydb.BAK'
--这里指定需要备份数据库的路径和文件名,注意:路径的文件夹是必须已经创建的.文件名可以使用日期来标示
/*
通过SQL语句还原数据库
*/
USE master
RESTORE DATABASE mydb
FROM DISK='C:/DBBACK/mydb.BAK'
WITH REPLACE

注意:很多时候不能直接还原,因为数据不是独占打开.可能用到下面的过程

--Kill掉访问某个数据库的连接
CREATE PROC KillSpid(@DBName varchar)
AS
BEGIN
DECLARE @SQL varchar
DECLARE @SPID int
SET @SQL='DECLARE CurrentID CURSOR FOR
SELECT spid FROM sysprocesses WHERE dbid=db_id('''+@DBName+''') '
FETCH NEXT FROM CurrentID INTO @SPID
WHILE @@FETCH_STATUS -1
BEGIN
exec('KILL '+@SPID)
FETCH NEXT FROM CurrentID INTO @SPID
END
CLOSE CurrentID
DEALLOCATE CurrentID
END

当kill掉用户后最好使用单用户操作数据库

SP_DBOPTION @DBName,'single user','true'
bitsCN.com

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn