Log information record table|A comprehensive understanding of mysql system library


In the last issue of "Replication Information Recording Table | A Comprehensive Understanding of the MySQL System Library", we introduced in detail the replication information record table in the mysql system library. In this issue, we will bring you the eighth part of the series "Logging and other miscellaneous Table|A comprehensive understanding of mysql system library", please follow us to start the system learning journey of mysql system library!

Log information record table

01

    1.1. Overview of Log Information

MySQL's log system includes: general query log, slow query log, error log (recording error information when MySQL Server is started, running, and stopping), binary log (logical log that records data changes during MySQL Server operation), Relay log (record the main library data change log obtained from the library IO thread from the main library), DDL log (record the metadata change information when the DDL statement is executed. Only writing to files is supported in 5.7, and writing to innodb_ddl_log is supported in 8.0 In the table, note that the ddl log is different from the alter log of the online ddl, so don’t confuse it). Among them, in MySQL 5.7, only the general query log and the slow query log support writing to the table (also support writing to the file ), other log types only support writing to files in MySQL 5.7. Therefore, the following description of the log system tables mainly introduces the general query log and slow query log tables.

By default, except for the error log on Windows, all logs of other platforms are not enabled by default (DDL logs are only created when needed, and there are no user-configurable options).

By default, all logs are written in the datadir directory, but you can use the path parameters corresponding to each log to change the path yourself.

  • general query log:general_log_file=/home/mysql/data/mysqldata1/mydata/localhost.log

  • error log:log_error=/home/mysql/data/mysqldata1/log/error.log

  • slow query log:slow_query_log_file=/home/mysql/data/mysqldata1/slowlog/slow-query.log

  • binary log:log_bin_basename=/home/mysql/data/mysqldata1/binlog/mysql-bin、log_bin_index=/home/mysql/data/mysqldata1/binlog/mysql-bin.index

  • relay log:relay_log_basename=/home/mysql/data/mysqldata1/relaylog/mysql-relay-bin、relay_log_index=/home/mysql/data/mysqldata1/relaylog/mysql-relay-bin.index

By default, all logs are written to disk files, but the general query log and slow query log can be saved to the tables mysql.general_log and mysql.slow_log by setting log_output=TABLE (DDL log can be configured in 8.0, but Print to the error log, or save it in the table innodb_ddl_log).

By default, the binary log automatically rolls according to the size set by the max_binlog_size parameter, and the relay log automatically rolls according to max_relay_log_size or max_binlog_size (if max_relay_log_size is not set, it rolls according to max_binlog_size). Other log types will not roll, and always use the same file. Therefore, after other log types grow too large, you need to cut them by yourself.

  • Generally use mv file file.bak; and then execute the refresh command. The refresh command can log in to the instance and use the flush logs command to refresh and regenerate a new log file. However, this command refreshes all log types. For specific log types, you can use: flush binary logs; flush binary logs, flush error logs; refresh error logs, flush general logs; refresh ordinary query logs, flush slow logs; refresh slow query logs, flush relay logs; refresh relay logs, flush engine logs; refresh storage engine Any relevant refreshable logs.

  • You can also use Server's flush tables; statement or flush table with read lock; statement.

  • The refresh operation can also be implemented using some command-line tool options, such as the flush-logs option of the mysqladmin command, or the flush-logs option and the --master-data option of mysqldump.

The log table implementation has the following characteristics:

Usually, the main purpose of the log table is to provide an access interface for the program to view the operation of SQL in the Server. Therefore, it is more convenient to store log records in a table than in a disk file, because storing in a table can be accessed remotely These logs are recorded without the need to log in to the operating system to access disk files.

The log table can use CREATE TABLE, ALTER TABLE and DROP TABLE statements, but the premise is that the corresponding switch needs to be used to close the table first, and it cannot be operated during use (for example: set global general_log=0, and then operate the general_log table).

The general_log and slow_log tables are the CSV engine by default. They use a comma-separated format to store log records. CSV data files can be easily imported into other programs for processing, such as excel spreadsheets.

The log table can be modified to MyISAM engine, but the use of the table must be stopped before modification. The legal engines are CSV and MyISAM, other engines are not supported.

