Is production: true

MySQL

MySQL is an open-source relational database management system (RDBMS). Its name is a combination of “My”, after co-founder Michael Widenius’s daughter, and “SQL”, which stands for Structured Query Language.

As an RDBMS, MySQL organizes data into one or more tables where records can be related to one another, helping to structure the information. Developers use SQL to create, modify, and query data from these relational databases, as well as to manage user access.

MySQL is widely used alongside other software to build applications that require relational database capabilities. It is a core component of the iconic LAMP web application stack (Linux, Apache, MySQL, Perl/PHP/Python), which once dominated the blogging and e-commerce industries, powering platforms such as WordPress, Drupal, Joomla, and phpBB, as well as major websites like Facebook, Flickr, MediaWiki, Twitter, and YouTube.

Web Site

Tools

  • MySQL Server
  • MySQL Workbench
  • MySQL Shell
  • phpMyAdmin

Check MySQL Version

  • From the command line:

    1
    
    mysql -V
    
  • From a SQL client:

    1
    
    SHOW VARIABLES LIKE "%version%";
    

Extract JSON field from JSON type

JSON value:

1
2
3
{
  "checkTime": 123456
}

Extract query:

1
SELECT JSON_EXTRACT(tableA, '$.checkTime') FROM schemaA.tableA;

Dumping a database from MySQL

The following code snippet shows how to dump a MySQL database for backup purposes:

1
2
3
4
5
dateStr=$(date +"%Y_%m_%d")
mysqldump \
        --user={username} \
        -p{password} \
        --databases {dbname} > {dbname}$dateStr.sql

Set MySQL from utf8 to utf8mb4

MySQL’s utf8 character set only supports up to 3 bytes per character, which means it cannot store certain Unicode characters (such as emojis and rare CJK characters). utf8mb4 is the recommended character set for full Unicode support (4 bytes per character).

Why switch to utf8mb4?

  • Full Unicode support (including emojis and rare characters)
  • Avoid data loss or errors when storing 4-byte characters
  • Better compatibility with modern applications

Recommended collation:

  • For MySQL 5.7 and below: utf8mb4_unicode_ci
  • For MySQL 8.0+: utf8mb4_0900_ai_ci (improved Unicode sorting)

Steps:

  1. Find your MySQL configuration file (often under /etc/mysql, but may vary):

    1
    2
    
    cd /etc/mysql
    ll
    
  2. Check for !includedir directives in my.cnf or mysqld.cnf to locate additional config files (e.g., /etc/mysql/mysql.conf.d).

  3. Backup your configuration file:

    1
    2
    
    cd /etc/mysql/mysql.conf.d/
    cp mysqld.cnf mysqld.cnf_bk_{date}
    
  4. Add or update these settings in mysqld.cnf:

    1
    2
    3
    4
    5
    6
    7
    8
    
    [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
    

    For MySQL 8.0+, you may use collation-server = utf8mb4_0900_ai_ci for better Unicode support.

  5. Restart MySQL server:

    1
    
    sudo service mysql restart
    
  6. Check the result:

    1
    
    SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
    

Converting existing databases/tables/columns: If your database, tables, or columns were created with utf8, you should convert them to utf8mb4 to avoid issues with 4-byte characters.

1
2
ALTER DATABASE dbname CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

For MySQL 8.0+, use utf8mb4_0900_ai_ci if desired.

Warning:

  • If you have existing data with 4-byte characters in utf8 columns, you may encounter errors or data truncation. Always backup your data before making changes.

References:


Create MySQL Docker container quickly

The following snippet helps create a simple MySQL Docker container for dev and testing purposes.

Start with default:

1
2
3
4
5
6
7
8
docker run \
    -p 3306:3306 \
    -v /data/mysql/mysql:/var/lib/mysql \
    --name mysql \
    -e MYSQL_ROOT_PASSWORD=my-secret-pw \
    --rm \
    -d \
    mysql:5.7.27

Start with case-insensitive table names:

1
2
3
4
5
6
7
8
9
docker run \
    -p 3306:3306 \
    -v /data/mysql/mysql:/var/lib/mysql \
    --name mysql \
    -e MYSQL_ROOT_PASSWORD=my-secret-pw \
    --rm \
    -d \
    mysql:5.7.27 \
    --lower_case_table_names=1