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:
1mysql -V -
From a SQL client:
1SHOW VARIABLES LIKE "%version%";
Extract JSON field from JSON type
JSON value:
|
|
Extract query:
|
|
Dumping a database from MySQL
The following code snippet shows how to dump a MySQL database for backup purposes:
|
|
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:
-
Find your MySQL configuration file (often under
/etc/mysql, but may vary):1 2cd /etc/mysql ll
-
Check for
!includedirdirectives inmy.cnformysqld.cnfto locate additional config files (e.g.,/etc/mysql/mysql.conf.d). -
Backup your configuration file:
1 2cd /etc/mysql/mysql.conf.d/ cp mysqld.cnf mysqld.cnf_bk_{date} -
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_ciFor MySQL 8.0+, you may use
collation-server = utf8mb4_0900_ai_cifor better Unicode support. -
Restart MySQL server:
1sudo service mysql restart -
Check the result:
1SHOW 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.
|
|
For MySQL 8.0+, use
utf8mb4_0900_ai_ciif desired.
Warning:
- If you have existing data with 4-byte characters in
utf8columns, 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:
|
|
Start with case-insensitive table names:
|
|