How to import a SQL Server .bak file into MySQL? - mysql

The title is self explanatory. Is there a way of directly doing such kind of importing?

The .BAK files from SQL server are in Microsoft Tape Format (MTF) ref: http://www.fpns.net/willy/msbackup.htm
The bak file will probably contain the LDF and MDF files that SQL server uses to store the database.
You will need to use SQL server to extract these. SQL Server Express is free and will do the job.
So, install SQL Server Express edition, and open the SQL Server Powershell. There execute sqlcmd -S <COMPUTERNAME>\SQLExpress (whilst logged in as administrator)
then issue the following command.
restore filelistonly from disk='c:\temp\mydbName-2009-09-29-v10.bak';
GO
This will list the contents of the backup - what you need is the first fields that tell you the logical names - one will be the actual database and the other the log file.
RESTORE DATABASE mydbName FROM disk='c:\temp\mydbName-2009-09-29-v10.bak'
WITH
MOVE 'mydbName' TO 'c:\temp\mydbName_data.mdf',
MOVE 'mydbName_log' TO 'c:\temp\mydbName_data.ldf';
GO
At this point you have extracted the database - then install Microsoft's "Sql Web Data Administrator". together with this export tool and you will have an SQL script that contains the database.

MySql have an application to import db from microsoft sql.
Steps:
Open MySql Workbench
Click on "Database Migration" (if it do not appear you have to install it from MySql update)
Follow the Migration Task List using the simple Wizard.

I did not manage to find a way to do it directly.
Instead I imported the bak file into SQL Server 2008 Express, and then used MySQL Migration Toolkit.
Worked like a charm!

In this problem, the answer is not updated in a timely. So it's happy to say that in 2020 Migrating to MsSQL into MySQL is that much easy. An online converter like RebaseData will do your job with one click. You can just upload your .bak file which is from MsSQL and convert it into .sql format which is readable to MySQL.
Additional note: This can not only convert your .bak files but also this site is for all types of Database migrations that you want.

Although my MySQL background is limited, I don't think you have much luck doing that. However, you should be able to migrate over all of your data by restoring the db to a MSSQL server, then creating a SSIS or DTS package to send your tables and data to the MySQL server.
hope this helps

I highly doubt it. You might want to use DTS/SSIS to do this as Levi says. One think that you might want to do is start the process without actually importing the data. Just do enough to get the basic table structures together. Then you are going to want to change around the resulting table structure, because whatever structure tat will likely be created will be shaky at best.
You might also have to take this a step further and create a staging area that takes in all the data first n a string (varchar) form. Then you can create a script that does validation and conversion to get it into the "real" database, because the two databases don't always work well together, especially when dealing with dates.

The method I used included part of Richard Harrison's method:
So, install SQL Server 2008 Express
edition,
This requires the download of the Web Platform Installer "wpilauncher_n.exe"
Once you have this installed click on the database selection ( you are also required to download Frameworks and Runtimes)
After instalation go to the windows command prompt and:
use sqlcmd -S \SQLExpress (whilst
logged in as administrator)
then issue the following command.
restore filelistonly from
disk='c:\temp\mydbName-2009-09-29-v10.bak';
GO This will list the contents of the
backup - what you need is the first
fields that tell you the logical names
- one will be the actual database and the other the log file.
RESTORE DATABASE mydbName FROM
disk='c:\temp\mydbName-2009-09-29-v10.bak' WITH MOVE 'mydbName' TO
'c:\temp\mydbName_data.mdf', MOVE
'mydbName_log' TO
'c:\temp\mydbName_data.ldf'; GO
I fired up Web Platform Installer and from the what's new tab I installed SQL Server Management Studio and browsed the db to make sure the data was there...
At that point i tried the tool included with MSSQL "SQL Import and Export Wizard" but the result of the csv dump only included the column names...
So instead I just exported results of queries like "select * from users" from the SQL Server Management Studio

