Find a good way to move from SQLServer to Mysql database
- 2020-05-06 11:45:59
- OfStack
I've been working on a website since the beginning of the year, MSSQL2000, ->
The migration of MySQL5, because of the use of different programming systems, is the main problem in the migration of data. Due to two system database structure difference is very big, is not convenient to take SQL SERVER import MYSQL ODBC in the function of the data source (also not recommended to do so, the field type differences will make the ~ of the dead), so they write their own PHP under WINDOWS script to read data from the SQLSERVER, and then disposed of in accordance with the need of the new system after insert MYSQL, flexible and more convenient. The actual process mainly has the following problems:
1. Database connection, mainly connecting SQL SERVER. There are three main methods:
1.1 makes use of the mssql_ family of functions in PHP, which is similar to the mysql_ family of functions, but opens the php.ini extension (extension= php_mssql.dll).
1.2 makes use of ODBC connections, and because the concrete database is abstracted, there is no way to make use of the data table field name => Array key names feature, not very convenient for specific applications, code form:
$conn = odbc_connect("datasource","username","password");
$sql = "select * from news";
$cur= odbc_exec($conn, $sql);
while(odbc_fetch_row($cur)){
$field1 = odbc_result($cur,1);
$field2 = odbc_result($cur,2);
//do something
}
1.3 USES PDO - PHP5 to add the data object abstraction layer. As an official data access interface, has many advantages, such as supporting parameter binding to prevent SQL injection. For different database load different drivers can be, the program code is consistent, easy to transplant, etc., I believe that should be the trend. However, due to the use of PHP5's new object-oriented features, PHP5 support, 5.1 can be used directly, 5 needs to install PECL, in addition to modify PHP.ini, extension= php_pdo_mysql.dll and extension= php_pdo_mssql.dll, the actual code is as follows:
try {
$DBH=new PDO("mssql:dbname=XXX;host=localhost",
"root", "password");//Connect to DB
} catch (PDOException $e) {
print "Error!: " . $e- > getMessage();//Error Message
die();
}
$stmt = $DBH- > prepare("SELECT * FROM news");//Stmt Handle $stmt
if ($stmt- > execute()) {
while ($row = $stmt- > fetch()) {
//do something
}
}
$stmt2 = $mssql- > prepare("INSERT INTO news
(title,author) VALUES (:title, :author)");
$stmt2- > bindParam(':title', $title);
$stmt2- > bindParam(':author', $author);
$stmt2- > execute();
$DBH = null; // Close Connection a reminder that MSSQL does not have LIMIT in MYSQL.
2. The truncated TEXT field.
The above three methods of connecting to the database were tried, because the data of SELECT after connecting to MSSQL was always only 4K in length, which was thought to be caused by the limitation of the connection mode, so we changed several of them, and finally checked the data, we found that it was php ; Valid range 0 - 2147483647. Default = 4096.
mssql.textlimit = 4096
; Valid range 0 - 2147483647. Default = 4096.
mssql.textsize = 4096
Change 4096 to -1(for unlimited), or use mssql_query("SET TEXTSIZE 65536"); To implement.
3. There are two problems about the two database field types. One is that the field supports the maximum length, so as not to be truncated when inserting into the database. "select unix_timestamp(created) from news" can be used to convert DATETIME to MYSQL in MSSQL. However, when migrating, all the fields in the table are extracted at once. This method is not as simple as "select * from news", which requires listing all the fields. In fact, SELECT comes out directly, and what you get is a string. For example, in MSSQL it is 2006-01-01 12:01, and the string you get is "2006 January 01 12:01 ". Use the following function to convert to timestamp:
function ConvertTime($timestring){
if($timestring == null){
return 0;
}
$time = explode(" ",$timestring);
$year = $time[0];
switch ($time[1]){
case "January ":$month = "1"; break;
case "February ":$month = "2"; break;
"march ":$month = "3"; break;
case "April ":$month = "4"; break;
case "may ":$month = "5"; break;
case "June ":$month = "6"; break;
case "July ":$month = "7"; break;
case "August ":$month = "8"; break;
"September ":$month = "9"; break;
case "October ":$month = "10"; break;
case "November ":$month = "11"; break;
case "December ":$month = "12"; break;
default:break;
}
$day = $time[2];
$h = 0;
$m = 0;
$s = 0;
if(!empty($time[3])){
$time2 = explode(":",$time[3]);
$h = $time2[0];
$i = $time2[1];
}
//return date("Y-m-d H:i:s",mktime($h,$i,$s,$month,$day,$year));
return mktime($h,$i,$s,$month,$day,$year);
Finally, the conversion script can be executed in CMD window with php.exe abc.php.
That's basically it, and I hope it helps.
Update(06/05/05): for timestamp conversion, add
to php.ini mssql.datetimeconvert = Off
You will get a format similar to 2006-01-01 12:01 without Chinese.
1. Database connection, mainly connecting SQL SERVER. There are three main methods:
1.1 makes use of the mssql_ family of functions in PHP, which is similar to the mysql_ family of functions, but opens the php.ini extension (extension= php_mssql.dll).
1.2 makes use of ODBC connections, and because the concrete database is abstracted, there is no way to make use of the data table field name => Array key names feature, not very convenient for specific applications, code form:
$conn = odbc_connect("datasource","username","password");
$sql = "select * from news";
$cur= odbc_exec($conn, $sql);
while(odbc_fetch_row($cur)){
$field1 = odbc_result($cur,1);
$field2 = odbc_result($cur,2);
//do something
}
1.3 USES PDO - PHP5 to add the data object abstraction layer. As an official data access interface, has many advantages, such as supporting parameter binding to prevent SQL injection. For different database load different drivers can be, the program code is consistent, easy to transplant, etc., I believe that should be the trend. However, due to the use of PHP5's new object-oriented features, PHP5 support, 5.1 can be used directly, 5 needs to install PECL, in addition to modify PHP.ini, extension= php_pdo_mysql.dll and extension= php_pdo_mssql.dll, the actual code is as follows:
try {
$DBH=new PDO("mssql:dbname=XXX;host=localhost",
"root", "password");//Connect to DB
} catch (PDOException $e) {
print "Error!: " . $e- > getMessage();//Error Message
die();
}
$stmt = $DBH- > prepare("SELECT * FROM news");//Stmt Handle $stmt
if ($stmt- > execute()) {
while ($row = $stmt- > fetch()) {
//do something
}
}
$stmt2 = $mssql- > prepare("INSERT INTO news
(title,author) VALUES (:title, :author)");
$stmt2- > bindParam(':title', $title);
$stmt2- > bindParam(':author', $author);
$stmt2- > execute();
$DBH = null; // Close Connection a reminder that MSSQL does not have LIMIT in MYSQL.
2. The truncated TEXT field.
The above three methods of connecting to the database were tried, because the data of SELECT after connecting to MSSQL was always only 4K in length, which was thought to be caused by the limitation of the connection mode, so we changed several of them, and finally checked the data, we found that it was php ; Valid range 0 - 2147483647. Default = 4096.
mssql.textlimit = 4096
; Valid range 0 - 2147483647. Default = 4096.
mssql.textsize = 4096
Change 4096 to -1(for unlimited), or use mssql_query("SET TEXTSIZE 65536"); To implement.
3. There are two problems about the two database field types. One is that the field supports the maximum length, so as not to be truncated when inserting into the database. "select unix_timestamp(created) from news" can be used to convert DATETIME to MYSQL in MSSQL. However, when migrating, all the fields in the table are extracted at once. This method is not as simple as "select * from news", which requires listing all the fields. In fact, SELECT comes out directly, and what you get is a string. For example, in MSSQL it is 2006-01-01 12:01, and the string you get is "2006 January 01 12:01 ". Use the following function to convert to timestamp:
function ConvertTime($timestring){
if($timestring == null){
return 0;
}
$time = explode(" ",$timestring);
$year = $time[0];
switch ($time[1]){
case "January ":$month = "1"; break;
case "February ":$month = "2"; break;
"march ":$month = "3"; break;
case "April ":$month = "4"; break;
case "may ":$month = "5"; break;
case "June ":$month = "6"; break;
case "July ":$month = "7"; break;
case "August ":$month = "8"; break;
"September ":$month = "9"; break;
case "October ":$month = "10"; break;
case "November ":$month = "11"; break;
case "December ":$month = "12"; break;
default:break;
}
$day = $time[2];
$h = 0;
$m = 0;
$s = 0;
if(!empty($time[3])){
$time2 = explode(":",$time[3]);
$h = $time2[0];
$i = $time2[1];
}
//return date("Y-m-d H:i:s",mktime($h,$i,$s,$month,$day,$year));
return mktime($h,$i,$s,$month,$day,$year);
Finally, the conversion script can be executed in CMD window with php.exe abc.php.
That's basically it, and I hope it helps.
Update(06/05/05): for timestamp conversion, add
to php.ini mssql.datetimeconvert = Off
You will get a format similar to 2006-01-01 12:01 without Chinese.