登陆进入数据库,本机测试数据库没有设置密码。其余密码登陆请执行命令。(假设服务器是127.0.0.1,用户名是root)mysql
这里输入代码
[root@localhost ~]mysql -h 127.0.0.1 -u root -psql
[root@localhost ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 10176 Server version: 5.5.52-MariaDB MariaDB Server Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show variables like 'collation_%'; +----------------------+-------------------+ | Variable_name | Value | +----------------------+-------------------+ | collation_connection | utf8_general_ci | | collation_database | latin1_swedish_ci | | collation_server | latin1_swedish_ci | +----------------------+-------------------+ 3 rows in set (0.00 sec) MariaDB [(none)]> show variables like 'character_set_%'; +--------------------------+----------------------------+ | 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/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) #修改数据库字符集 MariaDB [(none)]>use cacti MariaDB [(none)]> alter database cacti character set utf8; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> show variables like 'character_set_%'; +--------------------------+----------------------------+ | 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/ | +--------------------------+----------------------------+ 8 rows in set (0.01 sec) MariaDB [(none)]>
修改数据库和服务器编码。数据库
MariaDB [(none)]> set character_set_database=utf8; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> set character_set_server=utf8; Query OK, 0 rows affected (0.00 sec) #修改查询结果编码 MariaDB [(none)]> SET character_set_results = utf8; Query OK, 0 rows affected (0.00 sec)
建立数据库指定数据库并字符集。服务器
mysql>create database mydb character set utf-8;
修改编码也能够修改MySQL数据库配置文件并重启服务,本文不加以介绍。测试