Django + MySQL - Unknown encoding: utf8mb4 - mysql

MySQL 5.5.35
Django 1.6.1
In order to support emoticons in the DB, I have configured in my django settings:
'OPTIONS': {'charset': 'utf8mb4'}
On MySQL connection, I get this error:
LookupError: unknown encoding: utf8mb4
How should I configure Django/MySQL in order to support utf8mb4?

https://code.djangoproject.com/ticket/18392#comment:10
As a workaround, you can make python understand 'utf8mb4' as an alias
for 'utf8':
import codecs
codecs.register(lambda name: codecs.lookup('utf8') if name == 'utf8mb4' else None)

If you really need utf8mb4,
follow the steps in https://mathiasbynens.be/notes/mysql-utf8mb4, and
make sure your python package "MySQL-python" version is >= 1.2.5 !

Fast and easy way.
connection = mysql.connector.connect(user='username',
password='mypass',
host='localhost',
database='mydb',
use_pure=True,
charset='utf8'
)

This worked in my case (MySQL 5.7 + Django 3.1):
Configure Django:
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'charset': 'utf8mb4'
}
Configure MySQL database
make sure all db tables are using InnoDB storage engine (this is important; the next step will probably fail if you skip it)
change the Collation for all your tables to utf8mb4_general_ci

Related

Django 2.0 - test using fixtures from dumpdata gives utf8mb4 encoding errors

I am using utf8mb4 encoding for my mySQL database.
All my Django apps work correctly with it even with Cyrillic characters.
But when I run ./manage.py test <module> and try to load a fixture dumped from the same database, I get errors regarding the encoding. One of the errors is:
Could not load advert.Region(pk=1): (1366, "Incorrect string value: '\\xD0\\x9F\\xD0\\xBB\\xD0\\xBE...' for column 'region_name' at row 1")
Then I set DEFAULT_CHARSET in the Django settings and the error changed to
LookupError: Problem installing fixture '<path>/test_db_one.xml': unknown encoding: utf8mb4
As you can see I have specified the format of the fixture to be XML as it has better handling of the encoding.
My setting module has the following properties:
DEFAULT_CHARSET = 'utf8mb4'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'autoplusplus_dev',
'USER': 'autoplusplus_django',
'PASSWORD': '1029',
'HOST': 'localhost',
'PORT': '3306',
'STORAGE_ENGINE': 'INNODB',
'OPTIONS': {'charset': 'utf8mb4'},
'TEST_CHARSET': 'utf8mb4',
}
}
I have mysqlclient 1.3.12 installed
Turned out that changing the encoding of the file which Django generates itself to UTF-8 fixes the issue.
You are expecting Пло? If so, that is reasonable UTF-8 encoding.
"unknown encoding: utf8mb4" -- You have an antique version of Django and/or MySQL? What versions are you running?
What does the table definition look like? Maybe the columns are latin1 instead of utf8 or utf8mb4.
Meanwhile, use MySQL's CHARACTER SET utf8; this may get you going.

1366, Incorrect string value django or collate or tables in mysql.

When in django admin panel i try to add something in database using russian symbols - it gives me error:
(1366, "Incorrect string value: '\\xD0\\xB2\\xD1\\x84\\xD1\\x8B' for column 'name' at row 1")
Request Method: POST
Request URL: https://giver.md/dev/admin/gift/categoryru/add/
Django Version: 1.10.4
Exception Type: OperationalError
Exception Value:
(1366, "Incorrect string value: '\\xD0\\xB2\\xD1\\x84\\xD1\\x8B' for column 'name' at row 1")
Exception Location: /home/ubuntu/giver/server/local/lib/python2.7/site-packages/MySQLdb/connections.py in defaulterrorhandler, line 36
Python Executable: /home/ubuntu/giver/server/bin/python2
Python Version: 2.7.12
I know, that i need to change collation to resolve this problem? But how can i collate al tables in my mysql database using ubuntu 16?
I tryed this, but this didn't helped me:
SELECT CONCAT("ALTER TABLE ", TABLE_NAME," COLLATE utf8_general_ci") AS ExecuteTheString
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA="giver"
As you mentioned, you have to change collation. The easiest way is to drop your old database and create a new utf8 one. Just note that with dropping your database, all of your tables and data will be gone, so you have to create tables and superuser again. So:
Create a new utf8 database:
CREATE DATABASE <new_database_name> CHARACTER SET utf8;
If the name of new database differ from the old one, don't forget to change database name in your app setting.py file!
Create tables; Just simply migrate:
$ python manage.py migrate
Create super user:
$ python manage.py createsuperuser
If you have some important data in your database and you don't want to lose them, see How to convert an entire MySQL database characterset and collation to UTF-8?
or simply use proper tools to backup your data from old database and import them to the new utf8 created database.

