#1273 - Unknown collation: 'utf8mb4_unicode_ci' cPanel
Asked Answered
F

14

198

I have a WordPress database on my local machine that I want to transfer to a hosted phpMyAdmin on cPanel. However, when I try to import the database into the environment, I keep getting this error:

#1273 - Unknown collation: 'utf8mb4_unicode_ci' 

I have tried to Google around and the only solution I can find is this one phpmysql error - #1273 - #1273 - Unknown collation: 'utf8mb4_general_ci' which as by now isn't much help. I have tried clearing the cookies but it still won't work. Please help!

Fullscale answered 28/4, 2015 at 10:16 Comment(1)
What version of WordPress? See make.wordpress.org/core/2015/04/02/the-utf8mb4-upgradeSturrock
I
109

I had the same issue as all of our servers run older versions of MySQL. This can be solved by running a PHP script. Save this code to a file and run it entering the database name, user and password and it'll change the collation from utf8mb4/utf8mb4_unicode_ci to utf8/utf8_general_ci

<!DOCTYPE html>
<html>
<head>
  <title>DB-Convert</title>
  <style>
    body { font-family:"Courier New", Courier, monospace; }
  </style>
</head>
<body>

<h1>Convert your Database to utf8_general_ci!</h1>

<form action="db-convert.php" method="post">
  dbname: <input type="text" name="dbname"><br>
  dbuser: <input type="text" name="dbuser"><br>
  dbpass: <input type="text" name="dbpassword"><br>
  <input type="submit">
</form>

</body>
</html>
<?php
if ($_POST) {
  $dbname = $_POST['dbname'];
  $dbuser = $_POST['dbuser'];
  $dbpassword = $_POST['dbpassword'];

  $con = mysql_connect('localhost',$dbuser,$dbpassword);
  if(!$con) { echo "Cannot connect to the database ";die();}
  mysql_select_db($dbname);
  $result=mysql_query('show tables');
  while($tables = mysql_fetch_array($result)) {
          foreach ($tables as $key => $value) {
           mysql_query("ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
     }}
  echo "<script>alert('The collation of your database has been successfully changed!');</script>";
}

?>
Inadmissible answered 29/4, 2015 at 9:41 Comment(7)
This seems like overkill vs mysqldump --compatible=mysql4Cattan
For me it worked. Remember to name the file db-convert.phpTrisoctahedron
If you make 'localhost' an input it's perfect.Arieariel
mysqldump --compatible=mysql4 or the answer below is a better option. I don't think it's a wise idea to change something like this on a live site. Better to export in the correct format, or if that's not an option, edit the exported file.Negus
For me the command mysqldump --compatible=no_table_options,mysql4 did the trick instead.Diaghilev
+1 I was able to use this answer's SQL query outside of the script context to "fix" each table and it worked for my problem. Then my table is the correct collation and I can import without having to remember to add flagsPoulin
Check answer below so you can get a PHP7 version of this (new mysql library)Radom
X
231

The technique in this post worked for me

1) Click the "Export" tab for the database

2) Click the "Custom" radio button

3) Go the section titled "Format-specific options" and change the dropdown for "Database system or older MySQL server to maximize output compatibility with:" from NONE to MYSQL40.

4) Scroll to the bottom and click "GO".

I'm not certain if doing this causes any data loss, however in the one time I've tried it I did not notice any. Neither did anyone who responded in the forums linked to above.

Edit 8/12/16 - I believe exporting a database in this way causes me to lose data saved in Black Studio TinyMCE Visual Editor widgets, though I haven't ran multiple tests to confirm.

Xymenes answered 30/4, 2015 at 18:8 Comment(5)
didn't work for me, i got the error #1231 - Variable 'character_set_client' can't be set to the value of 'NULL'Ethics
After multiple failed attempts at other solutions, this one worked perfectly on the first attempt.Ghats
Does NOT work with some new Wordpress sites. When importing it on the other server, it generates this error on the very first table it tries to import (wp_commentmeta) #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TYPE=MyISAM AUTO_INCREMENT=1' at line 19Crabstick
this didn't seem to make any difference for me, still getting the same error messagesTernary
you are the bossNormalize
C
187