SQL Server databases are very Microsoft proprietary. Two options I can think of are:
Dump the database in CSV, XML or similar format that you'd then load into MySQL.
Setup ODBC connection to MySQL and then using DTS transport the data. As Charles Graham has suggested, you may need to build the tables before doing this. But that's as easy as a cut and paste from SQL Enterprise Manager windows to the corresponding MySQL window.

For those attempting Richard's solution above, here are some additional information that might help navigate common errors:
1) When running restore filelistonly you may get Operating system error 5(Access is denied). If that's the case, open SQL Server Configuration Manager and change the login for SQLEXPRESS to a user that has local write privileges.
2) #"This will list the contents of the backup - what you need is the first fields that tell you the logical names" - if your file lists more than two headers you will need to also account for what to do with those files in the RESTORE DATABASE command. If you don't indicate what to do with files beyond the database and the log, the system will apparently try to use the attributes listed in the .bak file. Restoring a file from someone else's environment will produce a 'The path has invalid attributes. It needs to be a directory' (as the path in question doesn't exist on your machine).
Simply providing a MOVE statement resolves this problem.
In my case there was a third FTData type file. The MOVE command I added:
MOVE 'mydbName_log' TO 'c:\temp\mydbName_data.ldf',
MOVE 'sysft_...' TO 'c:\temp\other';
in my case I actually had to make a new directory for the third file. Initially I tried to send it to the same folder as the .mdf file but that produced a 'failed to initialize correctly' error on the third FTData file when I executed the restore.

The .bak file from SQL Server is specific to that database dialect, and not compatible with MySQL.
Try using etlalchemy to migrate your SQL Server database into MySQL. It is an open-sourced tool that I created to facilitate easy migrations between different RDBMS's.
Quick installation and examples are provided here on the github page, and a more detailed explanation of the project's origins can be found here.

Related

How do you create a folder using SQL

I have a SQL script which selects data from DB and stores it to files. I am unable to create a directory to store these files.
I have shell script that loads the SQL file. Shell and the SQL are on separate server than MySQL db. I would prefer to create this directory using SQL as I want to avoid ssh.
Any suggestions? Surprisingly I couldn't find anything on Google.
I will assume that you're using mysql, according to your tags. You could do it with a Microsoft SQL Server or Oracle database but unfortunately, at the moment, there is no solution to create a directory from MySQL.
Some will guide you with a workaround based on the creation of a data directory, I wouldn't recommand this, as it could lead to performances issues in the future, or worst.
The best solution would be to use a script (java, vbscript, SSH, batch, ...). Again, you won't be able to start this script within your SQL query easily. I know that's no good news, but it is important not to lead you on the wrong direction.
I would suggest to reverse your thinking, and start your SQL query from a script (again, any language you're used to).
I couldn't find any other way other than opening ssh session to the target box.
Open ssh session
Create directory
close ssh session
Load sql file using shell
The sql adds the generated files to the directory created in step 2.
ssh -t $USER#$HOST <<-SSH-END;
mkdir -p "dir/path";
exit;
SSH-END
Sharing just in case someone else needs to do the same.

how to extract mysql database as .mdf

when I go to the database I created in mysql I just see a db.opt, .frm and .ibd file types. Is there a way to create a .mdf database file so I can further create my project in visual studio?
To use a MySQL database in Visual Studio, you need to use Connector / Net and MySQL For Visual Studio. This code works well and will allow you to use MySQL with the various features of VS.
Or you need to transport the data from MySQL to SQL Server. There are tools to do that if you must.
See here. http://dev.mysql.com/tech-resources/articles/mysql-installer-for-windows.html
It is true that some MySQL databases have on-disk structures that use .frm and .ibd files. But those files are intended to be accessed pretty much exclusively by the MySQL server process.
It's also true that the mdf file is an ondisk structure for SQL Server. These files aren't transportable like .xls files or other "document" files.
1) You do NOT want to "extract a MySQL database as an .mdf file". Instead, you want to export a MySQL database to a SQL server database.
2) SQL Server uses any of the following files - at a minimum, you need a "primary database file" and a "log file". But you absolutely need MSSQL to manage these files for you:
.mdf - Primary database data file.
.ndf - Other database data files i.e. non Primary.
.ldf - Log data file.
3) So how do you export? If you're familiar with MSSQL SSMS (the MSSQL GUI), you might want to consider using the Export/Import Wizard. Here is a good "howto" on using MSSQL "import" tools:
http://www.mssqltips.com/sqlservertutorial/2205/mysql-to-sql-server-data-migration/
4) Alternatively, you can also use phpAdmin to "export" into MSSQL:
http://www.waynezim.com/2010/03/how-to-export-mysql-database-to-mssql-using-phpmyadmin/
5) Finally, if all you want to do is access mySQL data from an MSVS C# or ASP.Net program ... there's really no need to export/import at all. .Net allows you to access mySQL data directly:
http://dev.mysql.com/doc/connector-net/en/connector-net-visual-studio.html
Good luck - and let us know what you find works best for you!