FreeTDS UTF-8 insert in mysql

I have a linux server with mysql with symfony framework installed.
Now i'm trying to get data from sql server 2012 and all works great except when i try to insert value in mysql database.
SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\x80 IN P...' for column 'note_anagrafica' at row 1
Seems that the problem is on data conversion.
The data type of column inside mysql where i'm trying to insert the data is:
text
And the collation is:
utf8_unicode_ci
FreeTds configuration is set on UTF-8 and driver is odbc.
Someone have a solution for this problem?
EDIT:
odbc.ini
[server]
Driver = FreeTDS
Server = x.x.x.x
Port = 1234
Database = databasename
TDS_Version = 8
client charseg = UTF-8
server charset = CP1252
freetds.conf
[server]
host = x.x.x.x
port = 1234
tds version = 8.0
client charset = UFT-8
text size = 20971520
use uft-16 = true
doctrine config for mysql.
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
doctrine config for external mssql database:
server:
driver_class: Lsw\DoctrinePdoDblib\Doctrine\DBAL\Driver\PDODblib\Driver
host: "%a_database_host%"
port: "%a_database_port%"
dbname: "%a_database_name%"
user: "%a_database_user%"
password: "%a_database_password%"
charset: utf8mb4
from now i'm getting error when i'm trying to insert data to mysql comes from sql server 2012 with doctrine.
Till now i have no problem on reading execpt that if i out the result on the web the special charaters like ø comes with � symbol.
There is a conversion that i have to made before call
$entityManager->persist($object);
$entityManager->flush();
Set the charset to utf8mb4 for doctrine config and will do the trick.

Doctrine2 with Symfony2 not recognizing db charset and collation

I am trying to get Doctrine2 to work properly with a mysql db in Symfony2 on a debian squeeze system.
The charset and collation of the DB are set to utf8 and utf8_general_ci and the db is created manually by reading in an sql script.
Now I enter some values for one of the tables with danish special chars like æøå and they simply wont display correctly.
My parameters.yml has set encoding: utf8 and in my config.yml the doctrine configuration has:
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
When I do a select in the mysql console, the characters display correctly and also if I try by replacing the output array in the controller by som hardcoded values, so the problem seems definately to stem from some kind of error/bug in doctrine2 configuration...
Does anybody have any clues on how to solve this?
Thanks a lot.
Ok I finally figured it out... there is some kind of double encoding going on with this configuration. Commenting out charset: UTF8 from doctrine dbal config solves the problem.
the problem might be in your application charset. it can be found in config.yml
framework:
charset: UTF-8
#translator: { fallback: lt }
#secret: %secret%
#other options

Setting up Django and Mysql on Mac os

I know there are tones of questions like that but sadly none of them resolved my problems and as I am new to django I don't know what is wrong.
So I installed django and mysql successfully, then the django-mysql package. When try to import the MySQLdb in python (running in the terminal) it works.
But my question is: how do you start mysql and create a database? And how to "link" it to django?
I tried running mysql but it doesn't work. So I tried /usr/local/mysql/bin/mysql which throws an error ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2).
What do I have to do?
Check your system preferences - there should be a MySQL icon on the bottom. There you can start it.
Also I recommend to install MySQLWorkbench to manage your db
I think that #init3 already answered the question regarding how to start mysql. How do you "link" your database to a django project? Well, in your settings.py file you find something like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'my_database_name', # Or path to database file if using sqlite3.
'USER': 'my_mysql_user', # Not used with sqlite3.
'PASSWORD': 'my_mysql_password', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
So change NAME, USER and PASSWORD accordingly and Django will use that database to create tables and populate fields.