character-set-server VS default-character-set in MySQL - mysql

What's the difference between character-set-server and default-character-set in my.cnf? I want to set MySQL's connection to UTF8 and both of these seem to work. Is one better than the other?

Always use
character-set-server
The server character set and collation are used as default values if the database character set and collation are not specified in CREATE DATABASE statements. They have no other purpose (source)
character-set-server has replaced default-character-set setting, since default-character-set is now deprecated and can cause problems.
p.s. I believe the answer by newtover is wrong.

Here is a quote from MySQL docs:
You can force client programs to use specific character set as follows:
[client]
default-character-set=charset_name
This is normally unnecessary. However, when character_set_system differs from character_set_server or character_set_client, and you input characters manually (as database object identifiers, column values, or both), these may be displayed incorrectly in output from the client or the output itself may be formatted incorrectly. In such cases, starting the mysql client with --default-character-set=system_character_set—that is, setting the client character set to match the system character set—should fix the problem.
In other words, character_set_server and character_set_client are settings for mysqld, when default-character-set is a setting for mysql and other client libraries which overrides character_set_client assumed by mysqld by default.
You may not see the difference if you connect with mysql to localhost, but default-character-set is used as well when you connect to some other server, which may have other defaults.
UPD from 2018-08-17
As John Smith noticed, my answer is currently outdated, but the essence of it is still correct: character_set_server is a server variable, but when you connect to the mysqld with a client, you should specify the client and connection settings.
In these days much many computers used as clients and servers have utf-8 as a default for locale encoding and only because of that setting character-set-server instead of default-character-set might seem to work.
To be clear, to set up mysqld (that is server) to use utf-8 and some its collation as default for schema names, table names, column names and column values instead of latin1_swedish_ci you should set up characted-set-server in mysqld configuration.
But when you connect a mysql client to the server, your current charset may be any other, and to correctly convert to the server character set the data you send over a connection and to convert back the data sent from the server as response, you should specify the corresponding client settings:
SET character_set_client = charset_name;
SET character_set_results = charset_name;
SET character_set_connection = charset_name;
or the corresponding settings in mysql.ini for your client application. If all of them are the same, you can start your communication with server with a shorter statement:
SET NAMES 'charset_name' [COLLATE 'collation_name'];

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

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.

mysql change default character set latin1 to utf8

I have PHP 5.5.9 and MySQL 5.5.43 Innodb. This is in Kubuntu LTS /etc/mysql/my.cnf
printf("Current character set: %s\n", mysqli_character_set_name($conn));
Current character set: latin1
I've tried
default-character-set = utf8
first but then my mysql server didn't restart
Then I've tried
character-set-server = utf8
then my mysql shell didn't start
How can I rescue this issue? I've managed with
init_connect = 'SET NAMES utf8'
solve the problem for nonroot users, but the charset remains the same (it only displays as nonprivileged users UTF8, it is still Latin1).
And if (a big if) smnd could tell me WHERE should I use utf8mb4? Also in my.cnf file? I had my tables in utf8 and have migrated those with which I am working to utf8mb4 ...
Another Q: IS that applicable for Linux (Kubuntu) server:
https://www.youtube.com/watch?v=3M1Wpw4uOoM
Please help me, thanks, Gregor from lilaum.com
mysqli_set_charset('utf8').
init_connect is ignored for root connections.
utf8mb4 is effectively a superset of utf8. It is needed for Chinese and the new emoji, plus a scattering of obscure character sets.
PHP manual says:
MySQLnd always assumes the server default charset. This charset is sent during connection hand-shake/authentication, which mysqlnd will use.
MySQLnd is the MySQL native driver included with PHP 5.4 and above.
Unfortunately, I have to confirm that even in my system (PHP 5.6.17 with MySQL 5.5.47 on Debian Jessie 8.3) despite having configured /etc/mysql/my.cnf with the following entries
[mysqld]
character-set-server=utf8
[mysql]
default-character-set=utf8
the mysqlnd does not seem to assume the server default charset (utf8) but sets the connection charset to latin1 with collate latin1_swedish (maybe a default value considering MySQL AB was a swedish company).
So, it seems that there's no permanent way to set utf8 charset when connecting from a PHP application. You have to call
$mysqli->set_charset("utf8")
after each connection. This could be embedded in a custom 'dbconnect' PHP function in order to provide a sort of abstraction layer.

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

Change variable in MySQL

In MySQL, how to change a variable such as character_set_client?
mysql> show variables like 'character_set%';
-------------------------+-------
character_set_client | latin1
to obtain
character_set_client | utf8
When starting MySQL client you have to specify --default-character-set=charset_name
From manual:
Use charset_name as the default character set for the client and
connection.
A common issue that can occur when the operating system uses utf8
or another multi-byte character set is that output from the mysql
client is formatted incorrectly, due to the fact that the MySQL
client uses the latin1 character set by default. You can usually
fix such issues by using this option to force the client to use the
system character set instead.
For example:
$>mysql -uUser -pPassword --default-character-set=utf8
For an example of how to set it via connection string see here.