当前位置:网站首页>Introduction to MySQL system tables

Introduction to MySQL system tables

2022-06-23 07:49:00 hzp666

In the previous issue 《 Copy the information record form | Get to know mysql System libraries 》 in , We introduced in detail mysql Copy information record table in system library , In this issue, we will bring you the eighth in the series 《 Miscellaneous tables such as log records | Get to know mysql System libraries 》, Now please follow us to start mysql System library system learning tour !

Log information record table

01

    1.1. Overview of log information

MySQL The log system contains :general query log、slow query log、error log( Record MySQL Server Startup time 、 Running 、 Stop error message )、binary log( Record MySQL Server Logical log of data changes during operation )、relay log( Record from library IO The main database data change log obtained by the thread from the main database )、DDL log( Record DDL Metadata change information during statement execution .5.7 Only writing to files is supported in ,8.0 Support writing to innodb_ddl_log In the table , Be careful ,ddl log And online ddl Of alter log Different , Don't confuse ), among , stay MySQL 5.7 in , Only general query log、slow query log Support writing to tables ( It also supports writing to files ), Other log types are in MySQL 5.7 Version only supports writing to files , therefore , The following is mainly about the log system table general query log、slow query log surface .

By default , except Windows In addition to the error log on , All logs of other platforms are not enabled by default (DDL Logs are created only when needed , And there are no user configurable options ).

By default , All the logs are written in datadir Under the table of contents , But you can use the path parameters corresponding to each log to change the path .

  • 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 general query log and slow query log Can pass log_output=TABLE Save settings to table mysql.general_log and mysql.slow_log In the table (DDL log stay 8.0 Medium configurable , It can be printed to the error log , It can also be saved in the table innodb_ddl_log in ).

By default ,binary log according to max_binlog_size The size of parameter setting is automatically scrolled 、relay log according to max_relay_log_size perhaps max_binlog_size Auto scroll ( If max_relay_log_size If it's not set, just follow max_binlog_size Size scrolling ), Other log types don't scroll , Always use the same file , So after other log types grow too much , You need to cut yourself .

  • In general use mv file file.bak; Then execute the refresh command , The refresh command can be used by login instance flush logs Command refresh to regenerate a new log file , But the command is to refresh all log types , For specific log types , have access to :flush binary logs; Refresh binary log 、flush error logs; Refresh the error log 、flush general logs; Refresh the normal query log 、flush slow logs; Refresh slow query log 、flush relay logs; Refresh relay log 、flush engine logs; Refresh any refreshable logs related to the storage engine .

  • You can also use Server Of flush tables; Statements or flush table with read lock; sentence .

  • The refresh operation can also be implemented with the options of some command-line tools , for example : Use mysqladmin Ordered flush-logs Options , perhaps mysqldump Of flush-logs Options and --master-data Options .

The log table implementation has the following features :

Usually , The main purpose of log table is to provide an access interface for the program , In order to see Server Internal SQL Operation of the , therefore , It is more convenient to store log records in tables than in disk files , Because it's stored in a table, you can access these log records remotely , You don't need to log in to the operating system to access disk files .

Log table can be used CREATE TABLE,ALTER TABLE and DROP TABLE sentence , But the premise is to use the corresponding switch to turn off the meter , Cannot operate during use ( for example :set global general_log=0, Then operate general_log surface ).

general_log and slow_log The default table is CSV engine , Use comma separated format to store log records ,CSV Data files can be easily imported into other programs for processing , for example :excel The spreadsheet .

The log table can be modified to MyISAM, But you must stop using the table before you modify it . The legal engine is CSV and MyISAM, Other engines don't support .

To disable the logging table for the corresponding DDL Statement operation , You can use the following steps ( Take the slow query table as an example ,slow_log and general_log Table operations are similar to ).

  1. SET @old_log_state = @@ global.general_log;

  2. SET GLOBAL general_log ='OFF';

  3. ALTER TABLE mysql.general_log ENGINE = MyISAM;

  4. SET GLOBAL general_log = @old_log_state;

have access to TRUNCATE TABLE To clear the log .

have access to RENAME TABLE To archive the log table , The old and new tables do an operation of exchanging the names of atoms , as follows :

  1. use mysql;

  2. DROP TABLE IF EXISTS general_log2;

  3. CREATE TABLE general_log2 LIKE general_log;

  4. RENAME TABLE general_log TO general_log_backup,general_log2 TO general_log;

matters needing attention

  • have access to CHECK TABLE sentence .

  • Out of commission LOCK TABLE sentence .

  • Out of commission INSERT,DELETE and UPDATE sentence , Changes to the log table are made by Server Internal maintenance , It can't be operated manually .

  • FLUSH TABLES WITH READ LOCK and read_only The setting of system variables has no effect on the log table .Server Log tables can always be written internally .

  • The data change operation of log table will not record binlog, So it won't be copied to the slave Library .

  • have access to FLUSH TABLES or FLUSH LOGS Statement to refresh the log table or log file .

  • Log table does not support partition table .

  • mysqldump The dump contains statements to recreate these tables , To restore the log table structure after reloading the dump file , But the records in the log table will not be dumped .

PS:MySQL Query log 、 Error logs, etc. are recorded in clear text , therefore , These logs may record the user's clear text password information , have access to rewrite Plug in to record in the original format , For details, see links. :

  • 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. Log sheet details

1.2.1. general_log

This table provides query common SQL Statement execution record information , It is used to find out what the client is executing on the server SQL( Of course , You can also use the enterprise version of audit log Audit plug-in records , This article does not elaborate , Interested children's shoes to study by themselves ).

The information in this table is in SQL It's recorded at the beginning of execution , Instead of waiting SQL Record only at the end of execution .

The following is the information stored in the table .

  1. [email protected] : (none) 07:25:50> set global log_output='TABLE';

  2. Query OK, 0 rows affected (0.00 sec)

  3. [email protected] : (none) 07:26:20> set global general_log=1;

  4. Query OK, 0 rows affected (0.01 sec)

  5. [email protected] : (none) 07:26:32> select * from mysql.general_log;

  6. +----------------------------+---------------------------+-----------+-----------+--------------+---------------------------------+

  7. | event_time | user_host | thread_id | server_id | command_type | argument |

  8. +----------------------------+---------------------------+-----------+-----------+--------------+---------------------------------+

  9. | 2018-06-19 19:26:32.891371 | root[root] @ localhost [] | 3 | 3306102 | Query | show databases |

  10. | 2018-06-19 19:26:42.012064 | root[root] @ localhost [] | 3 | 3306102 | Query | select * from mysql.general_log |

  11. +----------------------------+---------------------------+-----------+-----------+--------------+---------------------------------+

  12. 2 rows in set (0.00 sec)

  13. [email protected] : (none) 07:26:42> select connection_id();

  14. +-----------------+

  15. | connection_id() |

  16. +-----------------+

  17. | 3 |

  18. +-----------------+

  19. 1 row in set (0.00 sec)

Table field meaning .

  • event_time: The moment the query log records to the table log_timestamps System variable value , Used to mark when query log records are put into storage .

  • user_host: Represents the source of the query log record , There are user name and host name information .

  • thread_id: Indicates when the query log record is executed process_id.

  • server_id: Represents the database instance that executed the query ID.

  • command_type: Represents the query command type , It's usually for query.

  • argument: Represents the execution of the query SQL The statement text .

mysqld Write the statements to the query log in the order of receiving requests ( This may be different from the order in which they are executed ).

In the master-slave replication Architecture .

  • When using statement based log format on the main library , After replaying these statements from the library , These statements will be recorded in their own query log ( Query logging needs to be enabled from the library ), Recorded in statement format binlog In the use of mysqlbinlog When the command is parsed and imported into the database , If the instance has enabled the query logging function , These parsing statements will also be recorded in the query log .

  • The main database is based on row Log format , After replaying these data changes from the library , These statements are not included in the query log from the library .

  • Use on the master library based on mixed Log format , If the main library is recorded in statement format , After replaying the data from the library, the statement will be recorded in its own query log ( Query logging needs to be enabled from the library ), If the master database is recording binlog Is converted to row Format , It also follows row The format is copied the same , Replaying these data changes from the library will not record these statements to your query log .

Query logs can use system variables sql_log_off Variable dynamically turns off the query logging function of the current session or all sessions ( And sql_log_bin The function of system variables is similar to ).

Query log switch general_log Variable and query disk log file path general_log_file Variables can be modified dynamically ( If a query log is already open , Then use general_log_file Variable to modify the query log path to close the old query log , Open a new query log ), When query logging is enabled , The query log will be maintained to the system variable log_output Designated destination .

If query logging is enabled , be Server The query log file will be reopened on restart , If there is a log query , Open it again , If the query log does not exist , Then recreate , If you need to Server Archive dynamic query runtime , You can follow the following command (linux perhaps unix platform ).

  1. shell> mv host_name.log host_name-old.log

  2. shell> mysqladmin flush-logs

  3. shell> mv host_name-old.log backup-directory

  4. #  stay Windows On , Please use rename directly , instead of mv command

It can also be in Server At run time, the query log function is first turned off by statements , Then use external commands to archive , Then re enable the query log , So you don't need to use flush-logs Command to refresh the log file , This method can be applied to any platform , The order is as follows :

  1. SET GLOBAL general_log ='OFF';

  2. #  With logging Disabled , Rename the log file from the outside ; for example , From the command line . Then enable logging again :SET GLOBAL general_log ='ON';#  This method can be applied to any platform , There is no need to restart the server .

By default , stay Server If the statement executed in is with the user password , Will be Server Rewrite the statement and write it to the query log , If you need to record clear text passwords , You need to use --low-raw Option to start the Server( Using this option will bypass the password rewriting function ), It is generally not recommended to record the password plaintext information in the query log , Because it's not safe , But if necessary , Judge for yourself ( for example : When you need to query the original statement information to troubleshoot the problem ).

  • If the password in the statement , Specifies that the password is a hash When the value of , The password string will not be rewritten , for example :CREATE USER 'user1'@'localhost' IDENTIFIED BY PASSWORD 'not-so-secret'; It will be recorded for the original reason , But if you remove PASSWORD keyword CREATE USER 'user1'@'localhost' IDENTIFIED BY 'not-so-secret';, In the query log, it will be rewritten as :CREATE USER 'user1'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS ''.

Some grammatical mistakes SQL By default, it will not be recorded in the query log , Use --low-raw Option to start the Server Will record all the original SQL sentence .

The timestamp information in the query log table comes from the system variable log_timestamps( The timestamp in both the slow query log file and the error log file comes from the value of this system variable ), The timestamp value can be used when querying CONVERT_TZ() Function or by setting the session to convert the timestamp information from these tables from the local system time zone to any desired time zone ( Modify session level time_zone A variable's value ).

1.2.2. slow_log

The query execution time of this table exceeds long_query_time Set value's SQL, Or not using the index ( Need to turn on parameters log_queries_not_using_indexes=ON) Or management statements ( Need to turn on parameters log_slow_admin_statements=ON).

The following is the information stored in the table .

  1. [email protected] : test 08:46:04> set global long_query_time=0;

  2. Query OK, 0 rows affected (0.01 sec)

  3. [email protected] : test 08:55:14> set global slow_query_log=1;

  4. Query OK, 0 rows affected (0.01 sec)

  5. #  Disconnect the session and re connect

  6. [email protected] : (none) 08:56:12> use test

  7. Database changed

  8. [email protected] : test 08:56:13> show tables;

  9. +----------------+

  10. | Tables_in_test |

  11. +----------------+

  12. | customer |

  13. | product |

  14. | shares |

  15. | test |

  16. | transreq |

  17. +----------------+

  18. 5 rows in set (0.01 sec)

  19. [email protected] : test 08:56:16> select * from test;

  20. +---+---+------+------+------+------+

  21. | a | b | c | d | e | f |

  22. +---+---+------+------+------+------+

  23. | 1 | 1 | 1 | 1 | 1 | 1 |

  24. | 2 | 2 | 2 | 2 | 2 | 2 |

  25. | 3 | 3 | 3 | 3 | 3 | 3 |

  26. | 4 | 4 | 4 | 4 | 4 | 4 |

  27. | 5 | 5 | 4 | 4 | 5 | 5 |

  28. +---+---+------+------+------+------+

  29. 5 rows in set (0.01 sec)

  30. [email protected] : test 08:56:18> select * from mysql.slow_log;

  31. +----------------------------+---------------------------+-----------------+-----------------+-----------+---------------+------+----------------+-----------+-----------+----------------------------------+-----------+

  32. | start_time | user_host | query_time | lock_time | rows_sent | rows_examined | db | last_insert_id | insert_id | server_id | sql_text | thread_id |

  33. +----------------------------+---------------------------+-----------------+-----------------+-----------+---------------+------+----------------+-----------+-----------+----------------------------------+-----------+

  34. | 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 |

  35. | 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 |

  36. | 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 |

  37. | 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 |

  38. | 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 |

  39. | 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 |

  40. +----------------------------+---------------------------+-----------------+-----------------+-----------+---------------+------+----------------+-----------+-----------+----------------------------------+-----------+

  41. 6 rows in set (0.00 sec)

Table field meaning .

  • start_time: When the slow query log is recorded to the table log_timestamps System variable value .

  • user_host: With user name and host name (IP) Value of format , Used to mark access sources .

  • query_time: Total execution time of slow query statements .

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

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

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

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

  • last_insert_id: Usually it is 0.

  • insert_id: Usually it is 0.

  • server_id: Generating slow query statements server id.

  • sql_text: Slow query log statement text .

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

Slow query logs contain execution times longer than long_query_time The number of seconds set by the system variable SQL sentence , And it includes the need to check that the number of lines exceeds min_examined_row_limit The value set by the system variable SQL sentence ( By default, the variable is 0, It means that there is no limit to the number of check lines ).long_query_time The minimum and default values of are 0 and 10( Unit second ). The value can be specified as microseconds ( Use decimals ), But microsecond units are only valid for recording to a file . For slow query statements recorded in the table , Microseconds are not supported , The microsecond part is ignored .

By default , Slow query logs do not record management statements , It also does not record statements that do not use indexes , But you can use log_slow_admin_statements and log_queries_not_using_indexes System variables change the default behavior , send MySQL Server Add management statements and statements that do not use indexes into the slow query log .

In slow query log, the time when the statement obtains the initial lock is not included in the execution time , The inclusive time range is : After getting the lock , And after the statement is executed , Before releasing the lock . Then write the slow query statement into the slow query log . therefore , The order recorded in the slow query log may be the same as MySQL Server The order of statements received ( Execution order ) Is not the same , Because there may be statements that execute first and then release all locks , Some post execution statements release all locks first .

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

  • If slow query logging is enabled , But no name was specified , The default is datadir The next name is host_name-slow.log, If you use --log-output=TABLE Set error report in the table , be slow_query_log_file = file_name Invalid path set .

  • To dynamically modify the slow query log file name , have access to slow_query_log=0 Close the slow query log file first , And then use slow_query_log_file=new_file_name Specify a new slow query log file name , And then use slow_query_log=1 Re enable slow query log files .

  • If mysqld In the start-up, it uses --log-short-format Options , be MySQL Server Slow queries will be written to the log .

If used log_slow_admin_statements=1 Set up , be MySQL Server The following management statements will be recorded in the slow query log :

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

If used log_queries_not_using_indexes=1 Set up , be MySQL Server Any query statements that do not use an index are recorded in the slow query log .

  • When you record these queries , Slow query logs can grow rapidly . At this time, you can set log_throttle_queries_not_using_indexes The system variable is used to limit the rate at which these non indexed statements are counted into the slow query log ( Be careful : This variable limits 60 The number of unused index statements per second , It's not a time limit ). By default , This variable is 0, There is no rate limit . When restrictions are enabled , After the first query without an index is executed , Will open a 60 Second time window , In this window , Log queries that do not use other indexes to slow query logs , Wait for the time window to end ,Server Record a summary message , Indicates how many times and the total time spent on these execution times . And then go to the next 60 Second window .

MySQL Server Use the following order to determine whether statements need to be included in slow queries :

  • Judgment parameters log_slow_admin_statements Is it enabled? , If enabled , Then judge whether the statement is a management statement , If it is Slow queries are included , If it is not, it will enter the next round of judgment . If the parameter is not enabled , Then go to the next step to judge .

  • Judge whether the execution time of query statement exceeds long_query_time second , If it exceeds, it will be counted as slow query , If not more than , Then judge log_queries_not_using_indexes Whether the parameter is enabled , If this parameter is enabled and the statement does not use an index , Slow queries are included , Otherwise, go to the next step of judgment .

  • If min_examined_row_limit Variable set non-zero value , Then judge whether the number of check lines of the statement exceeds the value set by the variable , If it exceeds, it will be counted as slow query , If not, slow queries are not recorded .

The timestamp of the slow query log record is made by log_timestamps System variable control .

By default , The slave Library in the replication schema will not be replayed binlog The generated slow query is written into its own slow query log , If records need to be replayed from the library binlog The slow query statement is included in the slow query log , Variables need to be enabled log_slow_slave_statements=1.

The password in the statement written to the slow query log is rewritten by the server , It doesn't appear in plain text . If you need to record the original statement , Need to use --log-raw Options .

Hybrid table

02

Because this series does not introduce the enterprise version authentication plug-in audit_log_filter, audit_log_user surface 、 Firewall plug-in firewall_users, firewall_whitelis surface , So there's only one left servers There's not enough space in the hybrid table to start another issue , So we're forcing it into this issue , Mainly federated Information used by the engine , If you are not interested, you can skip the following contents of this issue .

    2.1. servers

This table provides query join combination information ( Remote instance IP、 port 、 Account number 、 password 、 Database name and other information , See the following example ), This connection combination information is usually used for federated engine ( Of course, it can also be used as a way to save the connection combination in the database , Maintenance is also more convenient ), The information in this table needs to use create server Way to create .

Before introducing the meaning of other fields , Have a look first dederated Two ways to create engine .

  1. #  Use create server Method to create a combination of connections

  2. Syntax:

  3. CREATE SERVER server_name

  4.     FOREIGN DATA WRAPPER wrapper_name

  5.     OPTIONS (option [, option] ...)

  6. option:

  7.   { HOST character-literal

  8.   | DATABASE character-literal

  9.   | USER character-literal

  10.   | PASSWORD character-literal

  11.   | SOCKET character-literal

  12.   | OWNER character-literal

  13.   | PORT numeric-literal }

  14. #  Use it directly CONNECTION Option to specify the complete connection combination

  15. CONNECTION=scheme://user_name[:password]@host_name[:port_num]/db_name/tbl_name

The following is the information stored in the table .

  1. [email protected] Tue Jun 5 01:12:05 2018 01:12:05 [(none)]>CREATE SERVER fedlink_ip

  2.     -> FOREIGN DATA WRAPPER mysql

  3.     -> OPTIONS (USER 'test',PASSWORD 'test', HOST '127.0.0.1', PORT 3306, DATABASE 'test_table',Owner 'test_table1');

  4. Query OK, 1 row affected (0.00 sec)

  5. [email protected] Tue Jun 5 01:12:10 2018 01:12:10 [(none)]>CREATE SERVER fedlink_socket

  6.     -> FOREIGN DATA WRAPPER mysql

  7.     -> OPTIONS (USER 'test',PASSWORD 'test', SOCKET '/data/mysql/mysql3306/data/mysql.sock', PORT 3306, DATABASE 'test_table',Owner 'test_table2');

  8. Query OK, 1 row affected (0.00 sec)

  9. [email protected] Tue Jun 5 01:12:10 2018 01:12:10 [(none)]>CREATE SERVER fedlink_socket_ip

  10.     -> FOREIGN DATA WRAPPER mysql

  11.     -> 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');

  12. Query OK, 1 row affected (0.00 sec)

  13. [email protected] Tue Jun 5 01:12:10 2018 01:12:10 [(none)]>select * from mysql.servers;

  14. +-------------------+-----------+------------+----------+----------+------+---------------------------------------+---------+-------------+

  15. | Server_name | Host | Db | Username | Password | Port | Socket | Wrapper | Owner |

  16. +-------------------+-----------+------------+----------+----------+------+---------------------------------------+---------+-------------+

  17. | fedlink_socket_ip | 127.0.0.1 | test_table | test | test | 3306 | /data/mysql/mysql3306/data/mysql.sock | mysql | test_table3 |

  18. | fedlink_socket | | test_table | test | test | 3306 | /data/mysql/mysql3306/data/mysql.sock | mysql | test_table2 |

  19. | fedlink_ip | 127.0.0.1 | test_table | test | test | 3306 | | mysql | test_table1 |

  20. +-------------------+-----------+------------+----------+----------+------+---------------------------------------+---------+-------------+

  21. 3 rows in set (0.00 sec)

  22. #  If you want to combine records , You can use the following statement

  23. [email protected] Tue Jun 5 01:10:41 2018 01:10:41 [(none)]>drop SERVER fedlink;

  24. Query OK, 1 row affected (0.00 sec)

  25. [email protected] Tue Jun 5 01:11:30 2018 01:11:30 [(none)]>drop SERVER fedlink_socket ;

  26. Query OK, 1 row affected (0.00 sec)

  27. [email protected] Tue Jun 5 01:11:55 2018 01:11:55 [(none)]>drop SERVER fedlink_socket_ip;

  28. Query OK, 1 row affected (0.00 sec)

federated Two ways to use the engine to read and write remote instance data examples .

  1. #  Create remote instance users

  2. [email protected] Tue Jun 5 00:23:45 2018 00:23:45 [(none)]>grant all on *.* to [email protected]'%' identified by 'test';

  3. Query OK, 0 rows affected (0.00 sec)

  4. #  Create a library for storing remote instance tables

  5. [email protected] Tue Jun 5 00:24:06 2018 00:24:06 [(none)]>create database test_table;

  6. Query OK, 1 row affected (0.00 sec)

  7. [email protected] Tue Jun 5 00:30:50 2018 00:30:50 [(none)]>use test_table

  8. Database changed

  9. #  Create remote instance table test_table1 and test_table2

  10.     [email protected] Tue Jun 5 00:31:03 2018 00:31:03 [test_table]>CREATE TABLE test_table1 (

  11.     -> id INT(20) NOT NULL AUTO_INCREMENT,

  12.     -> name VARCHAR(32) NOT NULL DEFAULT '',

  13.     -> other INT(20) NOT NULL DEFAULT '0',

  14.     -> PRIMARY KEY (id),

  15.     -> INDEX name (name),

  16.     -> INDEX other_key (other)

  17.     -> );

  18. Query OK, 0 rows affected (0.06 sec)

  19. [email protected] Tue Jun 5 00:31:09 2018 00:31:09 [test_table]>CREATE TABLE test_table2 (

  20.     -> id INT(20) NOT NULL AUTO_INCREMENT,

  21.     -> name VARCHAR(32) NOT NULL DEFAULT '',

  22.     -> other INT(20) NOT NULL DEFAULT '0',

  23.     -> PRIMARY KEY (id),

  24.     -> INDEX name (name),

  25.     -> INDEX other_key (other)

  26.     -> );

  27. Query OK, 0 rows affected (0.00 sec)

  28. #  Create and store federated Library of engine tables

  29. [email protected] Tue Jun 5 00:31:16 2018 00:31:16 [test_table]>create database federated;

  30. Query OK, 1 row affected (0.00 sec)

  31. [email protected] Tue Jun 5 00:31:22 2018 00:31:22 [test_table]>use federated

  32. Database changed

  33. #  Use create server Method to create a connection string combination , The record will be saved to mysql.servers In the table

  34. [email protected] Tue Jun 5 00:31:25 2018 00:31:25 [federated]>CREATE SERVER fedlink

  35.     -> FOREIGN DATA WRAPPER mysql

  36.     -> OPTIONS (USER 'test',PASSWORD 'test', HOST '127.0.0.1', PORT 3306, DATABASE 'test_table');

  37. Query OK, 1 row affected (0.03 sec)

  38. #  see mysql.servers The records in the table

  39. [email protected] Tue Jun 5 00:31:37 2018 00:31:37 [federated]>select * from mysql.servers;

  40. +-------------+-----------+------------+----------+----------+------+--------+---------+-------+

  41. | Server_name | Host | Db | Username | Password | Port | Socket | Wrapper | Owner |

  42. +-------------+-----------+------------+----------+----------+------+--------+---------+-------+

  43. | fedlink | 127.0.0.1 | test_table | test | test | 3306 | | mysql | |

  44. +-------------+-----------+------------+----------+----------+------+--------+---------+-------+

  45. 1 row in set (0.00 sec)

  46. #  Use create server Connection string combination mode , establish federated Engine watch

  47. [email protected] Tue Jun 5 00:32:12 2018 00:32:12 [federated]>CREATE TABLE federated1 (

  48.     -> id INT(20) NOT NULL AUTO_INCREMENT,

  49.     -> name VARCHAR(32) NOT NULL DEFAULT '',

  50.     -> other INT(20) NOT NULL DEFAULT '0',

  51.     -> PRIMARY KEY (id),

  52.     -> INDEX name (name),

  53.     -> INDEX other_key (other)

  54.     -> )

  55.     -> ENGINE=FEDERATED

  56.     -> CONNECTION='fedlink/test_table1';

  57. Query OK, 0 rows affected (0.04 sec)

  58. [email protected] Tue Jun 5 00:32:17 2018 00:32:17 [federated]>show create table federated1;

  59. ...

  60. | Table | Create Table |

  61. ...

  62. | federated1 | CREATE TABLE `federated1` (

  63.   `id` int(20) NOT NULL AUTO_INCREMENT,

  64.   `name` varchar(32) NOT NULL DEFAULT '',

  65.   `other` int(20) NOT NULL DEFAULT '0',

  66.   PRIMARY KEY (`id`),

  67.   KEY `name` (`name`),

  68.   KEY `other_key` (`other`)

  69. ) ENGINE=FEDERATED DEFAULT CHARSET=utf8 CONNECTION='fedlink/test_table1' |

  70. ...

  71. 1 row in set (0.00 sec)

  72. #  Go to federated Engine watch federated1 Insert data , Then you can go to federated The same data is found in both the engine table and the remote instance table

  73. [email protected] Tue Jun 5 00:32:58 2018 00:32:58 [federated]>insert into federated1(name) values('federated1');

  74. Query OK, 1 row affected (0.00 sec)

  75. [email protected] Tue Jun 5 00:33:42 2018 00:33:42 [federated]>select * from federated1;

  76. +----+------------+-------+

  77. | id | name | other |

  78. +----+------------+-------+

  79. | 1 | federated1 | 0 |

  80. +----+------------+-------+

  81. 1 row in set (0.00 sec)

  82. [email protected] Tue Jun 5 00:33:49 2018 00:33:49 [federated]>select * from test_table.test_table1;

  83. +----+------------+-------+

  84. | id | name | other |

  85. +----+------------+-------+

  86. | 1 | federated1 | 0 |

  87. +----+------------+-------+

  88. 1 row in set (0.00 sec)

  89. #  Use CONNECTION Method complete connection string creation federated Engine watch

  90. [email protected] Tue Jun 5 00:32:32 2018 00:32:32 [federated]>CREATE TABLE federated2 (

  91.     -> id INT(20) NOT NULL AUTO_INCREMENT,

  92.     -> name VARCHAR(32) NOT NULL DEFAULT '',

  93.     -> other INT(20) NOT NULL DEFAULT '0',

  94.     -> PRIMARY KEY (id),

  95.     -> INDEX name (name),

  96.     -> INDEX other_key (other)

  97.     -> )

  98.     -> ENGINE=FEDERATED

  99.     -> CONNECTION='mysql://test:[email protected]:3306/test_table/test_table2';

  100. Query OK, 0 rows affected (0.00 sec)

  101. #  Go to federated Engine watch federated2 Insert data , Then you can go to federated The same data is found in both the engine table and the remote instance table

  102. [email protected] Tue Jun 5 00:34:08 2018 00:34:08 [federated]>insert into federated2(name) values('federated2');

  103. Query OK, 1 row affected (0.00 sec)

  104. [email protected] Tue Jun 5 00:34:16 2018 00:34:16 [federated]>select * from test_table.test_table2;

  105. +----+------------+-------+

  106. | id | name | other |

  107. +----+------------+-------+

  108. | 1 | federated2 | 0 |

  109. +----+------------+-------+

  110. 1 row in set (0.00 sec)

  111. [email protected] Tue Jun 5 00:34:22 2018 00:34:22 [federated]>select * from federated2;

  112. +----+------------+-------+

  113. | id | name | other |

  114. +----+------------+-------+

  115. | 1 | federated2 | 0 |

  116. +----+------------+-------+

  117. 1 row in set (0.00 sec)

  118. [email protected] Tue Jun 5 00:34:28 2018 00:34:28 [federated]>select * from mysql.servers;

  119. +-------------+-----------+------------+----------+----------+------+--------+---------+-------+

  120. | Server_name | Host | Db | Username | Password | Port | Socket | Wrapper | Owner |

  121. +-------------+-----------+------------+----------+----------+------+--------+---------+-------+

  122. | fedlink | 127.0.0.1 | test_table | test | test | 3306 | | mysql | |

  123. +-------------+-----------+------------+----------+----------+------+--------+---------+-------+

  124. 1 row in set (0.00 sec)

  125. #  Use socket Similar way , If you use socket when ,create server Connection combination creation method reference " Table record content example "

Table field meaning .

  • Server_name: Connection combination unique identifier ( That is the name , Use drop server When deleting a connection combination record , Directly specify the... Existing in the table server_name You can delete the combined records , Such as :drop server server_name;).

  • Host: Remote host name in connection composition (IP Or domain name ), Corresponding create server Medium HOST, Corresponding CONNECTION Concatenate... In a composite string host_name.

  • Db: The database name of the remote instance in the connection composition , Corresponding create server Medium DATABASE , Corresponding CONNECTION Concatenate... In a composite string db_name.

  • Username: Combination of remote user names for instance , Corresponding create server Medium USER , Corresponding CONNECTION Concatenate... In a composite string user_name.

  • Password: Remote instance user password for connection combination , Corresponding create server Medium PASSWORD , Corresponding CONNECTION Concatenate... In a composite string password.

  • Port: Connect the combined remote instance port , Corresponding create server Medium PORT , Corresponding CONNECTION Concatenate... In a composite string port_num.

  • Socket: Connect the local instance of the composition socket route , Corresponding create server Medium SOCKET , Corresponding CONNECTION Concatenate... In a composite string host_name.

  • Wrapper: It's like a protocol name , Corresponding create server Medium WRAPPER , Corresponding CONNECTION Concatenate... In a composite string scheme.

PS:

  • CONNECTION String mode will not be in mysql.servers Add records to the table .

So far as this issue is concerned , The links to this issue are 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

“ 

" Over this mountain , You can see a sea !". Keep reading our " Get to know mysql System libraries " Series article sharing , You can learn it systematically . Thanks for reading , We'll see you next time !

| Author's brief introduction

Luoxiaobo ·ScaleFlux Database technology experts

《 A golden prescription ——MySQL Performance optimization pyramid rule 》、《 Data Ecology :MySQL Replication technology and production practice 》 One of the authors .

be familiar with MySQL Architecture , Good at overall database tuning , I like studying open source technology , And keen on the promotion of open source technology , Online and offline have done many public database special sharing , Published recently 100 Database related research articles .

The full text after .

Enjoy MySQL :)

原网站

版权声明
本文为[hzp666]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230711200421.html