MariaDB客户端默认使用UTF-8

时间:2013-04-12 22:56:31

标签: mysql mysqli mariadb

如何让MariaDB客户端使用UTF-8?我没有相同版本的MySQL的这个问题。 我试图在不通过客户端发出SET NAMES的情况下执行此操作。我使用cmd行客户端和php的mysqli驱动程序获取latin1。

PHP mysqli_get_charset:

print_r(mysqli_get_charset($link));
stdClass Object
(
    [charset] => latin1
    [collation] => latin1_swedish_ci
    [dir] => 
    [min_length] => 1
    [max_length] => 1
    [number] => 8
    [state] => 1
    [comment] => 
)

以下是命令行客户端的输出:

echo "show variables like 'char%';" | mysql -u root -p

Variable_name   Value
character_set_client    utf8
character_set_connection        utf8
character_set_database  latin1
character_set_filesystem        binary
character_set_results   utf8
character_set_server    latin1
character_set_system    utf8
character_sets_dir      /usr/share/mysql/charsets/

这是我的my.cnf文件。

[client]
port            = 3306
socket          = /var/run/mysqld/mysqld.sock
default-character-set=utf8
[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock
nice            = 0
default-character-set=utf8
[mysqld]
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
default-character-set  = utf8
default-collation      = utf8_general_ci
character_set_server   = utf8
collation_server       = utf8_general_ci
init_connect='SET NAMES utf8'
key_buffer              = 512M
max_allowed_packet      = 64M
thread_stack            = 192K
thread_cache_size       = 8
myisam-recover         = BACKUP
query_cache_limit       = 4M
query_cache_size        = 64M
expire_logs_days        = 10
max_binlog_size         = 100M
[mysqldump]
quick
quote-names
max_allowed_packet      = 64M
default-character-set=utf8
[mysql]
no-auto-rehash  # faster start of mysql but no tab completition
default_character_set=utf8
[isamchk]
key_buffer              = 128M
[client-server]
default-character-set=utf8
[client-mariadb]
default-character-set=utf8
!includedir /etc/mysql/conf.d/

2 个答案:

答案 0 :(得分:1)

问题是,您不应该通过客户端发出SET NAMES
由于此命令不足,因此除了服务器之外,您还必须设置客户端编码。这只能通过使用API​​函数来完成。因此,您不应在PHP脚本中发出SET NAMES查询,而应通过msql(i)_set_charset()函数或DSN设置客户端编码。

所以,请记住 - 更改默认编码不会减轻你在PHP中设置编码的必要性。

尽管如此,utf-8作为默认值比latin1更有意义。因此,请在my.ini部分

下的[mysqld]中添加以下行
init-connect='SET NAMES utf8'

答案 1 :(得分:1)

我弄明白了这个问题。我用他们的repo和apt-get安装了mariadb。一切似乎都没问题。我使用命令service mysql restart以及编码问题进行了这些非常慢的重启。我注意到有2个mysqld实例,我继续发送了一个kill命令,我不建议杀掉-9,常规杀死工作得很好。然后做了service mysql restart,它开始很快,字符编码按预期工作。