If you have already exported a .sql file, the best thing to do is to Find and Replace the following if you have them in your file:

  • utf8mb4_0900_ai_ci to utf8_unicode_ci
  • utf8mb4 to utf8
  • utf8_unicode_520_ci to utf8_unicode_ci

It will replace utf8mb4_unicode_ci to utf8_unicode_ci. Now you go to your phpMyAdmin cPanel and set the DB collation to utf8_unicode_ci through Operations > Collation.

If you are exporting to a .sql, it's better to change the format on how you're exporting the file. Check out Evster's anwer (it's in the same page as this)

Centrum answered 19/7, 2015 at 23:1 Comment(5)
it's working perfectly along with this one https://mcmap.net/q/127594/-1273-unknown-collation-39-utf8mb4_unicode_ci-39-cpanelDix
For all you unix folks: sed -i.bak s/utf8mb4/utf8/g FILE_NAME. This will find all occurrences of utf8mb4 in FILE_NAME and replace it with utf8 while saving a copy of the original file to FILE_NAME.bak. You may need to tweak it to specify the exact coalition in your case but this is a start :)Monolingual
I also had to replace: utf8_unicode_520_ci with: utf8_unicode_ciRestate
Or via vi: vi dump.sql and then in via do this: :%s/uf8mb4/utf8/g.Hagerty
and replace utf8_0900_ai_ci with utf8_unicode_ciHachman
I
109

I had the same issue as all of our servers run older versions of MySQL. This can be solved by running a PHP script. Save this code to a file and run it entering the database name, user and password and it'll change the collation from utf8mb4/utf8mb4_unicode_ci to utf8/utf8_general_ci

<!DOCTYPE html>
<html>
<head>
  <title>DB-Convert</title>
  <style>
    body { font-family:"Courier New", Courier, monospace; }
  </style>
</head>
<body>

<h1>Convert your Database to utf8_general_ci!</h1>

<form action="db-convert.php" method="post">
  dbname: <input type="text" name="dbname"><br>
  dbuser: <input type="text" name="dbuser"><br>
  dbpass: <input type="text" name="dbpassword"><br>
  <input type="submit">
</form>

