Backup through SQL statements, restore database _mysql

Source: Internet
Author: User
eg
Copy Code code as follows:

/*
Backing up a database with SQL statements
*/
BACKUP DATABASE MyDB
to DISK = ' C:\DBBACK\mydb. BAK '
--Here specify the path and file name of the database you want to back up, and note that the path's folder must have been created. File names can be marked with dates
/*
Restoring a database from an SQL statement
*/
Use master
RESTORE DATABASE MyDB
From disk= ' C:\DBBACK\mydb. BAK '
With REPLACE

Note: Most of the time you cannot restore directly because the data is not open exclusively. You might use the following procedure
Copy Code code as follows:

--kill a connection that accesses a database
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

Single user action database is best used when killing users
Copy Code code as follows:

sp_dboption @DBName, ' Single user ', ' true '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.