Can we restore database backup of SQL Server 2008 R2 Express to SQL Server 2008 Express

I have a database backup in "SQL Server 2008 R2", Can i store this database backup in "SQL 2008 Express" ?
Anyone who have knowledge about SQL-2008 kindly share to me.
No and 'yes'.
A backup (.bak) is not possible, nor is copying .mdf over but you can downgrade.
You could go back to 2005 if you wish to.
I found this information which runs you through it the easy way.
How to Downgrade SQL Server Database
Direct link: http://www.mytechmantra.com/LearnSQLServer/Downgrade_SQL_Server_Database_P1.html
TIP: Use Microsoft OLEDB connector to destination instead of the native client and you should avoid any errors in the transfer
Nothing is impossible, you just have to get creative :)
Install an instance of SQL 2008 R2 on one machine and SQL 2008 on another
Open SQL Server Management Studio R2
Select your source Database
Select Tasks>Generate Scripts.
Select 'Script entire database and all database objects', press 'Next'
Select 'Save to File' and click on the 'Advanced' button
Select 'Script for Server Version' and select the version you want: 200/2005/2008
Select 'Type of data to Script' and select Schema/Data/both
Click 'OK',Next and do it!
Copy the resulting file to the target machine with SQL 2008 [or whatever]
Log onto your SQL Management Studio and open the copied .sql file... be aware that there may be limitations on the file size.
There may also be issues with the order that the .SQL file inserts the data into the target database and if there are FK constraints in place, this could be an issue.... simply re-order the insert lists.
Once the .sql file is organized, parse it [just to sure], and then execute.
Let me clarify a little on these directions.
On the SQL Management Tools be logged into both the old and the new server if possible.This makes these steps even easier.
So, start with the source server, right click on the database and script it. There is one option called ‘Script Data’ that is off by default, we set it to true so the script will include inserts for all of the data. We choose to script the tables, views, indexes and keys (and triggers … not sure if you use any of those). I also did not mess with the users, since that is problematic. It is easier to create the user by hand once on the local SQL server and give them permissions to the dbs.
Then script the DB to a file.
Then select the local SQL server and add the database by hand, then open up the script file and execute it under the new database, all the tables, data, indexes, etc came over just fine. Setup the user permission and you are golden.
NO, you cannot do this.
There is no way, no method, no hack, no workaround, no trick to get a backup from a more recent version of SQL Server back into an older version.
It just doesn't work - it's not supported.
You'll need to synchronize your structure using some kind of SQL diff tool, and possibly your data by exporting and importing, e.g. via the "Data Export / Import" wizard or some custom way.

Downgrade mdf file from 661 to 655