To disable the logging table in order to perform the corresponding DDL statement operation, you can use the following steps (take the slow query table as an example to illustrate, the slow_log and general_log table operations are similar).

SET @old_log_state = @@ global.general_log;
SET GLOBAL general_log ='OFF';
ALTER TABLE mysql.general_log ENGINE = MyISAM;
SET GLOBAL general_log = @old_log_state;

You can use TRUNCATE TABLE to clear log records.

You can use RENAME TABLE to archive log tables. The old and new tables do an atomic name swap operation, as follows:

use mysql;
DROP TABLE IF EXISTS general_log2;
CREATE TABLE general_log2 LIKE general_log;
RENAME TABLE general_log TO general_log_backup,general_log2 TO general_log;

Precautions

  • You can use the CHECK TABLE statement.

  • You cannot use the LOCK TABLE statement.

  • INSERT, DELETE and UPDATE statements cannot be used. The record changes of the log table are maintained internally by the Server and cannot be operated manually.

  • The settings of the FLUSH TABLES WITH READ LOCK and read_only system variables have no effect on the log table. The log table can always be written inside the Server.

  • The data change operation of the log table will not be recorded in binlog, so it will not be copied to the slave database.

  • You can use FLUSH TABLES or FLUSH LOGS statements to refresh log tables or log files.

  • The log table does not support partition tables.

  • The mysqldump dump contains statements to recreate these tables to restore the log table structure after reloading the dump file, but the contents of the log table will not be dumped.

PS: MySQL query logs, error logs, etc. are recorded in clear text. Therefore, these logs may record the user's clear text password information. You can use the rewrite plugin to record in the original format. For details, see the link:

  • https://dev.mysql.com/doc/refman/5.7/en/plugin-types.html#query-rewrite-plugin-type

  • https://dev.mysql.com/doc/refman/5.7/en/rewriter-query-rewrite-plugin.html

    1.2. Detailed log table

1.2.1. general_log

