数据库现在是latin1_general_ci,我想将排序规则更改为utf8mb4_general_ci。
在PhpMyAdmin中是否有任何设置来更改数据库,表,列的排序规则?而不是一个一个地改变?
数据库现在是latin1_general_ci,我想将排序规则更改为utf8mb4_general_ci。
在PhpMyAdmin中是否有任何设置来更改数据库,表,列的排序规则?而不是一个一个地改变?
当前回答
我很惊讶地发现,所以我不得不回到这里报告,优秀和维护良好的Interconnect/it SAFE SEARCH and REPLACE ON DATABASE脚本有一些选项可以将表转换为utf8 / unicode,甚至转换为innodb。它是一个脚本,通常用于将数据库驱动的网站(Wordpress, Drupal, Joomla等)从一个域迁移到另一个域。
https://github.com/interconnectit/Search-Replace-DB https://interconnectit.com/products/search-and-replace-for-wordpress-databases/
其他回答
您可以通过PHP脚本更改所有表的CHARSET和COLLATION,如下所示。我喜欢hkasera的答案,但它的问题是查询在每个表上运行两次。这段代码几乎是一样的,除了使用MySqli而不是mysql和防止双重查询。如果我可以投票的话,我会给hkasera的答案投票。
<?php
$conn1=new MySQLi("localhost","user","password","database");
if($conn1->connect_errno){
echo mysqli_connect_error();
exit;
}
$res=$conn1->query("show tables") or die($conn1->error);
while($tables=$res->fetch_array()){
$conn1->query("ALTER TABLE $tables[0] CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci") or die($conn1->error);
}
echo "The collation of your database has been successfully changed!";
$res->free();
$conn1->close();
?>
我很惊讶地发现,所以我不得不回到这里报告,优秀和维护良好的Interconnect/it SAFE SEARCH and REPLACE ON DATABASE脚本有一些选项可以将表转换为utf8 / unicode,甚至转换为innodb。它是一个脚本,通常用于将数据库驱动的网站(Wordpress, Drupal, Joomla等)从一个域迁移到另一个域。
https://github.com/interconnectit/Search-Replace-DB https://interconnectit.com/products/search-and-replace-for-wordpress-databases/
注意,在改变数据库的字符集/表/列,实际上你可能需要将现有的数据(例如,如果你看到类似“U…Ø·U”ˆØ¨ØªUˆØ±UŠØ¯Ø¬U”)是这样的:
update country set name = convert(cast(convert(name using latin1) as binary) using utf8), cn_flag = convert(cast(convert(cn_flag using latin1) as binary) using utf8), and so on..
对于转换数据库、表和字段,我建议从这个线程中得到这个答案,它会生成一组大的查询,你只需要在粘贴时复制,在这里我还找不到一个自动的解决方案。 还要注意的是,如果你将同一个字段转换两次,你将得到不可恢复的问号:"?? "。如果在转换字段/表之前转换数据,也会得到这个问号。
我的解决方案是@Dzintars和@Quassnoi Answer的组合。
SELECT CONCAT("ALTER TABLE ", TABLE_SCHEMA, '.', TABLE_NAME," CONVERT TO CHARACTER SET utf8mb4 ;") AS ExecuteTheString
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA="<your-database>"
AND TABLE_TYPE="BASE TABLE";
通过使用CONVERT TO,这将生成一个脚本,它将<your-database>的所有表转换为所需的编码。这也改变了每一列的编码!
修改数据库中所有表中所有字段的排序规则。
我只是通过前面提到的Php为表中的字段添加另一个循环到解决方案。这很有帮助,表中的所有字段也都转换了。
<?php
$con = mysql_connect('localhost','user','pw');
if(!$con) { echo "Cannot connect to the database ";die();}
mysql_select_db('database_name');
$result=mysql_query('show tables');
while($tables = mysql_fetch_array($result)) {
foreach ($tables as $key => $table) { // for each table
$sql = "ALTER TABLE $table CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci";
echo "\n".$sql;
mysql_query($sql);
$sql = "show fields in ".$table." where type like 'varchar%' or type like 'char%' or type='text' or type='mediumtext';";
$rs2=mysql_query($sql);
while( $rw2 = mysql_fetch_array($rs2) ){ // for each field in table
$sql = "ALTER TABLE `".$table."` CHANGE `".$rw2['Field']."` `".$rw2['Field']."` ".$rw2['Type']." CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;";
echo "\n".$sql;
mysql_query($sql);
}
}
}
echo "The collation of your database has been successfully changed!";
?>}