Can not insert UTF8 to Database MySQL in Linux - mysql

When create table, I have setted charset = utf8.
I create 1 store procedure to insert data to database.
When insert data UTF8 to Database on Window, it works OK.(Display data correctly)
But it doesnot work in Linux.(Display data not correctly)
The strange thing is insert UTF8 work fine in window, but when i deploy MySQL in linux, when insert data to database it insert wrong UTF8 value.
Thanks for help
Edit: Update more detail follow comment of #Col. Shrapnel

You should start mysql console with --default-character-set=utf8:
mysql --default-character-set=utf8 -uyour_user -p

most probably you didn't specify client character set by issuing SET NAMES UTF8 query
but there can be other problems - your database or a web page may be not in the utf-8

I just found a solution for my problem: Edit file my.cnf under /etc/my.cnf as below:
[mysqld]
default-character-set=utf8
default-collation=utf8_general_ci
character-set-server=utf8
collation-server=utf8_general_ci
init-connect='SET NAMES utf8'
[client]
default-character-set=utf8
But i still confuse why this bug occur in Linux, not in Window.

Related

mysql_install_db wrong charset and collation

I am creating a new server with the mysql_install_db tool. It sets the correct datadir, port, password, service etc. But My problem is that the charset and collation of my base tables are wrong. They need to be utf8mb4 and utf8mb4_general_ci.
I can't find a way to change these tables with the installation. When I change my.cnf/my.ini it only changes for newly created databases. But Since mysql_install_db creates the system databases, they are created wrong.
It also looks like my msyql_install_db.exe does not accept a defaults-file argument.
Something else is also weird. If I run the command to create a new database, it will also use utf8mb3 while I explicitly set the COLLATE to utf8mb4_unicode_ci.
CREATE DATABASE IF NOT EXISTS tt DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
I am using mariadb 10.6.4 which is the latest version.
Anyone knows how to setup the correct charset and collation to the system databases?
Tools\mariadb-winx64\bin\mysql_install_db.exe --datadir="Tools\mariadb-data" --password=PASSWORD --port=8137 --service=MyDB
[Update]
I tried to setup my.cnf with the following, but seems to take no effect: Change MySQL default character set to UTF-8 in my.cnf?
Also tried using a different way with initializing-insecure, but also the same results. I created a my.cnf with the correct encoding, but still got the wrong table encoding:
mariadb-winx64\bin\mariadbd.exe --defaults-file=./my.cnf --initialize-insecure --datadir=./Test
And my.cnf
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
skip-grant-tables
port = 5137
collation-server = utf8mb4_unicode_ci
init-connect='SET NAMES utf8mb4'
character-set-server = utf8mb4

Set default collation for create database

I'm trying to set the default-default characterset for a new created mysql database. Because I'm lazy, I will use "CREATE DATABASE blahblah;" without providing additional "CHARSET" or "DEFAULT COLLATE". After "SHOW CREATE DATABASE blahblah;" I see:
blahblah | CREATE DATABASE `blahblah` /*!40100 DEFAULT CHARACTER SET latin1 */
Latin1? I need utf8. Let's change my.cnf and mysqld.cnf.
mysqld.conf (server config) is
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
.my.cnf (my local file) is
[client]
default-character-set=utf8
Just to by sure, let's restart the server, drop the database, create and... again latin1.
To be honest, I also tried character_set_server (underscores, instead of dashes), copy the entries to .my.conf back and forth... and there is still the f***g latin1.
So... did I made a typo? Do I miss any extra line somewhere? Is it impossible to set a default-default charset, so I need to provide them everytime I do a "create database..."?
Ubuntu 18.04.2, mysqld 5.7.26-0ubuntu0.18.04.1
(I am aware of the Change default collation/character set, but it doesnt work)

MySQL with multiple database encodings in my.cnf

I have a database set up to use the encoding UTF-8 multibyte-4. This is configured in the my.cnf file:
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
(...)
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
I would like to add a 2nd database, let's call them DB1 and DB2.
The problem: The application that uses this 2nd database DB2 cannot work with the encoding I have configured in my.cnf. It's a 3rd party application, so I have no possibility to change something, all I can do is modify the databse setup.
If I comment the above lines in my.cnf, the 3rd party application works, I guess it's using plain UTF-8 encoding. But now the system using the 1st database DB1 fails on encoding.
Is there a way to specify a default encoding in my.cnf for each database, separating DB1 encoding from DB2? Searching the internet has not shown me any solution other than using a separate/2nd MySQL installation.
Using MySQL version 5.6.x
Any hints are greatly appreciated.
Shoot that 3rd party software.
Have utf8 in my.cnf. Perform SET NAMES utf8mb4; in that applications that need it and that you can get to.

Using utf8mb4 in MySQL

In order to use 4-byte utf8mb4 in MySQL (5.6.11), I have set the following variables in the my.ini file (my.cnf is not found). This file is located in a hidden folder named Application Data (C:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5.6) on Windows XP. It is not available under the installation directory.
[client]
port=3306
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
init-connect='SET NAMES utf8mb4'
collation_server=utf8mb4_unicode_ci
character_set_server=utf8mb4
And then issuing the following command,
SHOW VARIABLES
WHERE Variable_name
LIKE 'character\_set\_%'
OR Variable_name LIKE 'collation%';
still displays the following list.
From the picture itself, it is clear that several variables are still using 3-byte utf8.
Before doing this, the following command had already been issued to make corresponding changes to the database itself.
ALTER DATABASE database_name
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
And the following command had also been issued on each and every table in the said database.
ALTER TABLE table_name
CONVERT TO CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
Nevertheless, what is the reason why some variables have not yet been set to the said character set as well as the collation? What is missing?
The system (operating system) itself was restarted after every single task specified above had been carried out.
The client usually sets these values when connecting. The settings in my.ini are merely defaults which apply when the client does not explicitly specify a connection encoding. Since they're unreliable, every client should specify a connection encoding. Since you've got some fancy screenshot there I'll guess that you're connecting with some GUI utility which probably explicitly does set some connection encodings.
PHP example of setting a connection charset:
new PDO('mysql:host=localhost;charset=utf8mb4')
If you show your global variables, you might see that all your settings are correct actually.
SHOW GLOBAL VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
This might be related to this bug
I also faced the same problem in the past. I changed a DB charset from utf8 to utf8mb4. Executing queries directly from mysql command line was ok but I had problem inserting emojis with Workbench. I ended up setting it manually by running
SET NAMES 'utf8mb4'
every time I open a connection to my DB with Workbench.
Using Sequel Pro as alternative was fine as well.
I think you are connecting as root, hence the init-connect='SET NAMES utf8mb4' is not executed.
It is unwise to use root (or SUPER) for any application code; just for administrative actions.
On win7
use "win + R" and type "services.msc"
find service of mysql
check the file path. It will tell you where the my.ini
open and add some properties:
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
restart the mysql service

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.