MySQL Code Primer Step III – Trick Of Running MySQL Server
As developers, we often need to check some problems or features with various MySQL options, or check them on a few different versions. So we need to change MySQL’s config file often and run many MySQL servers of different versions at the same machines. That is very boring and inconvenient. Fortunately , we have ‘MySQL Test’ platform which can do the boring job for us and make the work simple.
* Running MySQL Test Cases
MySQL test platform and its test cases are in ‘mysql-test’ directory.
cd mysql-test; ./mysql-test-run --force # Run all the test cases ./mtr bug39002 # Run 'bug39002' only, 'mtr' is a symbolic link of 'mysql-test-run'
If some special mysqld options are necessary, you can use ‘–mysqld’ option to set them.
./mtr my_events --mysqld='--event-scheduler=disabled'
* Writing Queries In Test Cases
The easiest way of checking some stuff of MySQL server is to write queries in a test case file and let MySQL test to help us to run a server and execute the queries.
Write following queries in ‘mysql-test/t/my_testcase.test’:
CREATE TABLE t1(int 1); INSERT INTO t1 VALUES(1),(2),(3); SELECT * FROM t1;
Execute the test case
* Setting A Single Server Up
If some stuff can only be done through real clients(Web server, mysql etc.), then we have to setup a server. That can be done by running a test case for a long time. We usually can let the test case sleep. It is a little bit tricky, but very simple.
Write following code in ‘mysql-test/t/my_testcase.test’:
sleep 100000; # sleep the test case 100,000 seconds
Execute the test case
./mtr my_testcase ============================================================================= TEST RESULT TIME (ms) or COMMENT -------------------------------------------------------------------------- worker Using MTR_BUILD_THREAD 300, with reserved ports 13000..13009
From the output, we can know port from 13000 to 13009 are reserved for MySQL servers. The first server is on port 13000 and second server will follow the first one if existing. In this case, there is only one server is set up. Now we can connect to the server from any client. E.g.
client/mysql -u root -P 13000 -h 127.0.0.1
* Running A Server With Binary Log
If you want to setup the server with binary log, You need to write a test case to ‘mysql-test/suite/binlog/t’ directory with below code:
source include/have_log_bin.inc; sleep 100000;
Following include files can be used to replace ‘have_log_bin.inc’ for different binary log format:
source include/have_binlog_format_row.inc; # Use row format source include/have_binlog_format_mixed.inc; # Use mixed format source include/have_binlog_format_statement.inc; # Use statement format
* Running A Few Servers For Replication
If you want to setup a master and a slave, you can add a test case to ‘mysql-test/suite/rpl/t’ directory with following code:
source include/master-slave.inc; source include/have_binlog_format_mixed.inc; sleep 100000;
Following include files can be used to replace ‘have_binlog_format_mixed.inc’ for logging data in ROW or STATEMENT format:
source include/have_binlog_format_row.inc; # Use row format source include/have_binlog_format_statement.inc; # Use statement format
The include file below is an alternative of ‘master-slave.inc’ if you want to setup replication with more complex topologies. E.g. circular replication or cascading replication.
原创文章，转载请注明： 文章地址MySQL Code Primer Step III – Trick Of Running MySQL Server