Java Mysql Emoji

premise

The minimum supported version of Mysql for utf8mb4 is 5.5.3+, if not, please upgrade to a newer version.

If you are using java server, upgrade or make sure your mysql connector version is higher than 5.1.13, otherwise utf8mb4 still cannot be used.

Preparation: Check the current version and character set

To view the current version, Linux command:

# mysql -V

Log in to Mysql and enter the command line mode:

# mysql -uroot -p你的密码

View the current Mysql character set settings:

msql> SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';

Step 1: Change the Mysql configuration file my.cnf

The configuration file under Windows is my.ini in the installation directory.

The Mysql configuration file under Linux is generally in: /etc/my.cnf.

The Mysql configuration file under Linux is generally in: /etc/mysql/mysql.cnf.

The character configuration is changed to:

[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
init_connect='SET NAMES utf8mb4'

Step 2: Change the existing database, table and column character set

msql> ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

msql> ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

msql> ALTER TABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

The database_name, table_name, and column_name should be replaced with your corresponding database name, table name, and column name. So is the length value of VARCHAR.

Step 3: Restart Mysql and check the changed character set settings

Restart command under Linux:

# service mysql restart

Log in to Mysql again and view the character set settings:

msql> SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';

The result should now look like this:

+--------------------------+--------------------+
| Variable_name            | Value              |
+--------------------------+--------------------+
| character_set_client    | utf8mb4            |
| character_set_connection | utf8mb4            |
| character_set_database  | utf8mb4            |
| character_set_filesystem | binary            |
| character_set_results    | utf8mb4            |
| character_set_server    | utf8mb4            |
| character_set_system    | utf8              |
| collation_connection    | utf8mb4_unicode_ci |
| collation_database      | utf8mb4_unicode_ci |
| collation_server        | utf8mb4_unicode_ci |
+--------------------------+--------------------+
rows in set (0.00 sec)

Step 4: About JDBC URL

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf8&autoReconnect=true
jdbc.username=root
jdbc.password=password

autoReconnect means: When the database connection is abnormally interrupted, is it automatically reconnected? Default is false.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326085137&siteId=291194637