If reprint this article, please indicate the original source: http://hi.baidu.com/hexiong/blog/item/e860e5dd9b0d3ae376c6381a.html
(Hexiong @ Baidu or iihero @ csdn)
Sometimes someone asks you to use the MySQL command line. It is annoying to input boring options such as default-character-set = GBK. A few more characters are entered.
In addition, sometimes I am DIY and seldom use the installed version on windows. I always decompress it, and then put my. ini to a special location under the decompressed directory. In this way, the MySQL Command Line cannot load the configuration file by default.
For the order of loading my. ini files, see: http://hi.baidu.com/hexiong/blog/item/313534a8b7e0dcbdcb130cc5.html
Taking windows as an example, it loads:
C:/Windows/My. cnf c:/Windows/My. ini C:/My. cnf c:/My. ini
For $ installdir/My. ini, hey, it's not the installation version. This variable is probably not readable.
If the preceding four files are successfully loaded, the specified configuration file is no longer loaded.
Therefore, to enable MySQL command lines to directly use the GBK character set, you can use the following methods:
1. command line:
Mysql-U test-p -- default-character-set = GBK
2. command line:
MySQL -- defaults-file = <your real my. ini path>-U test-P
Note: -- defaults-file must always be placed at the first parameter.
3. directly create a client version of my. ini, such:
# Uncomment or add only the keys that you know how works.
# Read the MySQL manual for instructions
[Client]
Port = 3306
Default_character_set = GBK
Save it to C:/My. ini
Note the following:
If the custom MySQL my. ini file is in the top four paths, you can directly change the default_character_set value under [client] to GBK. Otherwise, you can generate a my. ini for the client.
Method 3 is convenient, but because it is a global configuration file, sometimes it will inevitably affect each other, especially if one machine has multiple MySQL instances.
At this time, you can use the command line:
D:/> mysql-U test-p -- default-character-set = Latin1
Enter Password :********
Welcome to the MySQL monitor. commands end with; or/g.
Your MySQL connection ID is 10 to server version: 5.0.9-beta-NT
Type 'help; 'or'/H' for help. type'/C' to clear the buffer.
Mysql> show variables like 'Char % ';
+ -------------------------- + ------------------------------------------- +
| Variable_name | value |
+ -------------------------- + ------------------------------------------- +
| Character_set_client | Latin1 |
| Character_set_connection | Latin1 |
| Character_set_database | GBK |
| Character_set_results | Latin1 |
| Character_set_server | GBK |
| Character_set_system | utf8 |
| Character_sets_dir | D:/mysql-5.0.9-beta-win32/share/charsets/|
+ -------------------------- + ------------------------------------------- +
7 rows in SET (0.00 Sec)
-- Default-character-set = Latin1 will overwrite the character set option value of [client] in the Global File My. ini.
Of course, if we carefully check the source code, we will find that $ installdir refers to the environment variable: mysql_home, huh, huh.
If you specify mysql_home, when you cannot find the first four files, it will find $ mysql_home/My. CNF or my. ini
The source code is as follows:
Static const char ** init_default_directories (mem_root * alloc)
{
Const char ** dirs;
Char * env;
Int errors = 0;
Dirs = (const char **) alloc_root (alloc, default_dirs_size * sizeof (char *));
If (dirs = NULL)
Return NULL;
Bzero (char *) dirs, default_dirs_size * sizeof (char *));
# Ifdef _ win __
{
Char fname_buffer [fn_reflen];
If (My_get_system_windows_directory(Fname_buffer, sizeof (fname_buffer )))
Errors + = add_directory (alloc, fname_buffer, dirs );
If (Getwindowsdirectory(Fname_buffer, sizeof (fname_buffer )))
Errors + = add_directory (alloc, fname_buffer, dirs );
Errors + =Add_directory(Alloc, "C:/", dirs );
If (my_get_module_parent (fname_buffer, sizeof (fname_buffer ))! = NULL)
Errors + = add_directory (alloc, fname_buffer, dirs );
}
# Elif defined (_ Netware __)
Errors + = add_directory (alloc, "SYS:/etc/", dirs );
# Else
Errors + = add_directory (alloc, "/etc/", dirs );
Errors + = add_directory (alloc, "/etc/MySQL/", dirs );
# If defined (default_sysconfdir)
If (default_sysconfdir! = "")
Errors + = add_directory (alloc, default_sysconfdir, dirs );
# Endif/* default_sysconfdir */
# Endif
If ((ENV = getenv (stringify_arg (default_home_env))))
Errors + = add_directory (alloc, ENV, dirs );
/* Placeholder for -- defaults-extra-file = <path> */
Errors + = add_directory (alloc, "", dirs );
# If! Defined (_ win __)&&! Defined (_ Netware __)
Errors + = add_directory (alloc ,"~ /", Dirs );
# Endif
Return (errors> 0? Null: dirs );
}
So much analysis.