I created and database in sql server 2008. I took the mdf and ldf files to Visual Studio 2010 project and tried to connect to that db with entity-framework. and got the following message:
I cannot reinstall or upgrade the software on the specific computer.
So is there any way to downgrade the version of the mdf and ldf files?
No, you cannot downgrade, you will need to move the schema and data another way if you can't install the right version. This recent question is essentially the same:
Failure attaching SQL Server 2008 database to SQL Server 2005
Essentially you can use the Generate Scripts tool or the Export Data wizard to get your destination database to look like the source. You will not be able attach your MDF file.
And this blog post shows some 3rd party tools that can help with this:
http://bertrandaaron.wordpress.com/2012/04/20/re-blog-the-cost-of-reinventing-the-wheel/
No, you need to install the same or newer version of SqlServer.
There is no known way to downgrade an MDF file.
Links
http://blog.sqlauthority.com/2008/10/16/sql-server-downgrade-database-to-previous-version/
Perhaps, if you have access to the SqlServer 2008, you could use the command
"Tasks" -> "Create Scripts" to start the wizard to create a script with schema and data.
On the options page there is the button 'Advanced' where you can select the syntax version of SqlServer
The way that I found that worked was to export the database and stored procedures from the original database. Then upload them into the second database(second computer).
Firstly export the DB content (data) - I used SQL server export data wizard. on the database you wish to export from right click then choose tasks, then export data. Follow the instructions and save in whichever format is best for you - I used excel for the data.
then to export the stored procedures rightclick the database name again. choose tasks and this time choose generate scripts. again follow the instructions of the wizard.
To import the data simply go to the second computer and right click the database you wish to import the data into. again tasks > import data. Follow the instructions to import all of the data from the database.
Finally to import the stored procedures, I opened up a new stored procedures command and dragged and dropped the script file that I had previously saved them in and dropped it into this window. The new stored procedure window filled with the entire list of my stored procedures.
Finally change the name of the database name that will be used by the SP ( if this is different from the original DB name). (This is the first line USE [DBName].
then simply execute and the SP's will be fully restored.
This has helped me get my entire database up and running again very quickly.
Hope this helps.
U can open it on another pic and then publish it to sql script with or without data then create database on target PC using sqlcmd or anything. Then run sql script...

Where to download AdventureWorks2008.msi?

I'd like to install AdventureWorks2008 (I just install SQL Server 2008 R2 Express).
Each time I download the recommended version from CodePlex, all I get is a AdventureWorks2008.mdf file. Not only I cannot attach the file from SQL Server Management Studio, but I cannot copy/paste the file directly into the the database.
I've read in several places that I need to use AdventureWorks2008.msi, but I cannot find where to download it.
I just cannot figure out how to install AdventureWorks2008
Thanks for helping
There isn't an .msi file for adventureworks, even though you'll find it mentioned in outdated documentation and books. You aren't alone in finding this confusing -- it seems the web site, files and steps Microsoft provides for installing these databases changes every time I need to install them.
You need to create the database and attach the .mdf file, which is the "data file" referred to in the instructions. (.mdf = primary data file, .ldf = log file, .ndf = secondary data file)
In order to attach the file, you need to make sure you carefully follow the steps here: http://social.technet.microsoft.com/wiki/contents/articles/3735.sql-server-samples-readme-en-us.aspx#Readme_for_Adventure_Works_Sample_Databases
Instructions for 2008R2:
To install AdventureWorks2008R2 OLTP database
Download the AdventureWorks2008R2 Data File.
From File Download, click Save and browse to a location on your local
server.
From SQL Server Management Studio, execute the following code:
Case-insensitive Database
CREATE DATABASE AdventureWorks2008R2
ON (FILENAME = '{drive}:\{file path}\AdventureWorks2008R2_Data.mdf')
FOR ATTACH_REBUILD_LOG;
As an alternative to step 3, you can attach the database using the SQL
Server Management Studio user interface. For more detailed
information, see Attach a Database (SQL Server Management Studio).
Note: You must remove the log file from the list of files to attach.
This will cause the operation to rebuild the log.
Headache saving tip from Aaron Bertrand:
You should place the mdf file in your normal data folder - SQL Server
will already have the proper permissions. You can get this path using
SELECT TOP (1) physical_name FROM master.sys.database_files;
You can directly paste that file into your database directory. For more information you can refer http://tryingmicrosoft.com/error-while-attaching-a-database-to-sql-server-2008-r2/.