MySQL - how to change default charset to uft8? - mysql

I need to change the default charset of MySQL. I want the new database and all tables to use UTF-8. When I create django unit tests, the tests fail while creating the DB because it is set to latin1.
I can't do that in my.cnf. I use Ubuntu and MySQL installed from repository.

create database `something` default charset=utf8;
http://dev.mysql.com/doc/refman/5.0/en/create-database.html

Try:
# Alter your database
ALTER DATABASE database_name DEFAULT CHARSET=utf8;
SHOW CREATE DATABASE database_name;
CREATE DATABASE `zzz_test` /*!40100 DEFAULT CHARACTER SET utf8 */

Related

How to convert the charset of system schema `mysql` in proper way?

While upgrading mysql from 5.6 -> 5.7 -> 8.0.23 in step 5.7 -> 8.0.23 I got a recommendation:
The following objects use the utf8mb3 character set. It is recommended to convert them to use utf8mb4 instead, for improved Unicode support.
More Information:
https://dev.mysql.com/doc/refman/8.0/en/charset-unicode-utf8mb3.html
...
mysql - schema's default character set: utf8
...
So, Is it enough to change the charset of the schema mysql to utf8mb4? Or at first, does it need to convert all the tables of this schema to utf8mb4?
The way to change a schema's default characters set:
ALTER SCHEMA mysql DEFAULT CHARACTER SET utf8mb4;
However, I don't know if your admin user on RDS has privileges to do that on the mysql system schema. It's possible that you don't, because RDS limits the privileges of your admin user.

heroku db:pull to local mysql utf8

I'm trying to dump a heroku db with heroku db:pull
The problem is that the data that has special character (like 'ñ') are not store propertly.
I see that the local tables are created with default charset=latin1
is there a way that the heroku script create the table with charset utf8?
heroku db:push -t tables mysql://user:pass#localhost/mydb encoding=UTF8 --confirm myapp
The solution is to set the default charset in mysql server itself like in:
Change MySQL default character set to UTF-8 in my.cnf?
doing that and after restart the local mysql, heroku db:pull will create the tables with the default utf8 charset.

utf8 garbled when importing into mysql

Importing UTF8-encoded data into mysql is not working for me. UTF8 characters are corrupted. For example Nöthnagel is displayed as Nöthnagel
I have created a sql dump file to do the importing which contains UTF-8 encoded data. For example:
INSERT INTO `users` VALUES(1, 'Fred','Nöthnagel');
The sequence of bytes representing ö in the file is c3 b6 which I believe is correct, as it displays correctly in vim and in my bash shell which has these environment variables set:
$ env | grep -i utf
LANG=en_US.UTF-8
XTERM_LOCALE=en_US.UTF-8
The mysql db was created as follows:
mysql> CREATE DATABASE mydb CHARACTER SET utf8;
The mysql table was created so:
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(30) NOT NULL,
`last_name` varchar(30) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `last_name` (`last_name`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
I am importing the dump file like so:
mysql -u root -psecret mydb < mydump.sql
Please tell me what is missing from the above.
I think it might have something to do with collation as well, but I'm not sure. In my case it certainly did, since I had to support cyrillic.
Try this, worked for me:
Set initial collation while creating the target database to utf8_unicode_ci
Add SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'; to the top of your sql file
Run mysql -u root -p --default-character-set=utf8 yourDB < yourSQLfile.sql
One more thing, in order to properly get the UTF-8 data form your database, you'll have to modify your connection string as well. For example:
mysql.url=jdbc:mysql://localhost:3306/nbs?useJvmCharsetConverters=false&useDynamicCharsetInfo=false&useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&useEncoding=true
Additionally, take a look at what my problem was.
Use this command for import utf8 table to database :
mysql -u USERNAME -pPASSWORD --default_character_set utf8 DATABASE < file.sql
The problem was solved by adding this at the top of the sql file:
SET NAMES utf8;
I had a similar problem. There are a number of variables that should be UTF8, not only the database, those include the client, the connection, ther server ...etc.
The solution to your problem is described in this article. The described solution is portable, so it does not only work for utf8, but for all other character sets. You may need to modify it to fit your needs.

mysql utf-8 support

when I create a new table, it always is 'latin-swedish-ci' by default. my goal is that this table can store utf-8 text. I tried this way, give me error .
cd C:\mysql-5.5.14-winx64\bin
mysqld --standalone --console --default-storage-engine=InnoDB --default-character-set=utf8
[ERROR] mysqld: unknown variable 'default-character-set=utf8'
Your Database probably has the default charset on latin1 and default collation set to latin1-swedish-ci and so altering the table should help
http://dev.mysql.com/doc/refman/5.1/en/alter-database.html
ALTER DATABASE `MyDatabaseName`
CHARACTER SET utf8
COLLATE utf8_general_ci;
You can always specify column encodings/collations within CREATE TABLE statement. This has additional benefit of making your data structure more portable in case you ever need to move it to another server.

How can I change my MySQL collation in WAMPSERVER

How can I change my MySQL collation in WAMPSERVER from latin1_swedish_ci to UTF-8 because I think my HTML special characters are getting all messed up
put in your C:\wamp\bin\mysql\mysql5.5.24\my.ini:
character-set-server=utf8
collation-server=utf8_general_ci
You could use
set names 'utf8'
each time you open a connection.
Or add the following line to your my.ini file and restart your server.
default-character-set=utf8
If you've already got tables set up you'll need to alter them too you can alter them with:
ALTER TABLE tablename COLLATE utf8_general_ci
etc or pop into phpmyadmin and do it there. Remember if you alter database collation it'll only affect new tables created after that not pre-existing ones in that database already, so you will need to alter them also.