This table provides query execution record information of ordinary SQL statements, which is used to find out what SQL the client has executed on the server (Of course, you can also use the enterprise version of the audit log audit plug-in to record. This article will not go into details. Interested children's shoes Do your own research).

The information in this table will be recorded when SQL starts to execute, instead of waiting for the end of SQL execution to be recorded.

The following is the information stored in the table.

root@localhost : (none) 07:25:50> set global log_output='TABLE';
Query OK, 0 rows affected (0.00 sec)
root@localhost : (none) 07:26:20> set global general_log=1;
Query OK, 0 rows affected (0.01 sec)
root@localhost : (none) 07:26:32> select * from mysql.general_log;
+----------------------------+---------------------------+-----------+-----------+--------------+---------------------------------+
| event_time | user_host | thread_id | server_id | command_type | argument |
+----------------------------+---------------------------+-----------+-----------+--------------+---------------------------------+
| 2018-06-19 19:26:32.891371 | root[root] @ localhost [] | 3 | 3306102 | Query | show databases |
| 2018-06-19 19:26:42.012064 | root[root] @ localhost [] | 3 | 3306102 | Query | select * from mysql.general_log |
+----------------------------+---------------------------+-----------+-----------+--------------+---------------------------------+
2 rows in set (0.00 sec)
root@localhost : (none) 07:26:42> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 3 |
+-----------------+
1 row in set (0.00 sec)

Table field meaning.

  • event_time: The value of the log_timestamps system variable at the moment the query log is recorded in the table, used to mark when the query log is stored in the database.

  • user_host: Indicates the source of the query log record, including user name and host name information.

  • thread_id: indicates the process_id when the query log record was executed.

  • server_id: indicates the ID of the database instance that executes the query.

  • command_type: indicates the command type of the query, usually query.

  • argument: Represents the SQL statement text for executing the query.

mysqld writes the statements to the query log in the order in which they are received (this may be different from their execution order).

In a master-slave replication architecture.

  • When the statement-based log format is used on the main library, the slave library will record these statements in its own query log after replaying these statements (you need to enable the query logging function from the library), and use the binlog recorded in the statement format When using the mysqlbinlog command to parse and import it into the database, if the instance has the query logging function enabled, these parsed statements will also be recorded in the query log.

  • When the row-based log format is used on the master database, these statements will not be counted in the query log of the slave database after the data changes are replayed by the slave database.

  • When the mixed log format is used on the main library, if the main library is recorded in statement format, the statement will be recorded in its own query log after replaying these data changes from the library (you need to enable the query logging function from the library ), if the main library is converted to row format when recording binlog, it will be the same as row format replication. After replaying these data changes from the library, these statements will not be recorded in its own query log.

The query log can use the system variable sql_log_off variable to dynamically close the query logging function of the current session or all sessions (similar to the sql_log_bin system variable).

The query log switch general_log variable and the query disk log file path general_log_file variable can be dynamically modified (if there is already a query log open, use the general_log_file variable to modify the query log path to close the old query log and open the new query log). When the query log is enabled, the query log will be kept to the destination specified by the system variable log_output.

If the query log is enabled, the query log file will be reopened when the server restarts. If the query log exists, it will be reopened directly. If the query log does not exist, it will be recreated. If the query log needs to be dynamically archived when the server is running, You can operate according to the following commands (linux or unix platforms).

shell> mv host_name.log host_name-old.log
shell> mysqladmin flush-logs
shell> mv host_name-old.log backup-directory
# 在Windows上,请直接使用重命名,而不是mv命令

You can also turn off the query log function through a statement when the Server is running, then use an external command to archive, and then re-enable the query log, so that you don’t need to use the flush-logs command to refresh the log files. This method is applicable to any platform. The command is as follows:

SET GLOBAL general_log ='OFF';
# 在禁用日志的情况下,从外部重命名日志文件;例如,从命令行。然后再次启用日志:SET GLOBAL general_log ='ON';# 此方法适用于任何平台,不需要重新启动服务器。

By default, if the statement executed in the Server carries the user password, it will be rewritten by the Server and then written to the query log. If you need to record the plaintext password, you need to use the --low-raw option to start the Server( Using this option will bypass the password rewriting function). Generally, it is not recommended to record the password plaintext information in the query log because it is not safe, but if necessary, use your own judgment (for example, when you need to query the original sentence information to troubleshoot problems).

  • If the password is specified as a hash value in the statement with password, the password string will not be rewritten, for example: CREATE USER'user1'@'localhost' IDENTIFIED BY PASSWORD'not-so-secret'; It is recorded for the original reason, but if the PASSWORD keyword CREATE USER'user1'@'localhost' IDENTIFIED BY'not-so-secret'; is removed, it will be rewritten as: CREATE USER'user1' in the query log @'localhost' IDENTIFIED WITH'mysql_native_password' AS''.

Some grammatically wrong SQL will not be recorded in the query log by default. Starting the Server with the --low-raw option will record all the original SQL statements.

The time stamp information in the query log table comes from the system variable log_timestamps (including the time stamps in the slow query log file and the error log file are derived from the value of this system variable). The time stamp value can be queried using the CONVERT_TZ() function or The time stamp information in these tables will be converted from the local system time zone to any desired time zone by setting the session (modify the time_zone variable value at the session level).

1.2.2. slow_log

This table provides SQL that query execution time exceeds the long_query_time setting value, or does not use indexes (need to turn on the parameter log_queries_not_using_indexes=ON) or management statements (need to turn on the parameter log_slow_admin_statements=ON).

The following is the information stored in the table.

root@localhost : test 08:46:04> set global long_query_time=0;
Query OK, 0 rows affected (0.01 sec)
root@localhost : test 08:55:14> set global slow_query_log=1;
Query OK, 0 rows affected (0.01 sec)
# 断开会话重新连接
root@localhost : (none) 08:56:12> use test
Database changed
root@localhost : test 08:56:13> show tables;
+----------------+
| Tables_in_test |
+----------------+
| customer |
| product |
| shares |
| test |
| transreq |
+----------------+
5 rows in set (0.01 sec)
root@localhost : test 08:56:16> select * from test;
+---+---+------+------+------+------+
| a | b | c | d | e | f |
+---+---+------+------+------+------+
| 1 | 1 | 1 | 1 | 1 | 1 |
| 2 | 2 | 2 | 2 | 2 | 2 |
| 3 | 3 | 3 | 3 | 3 | 3 |
| 4 | 4 | 4 | 4 | 4 | 4 |
| 5 | 5 | 4 | 4 | 5 | 5 |
+---+---+------+------+------+------+
5 rows in set (0.01 sec)
root@localhost : test 08:56:18> select * from mysql.slow_log;
+----------------------------+---------------------------+-----------------+-----------------+-----------+---------------+------+----------------+-----------+-----------+----------------------------------+-----------+
| start_time | user_host | query_time | lock_time | rows_sent | rows_examined | db | last_insert_id | insert_id | server_id | sql_text | thread_id |
+----------------------------+---------------------------+-----------------+-----------------+-----------+---------------+------+----------------+-----------+-----------+----------------------------------+-----------+
| 2018-06-19 20:56:12.254716 | root[root] @ localhost [] | 00:00:00.000286 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306102 | select @@version_comment limit 1 | 4 |
| 2018-06-19 20:56:12.258551 | root[root] @ localhost [] | 00:00:00.000153 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306102 | select USER() | 4 |
| 2018-06-19 20:56:13.975382 | root[root] @ localhost [] | 00:00:00.000247 | 00:00:00.000000 | 1 | 0 | | 0 | 0 | 3306102 | SELECT DATABASE() | 4 |
| 2018-06-19 20:56:13.975627 | root[root] @ localhost [] | 00:00:00.000095 | 00:00:00.000000 | 1 | 0 | test | 0 | 0 | 3306102 | Init DB | 4 |
| 2018-06-19 20:56:16.277207 | root[root] @ localhost [] | 00:00:00.000490 | 00:00:00.000264 | 5 | 5 | test | 0 | 0 | 3306102 | show tables | 4 |
| 2018-06-19 20:56:18.936831 | root[root] @ localhost [] | 00:00:00.000694 | 00:00:00.000400 | 5 | 5 | test | 0 | 0 | 3306102 | select * from test | 4 |
+----------------------------+---------------------------+-----------------+-----------------+-----------+---------------+------+----------------+-----------+-----------+----------------------------------+-----------+
6 rows in set (0.00 sec)

Table field meaning.

  • start_time: The value of the log_timestamps system variable when the slow query log is recorded to the table.

  • user_host: A value with username and hostname (IP) format, used to mark the source of access.

  • query_time: The total execution time of slow query statements.

  • lock_time: The time that the slow query statement holds the lock.

  • rows_sent: The number of data records finally returned to the client by the slow query statement.

  • rows_examined: The number of inspection records of slow query statements in the storage engine.

  • db: The default library name when the slow query statement is executed.

  • last_insert_id: usually 0.

  • insert_id: usually 0.

  • server_id: The server id that generated the slow query statement.

  • sql_text: The sentence text of the slow query log.

  • thread_id: The process_id of the thread that generated the slow query log.

The slow query log contains SQL statements whose execution time exceeds the number of seconds set by the long_query_time system variable, and contains SQL statements whose number of rows need to be checked exceeds the value set by the min_examined_row_limit system variable (by default, this variable is 0, which means that there is no restriction on checking rows number). The minimum and default values ​​of long_query_time are 0 and 10 (in seconds) respectively. The value can be specified as microseconds (using decimals), but the microsecond unit is only valid for recording to the file. For slow query statements recorded in the table, microseconds are not supported, and the microsecond part is ignored.

By default, the slow query log does not record management statements, nor does it record statements that do not use indexes, but you can use the log_slow_admin_statements and log_queries_not_using_indexes system variables to change the default behavior, so that MySQL Server will also manage statements and statements that do not use indexes. It is included in the slow query log.

The time for the statement in the slow query log to acquire the initial lock is not included in the execution time, including the time range: after the lock is acquired, after the statement is executed, and before the lock is released. Then write the slow query statement into the slow query log. Therefore, the order of records in the slow query log may not be the same as the order of statements (execution order) received by MySQL Server, because some statements may be executed first and all locks are released at the end, and some statements executed first are executed first. Release all locks.

By default, the slow query log is not enabled. To enable it, you can use --slow_query_log =1 to set, to specify the slow query log file name, you can use --slow_query_log_file = file_name to set, to specify the slow query log output destination, you can use --log-output=FILE|TABLE| NONE is set.

  • If the slow query logging function is enabled, but the name is not specified, it is named host_name-slow.log under datadir by default. If the error is reported in the table using --log-output=TABLE, then slow_query_log_file = the path set by file_name invalid.

  • To dynamically modify the slow query log file name, you can use slow_query_log=0 to turn off the slow query log file first, then use slow_query_log_file=new_file_name to specify the new slow query log file name, and then use slow_query_log=1 to re-enable the slow query log log file.

  • If mysqld uses the --log-short-format option at startup, MySQL Server will write less slow query information into the slow query log.

If the log_slow_admin_statements=1 setting is used, MySQL Server will record the following management statements in the slow query log:

  • ALTER TABLE,ANALYZE TABLE,CHECK TABLE,CREATE INDEX,DROP INDEX,OPTIMIZE TABLE和REPAIR TABLE

If the log_queries_not_using_indexes=1 setting is used, MySQL Server will record any query statements that do not use indexes in the slow query log.

  • When recording these query statements, the slow query log may grow rapidly. At this time, you can set the log_throttle_queries_not_using_indexes system variable to limit the rate at which these unused index statements are included in the slow query log (note: this variable limits the number of unused indexes within 60 seconds, not the time limit). By default, this variable is 0, which means there is no rate limit. When the limit is enabled, after the first query that does not use the index is executed, a 60-second time window will be opened, in which other queries that do not use the index will be recorded in the slow query log, and wait after the time window ends , Server records a summary information indicating how many times and the total time spent in these executions. Then enter the next 60-second window.

MySQL Server judges whether the statement needs to be included in the slow query in the following order:

  • Determine whether the parameter log_slow_admin_statements is enabled, if enabled, determine whether the statement is a management statement, if it is, it will be counted as a slow query, and if it is not, it will enter the next round of determination. If the parameter is not enabled, proceed to the next step of judgment.

  • Determine whether the query statement execution time exceeds long_query_time seconds. If it exceeds long_query_time seconds, it will be counted as a slow query. If it does not exceed, it will be judged whether the log_queries_not_using_indexes parameter is enabled. If this parameter is enabled and the statement does not use an index, it will be counted as a slow query, otherwise go to the next step. judgment.

  • If the min_examined_row_limit variable is set to a non-zero value, it is judged whether the number of check lines of the statement exceeds the value set by the variable, if it exceeds, it is counted as a slow query, if it does not exceed, the slow query is not recorded.

The time stamp of slow query log records is controlled by the log_timestamps system variable.

By default, the slave library in the replication architecture will not write the slow query generated by the replay binlog into its own slow query log. If you need to record the slow query statement of the replay binlog from the library and count it in the slow query log, you need to enable it The variable log_slow_slave_statements=1.

The password in the statement written to the slow query log is rewritten by the server and will not appear in plain text. If you need to log the original statement, you need to use the --log-raw option.

Miscellaneous table

02

Since this series does not introduce the audit_log_filter, audit_log_user tables of the enterprise version authentication plug-in, and the firewall_users, firewall_whitelis tables of the firewall plug-in, there is only one server hybrid table that is not enough to start a new issue. All we force into this issue, mainly It is the information used by the federated engine. If you are not interested, you can skip the follow-up content of this issue.

    2.1. servers

This table provides query connection combination information (the remote instance’s IP, port, account, password, database name and other information, see the following examples for details), these connection combination information is usually used in federated engines (of course, it can also be used to save the connection combination in the database One way, maintenance is also more convenient), the information in the table needs to be created using the create server method.

Before introducing the meaning of other fields, let's take a look at the two creation methods of the dedicated engine.

# 使用create server方式创建的连接组合
Syntax:
CREATE SERVER server_name
    FOREIGN DATA WRAPPER wrapper_name
    OPTIONS (option [, option] ...)
option:
  { HOST character-literal
  | DATABASE character-literal
  | USER character-literal
  | PASSWORD character-literal
  | SOCKET character-literal
  | OWNER character-literal
  | PORT numeric-literal }
# 直接使用CONNECTION选项指定完整的连接组合
CONNECTION=scheme://user_name[:password]@host_name[:port_num]/db_name/tbl_name

The following is the information stored in the table.

root@localhost Tue Jun 5 01:12:05 2018 01:12:05 [(none)]>CREATE SERVER fedlink_ip
    -> FOREIGN DATA WRAPPER mysql
    -> OPTIONS (USER 'test',PASSWORD 'test', HOST '127.0.0.1', PORT 3306, DATABASE 'test_table',Owner 'test_table1');
Query OK, 1 row affected (0.00 sec)
root@localhost Tue Jun 5 01:12:10 2018 01:12:10 [(none)]>CREATE SERVER fedlink_socket
    -> FOREIGN DATA WRAPPER mysql
    -> OPTIONS (USER 'test',PASSWORD 'test', SOCKET '/data/mysql/mysql3306/data/mysql.sock', PORT 3306, DATABASE 'test_table',Owner 'test_table2');
Query OK, 1 row affected (0.00 sec)
root@localhost Tue Jun 5 01:12:10 2018 01:12:10 [(none)]>CREATE SERVER fedlink_socket_ip
    -> FOREIGN DATA WRAPPER mysql
    -> OPTIONS (USER 'test',PASSWORD 'test', HOST '127.0.0.1',SOCKET '/data/mysql/mysql3306/data/mysql.sock', PORT 3306, DATABASE 'test_table',Owner 'test_table3');
Query OK, 1 row affected (0.00 sec)
root@localhost Tue Jun 5 01:12:10 2018 01:12:10 [(none)]>select * from mysql.servers;
+-------------------+-----------+------------+----------+----------+------+---------------------------------------+---------+-------------+
| Server_name | Host | Db | Username | Password | Port | Socket | Wrapper | Owner |
+-------------------+-----------+------------+----------+----------+------+---------------------------------------+---------+-------------+
| fedlink_socket_ip | 127.0.0.1 | test_table | test | test | 3306 | /data/mysql/mysql3306/data/mysql.sock | mysql | test_table3 |
| fedlink_socket | | test_table | test | test | 3306 | /data/mysql/mysql3306/data/mysql.sock | mysql | test_table2 |
| fedlink_ip | 127.0.0.1 | test_table | test | test | 3306 | | mysql | test_table1 |
+-------------------+-----------+------------+----------+----------+------+---------------------------------------+---------+-------------+
3 rows in set (0.00 sec)
# 如果要删除连接组合记录,可以使用如下语句
root@localhost Tue Jun 5 01:10:41 2018 01:10:41 [(none)]>drop SERVER fedlink;
Query OK, 1 row affected (0.00 sec)
root@localhost Tue Jun 5 01:11:30 2018 01:11:30 [(none)]>drop SERVER fedlink_socket ;
Query OK, 1 row affected (0.00 sec)
root@localhost Tue Jun 5 01:11:55 2018 01:11:55 [(none)]>drop SERVER fedlink_socket_ip;
Query OK, 1 row affected (0.00 sec)

Examples of two ways to use federated engine to read and write remote instance data.

# 创建远程实例用户
root@localhost Tue Jun 5 00:23:45 2018 00:23:45 [(none)]>grant all on *.* to test@'%' identified by 'test';
Query OK, 0 rows affected (0.00 sec)
# 创建用于存放远程实例表的库
root@localhost Tue Jun 5 00:24:06 2018 00:24:06 [(none)]>create database test_table;
Query OK, 1 row affected (0.00 sec)
root@localhost Tue Jun 5 00:30:50 2018 00:30:50 [(none)]>use test_table
Database changed
# 创建远程实例表test_table1和test_table2
    root@localhost Tue Jun 5 00:31:03 2018 00:31:03 [test_table]>CREATE TABLE test_table1 (
    -> id INT(20) NOT NULL AUTO_INCREMENT,
    -> name VARCHAR(32) NOT NULL DEFAULT '',
    -> other INT(20) NOT NULL DEFAULT '0',
    -> PRIMARY KEY (id),
    -> INDEX name (name),
    -> INDEX other_key (other)
    -> );
Query OK, 0 rows affected (0.06 sec)
root@localhost Tue Jun 5 00:31:09 2018 00:31:09 [test_table]>CREATE TABLE test_table2 (
    -> id INT(20) NOT NULL AUTO_INCREMENT,
    -> name VARCHAR(32) NOT NULL DEFAULT '',
    -> other INT(20) NOT NULL DEFAULT '0',
    -> PRIMARY KEY (id),
    -> INDEX name (name),
    -> INDEX other_key (other)
    -> );
Query OK, 0 rows affected (0.00 sec)
# 创建存放federated引擎表的库
root@localhost Tue Jun 5 00:31:16 2018 00:31:16 [test_table]>create database federated;
Query OK, 1 row affected (0.00 sec)
root@localhost Tue Jun 5 00:31:22 2018 00:31:22 [test_table]>use federated
Database changed
# 使用create server方式创建一个连接字符串组合,该记录会保存到mysql.servers表中
root@localhost Tue Jun 5 00:31:25 2018 00:31:25 [federated]>CREATE SERVER fedlink
    -> FOREIGN DATA WRAPPER mysql
    -> OPTIONS (USER 'test',PASSWORD 'test', HOST '127.0.0.1', PORT 3306, DATABASE 'test_table');
Query OK, 1 row affected (0.03 sec)
# 查看mysql.servers表中的记录
root@localhost Tue Jun 5 00:31:37 2018 00:31:37 [federated]>select * from mysql.servers;
+-------------+-----------+------------+----------+----------+------+--------+---------+-------+
| Server_name | Host | Db | Username | Password | Port | Socket | Wrapper | Owner |
+-------------+-----------+------------+----------+----------+------+--------+---------+-------+
| fedlink | 127.0.0.1 | test_table | test | test | 3306 | | mysql | |
+-------------+-----------+------------+----------+----------+------+--------+---------+-------+
1 row in set (0.00 sec)
# 使用create server连接字符串组合方式,创建federated引擎表
root@localhost Tue Jun 5 00:32:12 2018 00:32:12 [federated]>CREATE TABLE federated1 (
    -> id INT(20) NOT NULL AUTO_INCREMENT,
    -> name VARCHAR(32) NOT NULL DEFAULT '',
    -> other INT(20) NOT NULL DEFAULT '0',
    -> PRIMARY KEY (id),
    -> INDEX name (name),
    -> INDEX other_key (other)
    -> )
    -> ENGINE=FEDERATED
    -> CONNECTION='fedlink/test_table1';
Query OK, 0 rows affected (0.04 sec)
root@localhost Tue Jun 5 00:32:17 2018 00:32:17 [federated]>show create table federated1;
...
| Table | Create Table |
...
| federated1 | CREATE TABLE `federated1` (
  `id` int(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) NOT NULL DEFAULT '',
  `other` int(20) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `name` (`name`),
  KEY `other_key` (`other`)
) ENGINE=FEDERATED DEFAULT CHARSET=utf8 CONNECTION='fedlink/test_table1' |
...
1 row in set (0.00 sec)
# 往federated引擎表federated1中插入数据,然后可以在federated引擎表和远程实例表中都查询到相同的数据
root@localhost Tue Jun 5 00:32:58 2018 00:32:58 [federated]>insert into federated1(name) values('federated1');
Query OK, 1 row affected (0.00 sec)
root@localhost Tue Jun 5 00:33:42 2018 00:33:42 [federated]>select * from federated1;
+----+------------+-------+
| id | name | other |
+----+------------+-------+
| 1 | federated1 | 0 |
+----+------------+-------+
1 row in set (0.00 sec)
root@localhost Tue Jun 5 00:33:49 2018 00:33:49 [federated]>select * from test_table.test_table1;
+----+------------+-------+
| id | name | other |
+----+------------+-------+
| 1 | federated1 | 0 |
+----+------------+-------+
1 row in set (0.00 sec)
# 使用CONNECTION方式完整的连接字符串创建federated引擎表
root@localhost Tue Jun 5 00:32:32 2018 00:32:32 [federated]>CREATE TABLE federated2 (
    -> id INT(20) NOT NULL AUTO_INCREMENT,
    -> name VARCHAR(32) NOT NULL DEFAULT '',
    -> other INT(20) NOT NULL DEFAULT '0',
    -> PRIMARY KEY (id),
    -> INDEX name (name),
    -> INDEX other_key (other)
    -> )
    -> ENGINE=FEDERATED
    -> CONNECTION='mysql://test:[email protected]:3306/test_table/test_table2';
Query OK, 0 rows affected (0.00 sec)
# 往federated引擎表federated2中插入数据,然后可以在federated引擎表和远程实例表中都查询到相同的数据
root@localhost Tue Jun 5 00:34:08 2018 00:34:08 [federated]>insert into federated2(name) values('federated2');
Query OK, 1 row affected (0.00 sec)
root@localhost Tue Jun 5 00:34:16 2018 00:34:16 [federated]>select * from test_table.test_table2;
+----+------------+-------+
| id | name | other |
+----+------------+-------+
| 1 | federated2 | 0 |
+----+------------+-------+
1 row in set (0.00 sec)
root@localhost Tue Jun 5 00:34:22 2018 00:34:22 [federated]>select * from federated2;
+----+------------+-------+
| id | name | other |
+----+------------+-------+
| 1 | federated2 | 0 |
+----+------------+-------+
1 row in set (0.00 sec)
root@localhost Tue Jun 5 00:34:28 2018 00:34:28 [federated]>select * from mysql.servers;
+-------------+-----------+------------+----------+----------+------+--------+---------+-------+
| Server_name | Host | Db | Username | Password | Port | Socket | Wrapper | Owner |
+-------------+-----------+------------+----------+----------+------+--------+---------+-------+
| fedlink | 127.0.0.1 | test_table | test | test | 3306 | | mysql | |
+-------------+-----------+------------+----------+----------+------+--------+---------+-------+
1 row in set (0.00 sec)
# 使用socket方式类似,如果使用socket时,create server连接组合创建方式参照"表记录内容示例"

Table field meaning.

  • Server_name: the unique identifier of the connection combination (name, when using drop server to delete the connection combination record, you can delete the combination record by directly specifying the server_name that exists in the table, such as: drop server server_name;).

  • Host: The remote host name (IP or domain name) in the connection combination, corresponding to the HOST in the create server, and corresponding to the host_name in the CONNECTION connection combination string.

  • Db: The database name of the remote instance in the connection combination, corresponding to DATABASE in the create server, and corresponding to the db_name in the CONNECTION connection combination string.

  • Username: The username of the remote instance of the connection combination, corresponding to USER in the create server, and user_name in the CONNECTION connection combination string.

  • Password: The remote instance user password of the connection combination, corresponding to PASSWORD in the create server, and password in the CONNECTION connection combination string.

  • Port: The remote instance port of the connection combination, corresponding to PORT in the create server, and port_num in the CONNECTION connection combination string.

  • Socket: The socket path of the local instance of the connection combination, which corresponds to the socket in the create server and the host_name in the CONNECTION connection combination string.

  • Wrapper: similar to a protocol name, corresponding to WRAPPER in create server, corresponding to scheme in CONNECTION connection combination string.

PS:

  • The CONNECTION string method will not add records to the mysql.servers table.

The content of this issue is introduced here, and the reference link for the content of this issue is as follows:

https://dev.mysql.com/doc/refman/5.7/en/federated-create-server.html

https://dev.mysql.com/doc/refman/5.7/en/server-logs.html

“ 

"Climbing over this mountain, you can see a piece of sea!". Keep reading our "A Comprehensive Understanding of the MySQL System Library" series of articles to share, and you can learn it systematically. Thank you for reading, we will see you in the next issue!

| About the author

Luo Xiaobo·ScaleFlux Database Technology Expert

One of the authors of "A Thousand Gold Recipes-MySQL Performance Optimization Pyramid Rule", "Data Ecology: MySQL Replication Technology and Production Practice".

Familiar with MySQL architecture, good at overall database tuning, like to specialize in open source technology, and keen on the promotion of open source technology, have done many public database topic sharing online and offline, and published nearly 100 database-related research articles.

The full text is over.

Enjoy MySQL :)

Teacher Ye's "MySQL Core Optimization" class has been upgraded to MySQL 8.0, scan the code to start the journey of MySQL 8.0 practice

Guess you like

Origin blog.csdn.net/n88Lpo/article/details/110600193