</body>
</html>
<?php
if ($_POST) {
  $dbname = $_POST['dbname'];
  $dbuser = $_POST['dbuser'];
  $dbpassword = $_POST['dbpassword'];

  $con = mysql_connect('localhost',$dbuser,$dbpassword);
  if(!$con) { echo "Cannot connect to the database ";die();}
  mysql_select_db($dbname);
  $result=mysql_query('show tables');
  while($tables = mysql_fetch_array($result)) {
          foreach ($tables as $key => $value) {
           mysql_query("ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
     }}
  echo "<script>alert('The collation of your database has been successfully changed!');</script>";
}

?>
Inadmissible answered 29/4, 2015 at 9:41 Comment(7)
This seems like overkill vs mysqldump --compatible=mysql4Cattan
For me it worked. Remember to name the file db-convert.phpTrisoctahedron
If you make 'localhost' an input it's perfect.Arieariel
mysqldump --compatible=mysql4 or the answer below is a better option. I don't think it's a wise idea to change something like this on a live site. Better to export in the correct format, or if that's not an option, edit the exported file.Negus
For me the command mysqldump --compatible=no_table_options,mysql4 did the trick instead.Diaghilev
+1 I was able to use this answer's SQL query outside of the script context to "fix" each table and it worked for my problem. Then my table is the correct collation and I can import without having to remember to add flagsPoulin
Check answer below so you can get a PHP7 version of this (new mysql library)Radom
S
54

i use this in linux :

sed -i 's/utf8mb4/utf8/g' your_file.sql
sed -i 's/utf8_unicode_ci/utf8_general_ci/g' your_file.sql
sed -i 's/utf8_unicode_520_ci/utf8_general_ci/g' your_file.sql
sed -i 's/utf8_0900_ai_ci/utf8_general_ci/g' your_file.sql

then restore your_file.sql

mysql -u yourdBUser -p yourdBPasswd yourdB < your_file.sql
Slavin answered 7/3, 2017 at 13:17 Comment(4)
There's a comment on another answer, which bears repeating here. The OS X version of sed requires an extra argument after the -i flag. So sed -i '' .... works.Beore
I had to run this as well: sed -i 's/utf8_0900_ai_ci/utf8_general_ci/g' your_file.sqlFilament
The -i flag just mean replace in-place, so not to generate a new fileOlin
another OSX gotcha: prepending sed with encoding info like LC_ALL=C sed … fixes the byte sequence error, see #29917110Incarcerate
I
30

Wordpress 4.2 introduced support for "utf8mb4" character encoding for security reasons, but only MySQL 5.5.3 and greater support it. The way the installer (and updater) handles this is that it checks your MySQL version and your database will be upgraded to utfmb4 only if it's supported.

This sounds great in theory but the problem (as you've discovered) is when you are migrating databases from a MySQL server that supports utf8mb4 to one that doesn't. While the other way around should work, it's basically a one-way operation.

As pointed out by Evster you might have success using PHPMYAdmin's "Export" feature. Use "Export Method: Custom" and for the "Database system or older MySQL server to maximize output compatibility with:" dropdown select "MYSQL 40".

For a command line export using mysqldump. Have a look at the flag:

$ mysqldump --compatible=mysql4

Note: If there are any 4-byte characters in the database they will be corrupted.

Lastly, for anyone using the popular WP Migrate DB PRO plugin, a user in this Wordpress.org thread reports that the migration is always handled properly but I wasn't able to find anything official.

The WP Migrate DB plugin translates the database from one collation to the other when it moves 4.2 sites between hosts with pre- or post-5.5.3 MySQL

At this time, there doesn't appear to be a way to opt out of the database update. So if you are using a workflow where you are migrating a site from a server or localhost with MySQL > 5.5.3 to one that uses an older MySQL version you might be out of luck.

Itemized answered 14/5, 2015 at 23:5 Comment(2)
Changing compatibility to "MYSQL 40" totally worked for me.Numerable
If you then try and import the mysql4 compatible dump into a post v5.5.3 database (I'm using 5.5.28) then it fails because the script includes TYPE=MyISAM that was removed in v5.1. Do a search and replace with ENGINE=MyISAM. I couldn't see a way around this using the mysqldump output options.Cattan
A
27

In my case it turns out my
new server was running MySQL 5.5,
old server was running MySQL 5.6.
So I got this error when trying to import the .sql file I'd exported from my old server.

MySQL 5.5 does not support utf8mb4_unicode_520_ci, but
MySQL 5.6 does.

Updating to MySQL 5.6 on the new server solved collation the error !

If you want to retain MySQL 5.5, you can:
- make a copy of your exported .sql file
- replace instances of utf8mb4unicode520_ci and utf8mb4_unicode_520_ci
...with utf8mb4_unicode_ci
- import your updated .sql file.

Assertive answered 7/1, 2017 at 13:7 Comment(1)
Yes - uploading to 5.6 is the simplest solution for this (and Ubuntu has a mysql-server-5.6 package you can install that will remove 5.5 for you automatically).Sager
G
16

There is a line in wp-config.php:

define('DB_CHARSET', 'utf8mb4');

If you follow Markouver's / Evster's instructions, don't forget to change this line on production server to

define('DB_CHARSET', 'utf8');

in order to fix broken 4-byte characters

Gromyko answered 7/6, 2015 at 14:4 Comment(1)
Also don't forget to modify define('DB_COLLATE', 'utf8_general_ci'); as well. This helped for me.Broadcloth
H
11

After the long time research i have found the solution for above:

  1. Firstly you change the wp-config.php> Database DB_CHARSET default to "utf8"

  2. Click the "Export" tab for the database

  3. Click the "Custom" radio button

  4. Go the section titled "Format-specific options" and change the dropdown for "Database system or older MySQL server to maximize output compatibility with:" from NONE to MYSQL40.

  5. Scroll to the bottom and click go

Then you are on.

Hartzel answered 19/8, 2015 at 11:12 Comment(0)
B
7

Seems like your host does not provide a MySQL-version which is capable to run tables with utf8mb4 collation.

The WordPress tables were changed to utf8mb4 with Version 4.2 (released on April, 23rd 2015) to support Emojis, but you need MySQL 5.5.3 to use it. 5.5.3. is from March 2010, so it should normally be widely available. Cna you check if your hoster provides that version?

If not, and an upgrade is not possible, you might have to look out for another hoster to run the latest WordPress versions (and you should always do that for security reasons).

Broadleaf answered 28/4, 2015 at 10:26 Comment(1)
You can check your version of MySQL via command line with "mysql -V"Elodia
A
2

So I solved in this way, from MySQL 5.6 to MySQL 5.5:

$ mysqldump -u username -p --compatible=mysql4 database_name > database_name.sql
$ sed -i 's/TYPE=InnoDB/ENGINE=InnoDB/g' database_name.sql

(Optional) Create a .sql.gz file:

$ gzip database_name.sql 

Explanation

$ mysqldump -u username -p --compatible=mysql4 database_name > database_name.sql

As explained in this answer, this is just the equivalent of this options from phpMyAdmin: "Database system or older MySQL server to maximize output compatibility with:" dropdown select "MYSQL 40".

$ sed -i 's/TYPE=InnoDB/ENGINE=InnoDB/g' database_name.sql

We needs this, to solve this issue:

ERROR 1064 (42000) at line 18: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TYPE=InnoDB' at line 9

Adventitious answered 23/12, 2016 at 11:29 Comment(3)
Note if you're using OS X's version of sed: $ sed -i'' 's/TYPE=InnoDB/ENGINE=InnoDB/g' database_name.sql . If you want to keep a copy as database_name.sql.bak before the sed replace: $ sed -i'bak' 's/TYPE=InnoDB/ENGINE=InnoDB/g' database_name.sqlVillarreal
you can also inline sed.Denaturalize
Also if you're running mysqldump on MySQL 8 you need to use --compatible=ansi optionSingsong
I
1

I also experienced this issue. Solution which worked for me was opening local database with Sequel Pro and update Encoding and Collation to utf8/utf8_bin for each table before importing.

Infract answered 28/4, 2015 at 10:47 Comment(0)
A
1

The easiest way to do is export your database to .sql, open it on Notepad++ and "Search and Replace" the utf8mb4_unicode_ci to utf8_unicode_ci and also replace utf8mb4 to utf8. Also don't forget to change the database collation to utf8_unicode_ci (Operations > Collation).

Abbasid answered 23/12, 2016 at 9:31 Comment(0)
W
0

open the sql file on Notepad++ and ctrl + H. Then you put "utf8mb4" on search and "utf8" on replace. The issue will be fixed then.

Waylonwayman answered 26/4, 2018 at 3:29 Comment(1)
It says "unknown collation utf8"Acquire
R
0

If you are using PHP7, then you would need a PHP script like this one in order to migrate your collation:

<!DOCTYPE html>
<html>
<head>
  <title>DB-Convert</title>
  <style>
    body { font-family:"Courier New", Courier, monospace; }
  </style>
</head>
<body>

<h1>Convert your Database to utf8_general_ci!</h1>

<form action="db-convert.php" method="post">
  dbname: <input type="text" name="dbname"><br>
  dbuser: <input type="text" name="dbuser"><br>
  dbpass: <input type="text" name="dbpassword"><br>
  <input type="submit">
</form>

</body>
</html>
<?php
if ($_POST) {
  $dbname = $_POST['dbname'];
  $dbuser = $_POST['dbuser'];
  $dbpassword = $_POST['dbpassword'];

  $mysqli = new mysqli('db',$dbuser,$dbpassword);
  if ($mysqli -> connect_errno) {
    echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
    exit();
  }
  $mysqli -> select_db($dbname);
  $result= $mysqli->query('show tables');
  while($tables = $result->fetch_array) {
          foreach ($tables as $key => $value) {
           $mysqli->query("ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
     }}
  echo "<script>alert('The collation of your database has been successfully changed!');</script>";
}

?>
Radom answered 4/2, 2021 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.