58
MySQL Administration Workshop Authors: Trifon M. Anguelov Creation Date: September 17, 2004 Last Update date: October 6, 2004 Version: 1.1 MySQL Administration Workshop

MySQL Training Course Workshop1

Embed Size (px)

Citation preview

MySQL Administration Workshop

Authors: Trifon M. Anguelov

Creation Date: September 17, 2004

Last Update date: October 6, 2004

Version: 1.1

MySQL Administration Workshop

1

2.1 How to install mysql_server and mysql_client on FreeBSDmachine

We are using the a-int.corp.yahoo.com test machine

2.1.1 mysql_server installation

<trifon@wine>/homes/trifon $ ssh [email protected]'s password:

Last login: Mon Sep 20 14:54:34 2004 from wine.corp.yahoo.Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994The Regents of the University of California. All rights reserved.

<trifon@a-int>/home/trifon $

The installation of yinst packages has to be done as “root” OS user:

$ sudo su -

The following command installs the MySQL server (mysql_server). Since noversion is specified, the yinst will pick us the latest stable version:

# yinst install -same -live mysql_server

yinst: Identifying packages for installation ...yinst: Found: mysql_server-4.0.22_1yinst: WARNING: Installing over an active package: mysql_server-4.0.22_1yinst: Checking prerequisites ... [OK]yinst: /home/y:yinst: S mysql_server-4.0.22_1yinst: Fetching mysql_server-4.0.22_1 ... [OK]yinst: mysql_server-4.0.22_1 (running install (PRE-INSTALL) commands)checking for mysql user... OKchecking for mysql group... OKyinst: mysql_server-4.0.22_1: installing ...yinst: mysql_server-4.0.22_1 (running install (POST-INSTALL) commands)yinst: mysql_server-4.0.22_1: install pending ...

MySQL Administration Workshop

2

yinst: mysql_server-4.0.22_1: stopping ...yinst: mysql_server-4.0.22_1 (running deactivate (DEACTIVATE) commands)yinst: mysql_server-4.0.22_1: deactivating ... [OK]yinst: mysql_server-4.0.22_1 (running deactivate (POST-DEACTIVATE)commands)checking for /etc/my.cnf symlink... OKremoving /etc/my.cnf symlink... OKyinst: mysql_server-4.0.22_1: removing ... [OK]yinst: mysql_server-4.0.22_1: install completedyinst: mysql_server-4.0.22_1 (running activate (ACTIVATE) commands)yinst: mysql_server-4.0.22_1: activating ...yinst: mysql_server-4.0.22_1 (running activate (POST-ACTIVATE)commands)checking for /etc/my.cnf symlink... MISSINGcreating /etc/my.cnf symlink... LINKED OKchecking for already instllalled db files... OKyinst: mysql_server-4.0.22_1 (running post-activate commands)yinst: mysql_server-4.0.22_1: activatedyinst: mysql_server-4.0.22_1: starting ...

2.1.2 Mysql_client installation

For the MySQL client there is again already packaged yinst package and itcould be installed with the followign command:

# yinst install mysql_client

yinst: Identifying packages for installation ...yinst: Found: mysql_client-4.0.22_1yinst: Checking prerequisites ... [OK]yinst: /home/y:yinst: U mysql_client-4.0.22_1yinst: Fetching mysql_client-4.0.22_1 ... [OK]yinst: mysql_client-4.0.22_1: installing ...yinst: mysql_client-4.0.22_1: install pending ...yinst: mysql_server-4.0.22_1: stopping ...yinst: mysql_client-4.0.13_2: deactivating ... [OK]yinst: mysql_client-4.0.22_1: install completedyinst: mysql_client-4.0.22_1: activating ... [OK]yinst: mysql_server-4.0.22_1: starting ...

MySQL Administration Workshop

3

2.2 MySQL datafiles location

2.2.1 MySQL binary files

The binary files installed from yinst are located in /home/y/bin directory:

<root@a-int>/home/y/logs/mysql # ls -la | grep mysql

-r-xr-xr-x 2 root wheel 187160 Sep 14 17:15 mysql-r-xr-xr-x 2 root wheel 4967 Sep 14 17:15 mysql_config-r-xr-xr-x 2 root wheel 2980 Sep 14 17:15 mysql_convert_table_format-r-xr-xr-x 2 root wheel 10422 Sep 14 17:15 mysql_explain_log-r-xr-xr-x 2 root wheel 2657 Sep 14 17:15 mysql_find_rows-r-xr-xr-x 2 root wheel 486 Sep 14 17:15 mysql_fix_extensions-r-xr-xr-x 2 root wheel 10317 Sep 14 17:15 mysql_fix_privilege_tables-r-xr-xr-x 2 root wheel 14985 Sep 14 17:15 mysql_install_db-r-xr-xr-x 2 root wheel 6308 Sep 14 17:15 mysql_secure_installation-r-xr-xr-x 2 root wheel 126 Sep 14 16:47 mysql_server_ssl_conf_fix-r-xr-xr-x 2 root wheel 725 Aug 10 17:02 mysql_server_ymon_setup-r-xr-xr-x 2 root wheel 17278 Sep 14 17:15 mysql_setpermission-r-xr-xr-x 2 root wheel 13650 Sep 14 17:15 mysql_tableinfo-r-xr-xr-x 2 root wheel 3126 Sep 14 17:15 mysql_zap-r-xr-xr-x 2 root wheel 111525 Sep 14 17:15 mysqlaccess-r-xr-xr-x 2 root wheel 28234 Sep 14 17:15 mysqladmin-r-xr-xr-x 2 root wheel 55264 Sep 14 17:15 mysqlbinlog-r-xr-xr-x 2 root wheel 10529 Sep 14 17:15 mysqlbug-r-xr-xr-x 2 root wheel 21601 Sep 14 17:15 mysqlcheck-r-xr-xr-x 2 root wheel 20102 Sep 14 17:15 mysqld_multi-r-xr-xr-x 2 root wheel 10890 Sep 14 17:15 mysqld_safe-r-xr-xr-x 2 root wheel 38501 Sep 14 17:15 mysqldump-r-xr-xr-x 2 root wheel 4989 Sep 14 17:15 mysqldumpslow-r-xr-xr-x 2 root wheel 32280 Sep 14 17:15 mysqlhotcopy-r-xr-xr-x 2 root wheel 17407 Sep 14 17:15 mysqlimport-r-xr-xr-x 2 root wheel 21775 Sep 14 17:15 mysqlshow

You can list the complete set of options for certain MySQL binary file byusing the –help or -? options with the command. For example:

# mysql --help

# mysqladmin -?

MySQL Administration Workshop

4

2.2.1.1 mysql – MySQL client text interface

This binary is similar to the Oracle “sqlplus” binary and it's the text basedinterface (MySQL client) to the MySQL database server.

Usage: mysql [OPTIONS] [database]

-D, --database=.. Database to use -e, --execute=... Execute command and quit.(Output like with --batch) -h, --host=... Connect to host -p[password] --password[=...]

Password to use when connecting to serverIf password is not given it's asked from the tty.

-P --port=... Port number to use for connection -S --socket=... Socket file to use for connection -t --table Output in table format -u, --user=# User for login if not current user -E, --vertical Print the output of a query (rows) vertically

Some examples of the options usage are:

Connect as MySQL user “root” on port “3306” to MySQL database“TESTDB” :

# mysql -u root -P 3306 TESTDB

Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 24 to server version: 4.0.22-Yahoo-SMP-log

Type 'help' for help.

mysql>

MySQL Administration Workshop

5

The default port on which MySQL daemon (mysqld) listens is 3306. To checkthis verify if the port 3306 is already included in the /etc/services file andcheck the open ports on the local host:

# cat /etc/services | grep 3306

mysql 3306/tcp #MySQL

# netstat -al | grep LISTEN

tcp4 0 0 *.mysql *.* LISTENtcp4 0 0 *.http *.* LISTENtcp4 0 0 *.6372 *.* LISTENtcp4 0 0 *.6370 *.* LISTENtcp4 0 0 *.6369 *.* LISTENtcp4 0 0 *.5253 *.* LISTENtcp4 0 0 *.bpcd *.* LISTENtcp4 0 0 *.ssh *.* LISTEN

mysql -u root -P 3306 -e "show status" TESTDB

+----------------------------------------+------------+ | Variable_name | Value |+----------------------------------------+-------------+ | Aborted_clients | 0 | | Aborted_connects | 2 | | Bytes_received | 1563 | | Bytes_sent | 33316 | | Com_admin_commands | 1 |......

MySQL Administration Workshop

6

2.2.1.2 mysqlshow - Shows the structure of a MySQL database(databases,tables and columns)

Usage: mysqlshow [OPTIONS] [database [table [column]]]

-#, --debug=... output debug log. Often this is 'd:t:o,filename` -?, --help display this help and exit -C, --compress Use compression in server/client protocol -h, --host=... connect to host -i, --status Shows a lot of extra information about each table -k, --keys show keys for table -p, --password[=...] password to use when connecting to server

If password is not given it's asked from the tty. -P --port=... Port number to use for connection -S --socket=... Socket file to use for connection -u, --user=# user for login if not current user -V, --version output version information and exit

Show all existing MySQL databases on the local host:

# mysqlshow

+--------------------+| Databases |+--------------------+| TESTDB || YAHOOGST || mysql || mysql_bkp || test || test1 |+--------------------+

Show all tables information in a single MySQL database:

# mysqlshow -i TESTDB

MySQL Administration Workshop

7

Database: TESTDB+------+--------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+----------------+---------+| Name | Type | Row_format | Rows | Avg_row_length | Data_length | Max_data_length |Index_length | Data_free | Auto_increment | Create_time | Update_time |Check_time | Create_options | Comment |+------+--------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+----------------+---------+| test | MyISAM | Dynamic | 3 | 20 | 60 | 4294967295 | 1024 | 0 || 2004-09-16 18:00:44 | 2004-09-16 18:07:17 | | | |+------+--------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+----------------+---------+

mysqlshow can also be used to display the index information. Let's create anindex on our “test” table in the “TESTDB” MySQL database:

# mysql -u root TESTDB

Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 60 to server version: 4.0.22-Yahoo-SMP-log

Type 'help' for help.

mysql> show tables;

+-----------------------------+ | Tables_in_TESTDB |+-----------------------------+ | test |+-----------------------------+

1 row in set (0.00 sec)

MySQL Administration Workshop

8

mysql> desc test;

+---------------+-----------+--------+------+-------------+---------+| Field | Type | Null | Key | Default | Extra |+---------------+-----------+--------+------+-------------+---------+| id | int(11) | YES | | NULL | || first_name | text | YES | | NULL | |+---------------+-----------+--------+------+-------------+---------+

2 rows in set (0.00 sec)

mysql> create index test_pk on test (id);

Query OK, 3 rows affected (0.09 sec)Records: 3 Duplicates: 0 Warnings: 0

mysql> show index from test;

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality |Sub_part | Packed | Null | Index_type | Comment |+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+| test | 1 | test_pk | 1 | id | A | NULL | NULL | NULL | YES |BTREE | |+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

1 row in set (0.00 sec)

Show table's index information:

# mysqlshow -k TESTDB test

Database: TESTDB Table: test Rows: 3+------------+---------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+------------+---------+------+-----+---------+-------+| id | int(11) | YES | MUL | | || first_name | text | YES | | | |+------------+---------+------+-----+---------+-------+

MySQL Administration Workshop

9

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality |Sub_part | Packed | Null | Index_type | Comment |+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+| test | 1 | test_pk | 1 | id | A | | | | YES | BTREE ||+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

2.2.1.3 mysqldump - Dumps definition and data MySQL database ortable

This MySQL binary is similar to the Oracle “exp” binary.

# mysqldump -? for options list

Usage: mysqldump [OPTIONS] database [tables]

-a, --all Include all MySQL specific create options -#, --debug=... Output debug log. Often this is 'd:t:o,filename` -?, --help Display this help message and exit. -c, --complete-insert Use complete insert statements. -C, --compress Use compression in server/client protocol -e, --extended-insert Allows utilization of the new, much faster

INSERT syntax --add-drop-table Add a 'drop table' before each create --add-locks Add locks around insert statements --allow-keywords Allow creation of column names that are keywords --delayed-insert Insert rows with INSERT DELAYED -F --flush-logs Flush logs file in server before starting dump -f, --force Continue even if we get an sql-error. -h, --host=... Connect to host. -l, --lock-tables Lock all tables for read. -t, --no-create-info Don't write table creation info. -d, --no-data No row information. -O, --set-variable var=option

give a variable a value. --help lists variables --opt Same as --add-drop-table --add-locks --all

--extended-insert --quick --lock-tables

MySQL Administration Workshop

10

-p, --password[=...] Password to use when connecting to server. If password is not given it's solicited on the tty.

-P, --port=... Port number to use for connection. -q, --quick Don't buffer query, dump directly to stdout. -Q, --quote-names Quote table and column names with ` -S, --socket=... Socket file to use for connection. -T, --tab=... Creates tab separated textfile for each table to

given path. (creates .sql and .txt files). NOTE: This only works if mysqldump is run on the same machine as the mysqld daemon.

-u, --user=# User for login if not current user. -v, --verbose Print info about the various stages. -V, --version Output version information and exit. -w, --where= dump only selected records; QUOTES mandatory!

Export MySQL database “TESTDB” structure only and use user login for“root”:

<root@a-int> # mysqldump -u root -d TESTDB

# MySQL dump 7.1## Host: localhost Database: TESTDB#--------------------------------------------------------# Server version 4.0.22-Yahoo-SMP-log

## Table structure for table 'test'#CREATE TABLE test ( id int(11), first_name text);

Export MySQL database “TESTDB” data only and use user login for “root”:

MySQL Administration Workshop

11

<root@a-int> # mysqldump -u root -t TESTDB# MySQL dump 7.1## Host: localhost Database: TESTDB#--------------------------------------------------------# Server version 4.0.22-Yahoo-SMP-log

## Dumping data for table 'test'#

INSERT INTO test VALUES (1,'TEST1');INSERT INTO test VALUES (2,'TEST2');INSERT INTO test VALUES (3,'TEST3');

Export MySQL database “TESTDB” structure and data, while using userlogin for “root”:

<root@a-int> # mysqldump -u root TESTDB

# MySQL dump 7.1## Host: localhost Database: TESTDB#--------------------------------------------------------# Server version 4.0.22-Yahoo-SMP-log

## Table structure for table 'test'#CREATE TABLE test ( id int(11), first_name text);

## Dumping data for table 'test'#

INSERT INTO test VALUES (1,'TEST1');INSERT INTO test VALUES (2,'TEST2');INSERT INTO test VALUES (3,'TEST3');

MySQL Administration Workshop

12

We will create one more table in the “TESTDB” database to demonstrate thetable level export:

mysql> create table test1 (id integer, salary integer, departmenttext);

Query OK, 0 rows affected (0.01 sec)

mysql> desc test1;

+------------------+-----------+-------+-----+------------+--------+ | Field | Type | Null | Key | Default | Extra |+------------------+-----------+-------+-----+-------------+--------+ | id | int(11) | YES | | NULL | | | salary | int(11) | YES | | NULL | | | department | text | YES | | NULL | |+------------------+-----------+--------+-----+------------+--------+

3 rows in set (0.00 sec)

Export the structure and data of table “test” from MySQL database“TESTDB”, while using user login for “root”:

<root@a-int> # mysqldump -u root TESTDB test

# MySQL dump 7.1## Host: localhost Database: TESTDB#--------------------------------------------------------# Server version 4.0.22-Yahoo-SMP-log

## Table structure for table 'test'#CREATE TABLE test ( id int(11), first_name text, KEY test_pk (id));

MySQL Administration Workshop

13

## Dumping data for table 'test'#

INSERT INTO test VALUES (1,'TEST1');INSERT INTO test VALUES (2,'TEST2');INSERT INTO test VALUES (3,'TEST3');

We can also export a list of tables.

Export the structure and data of tables “test” and “test1” from MySQLdatabase “TESTDB”, while using user login for “root”:

<root@a-int> # mysqldump -u root TESTDB test test1

# MySQL dump 7.1## Host: localhost Database: TESTDB#--------------------------------------------------------# Server version 4.0.22-Yahoo-SMP-log

## Table structure for table 'test'#CREATE TABLE test ( id int(11), first_name text, KEY test_pk (id));

## Dumping data for table 'test'#

INSERT INTO test VALUES (1,'TEST1');INSERT INTO test VALUES (2,'TEST2');INSERT INTO test VALUES (3,'TEST3');

MySQL Administration Workshop

14

## Table structure for table 'test1'#CREATE TABLE test1 ( id int(11), salary int(11), department text);

## Dumping data for table 'test1'#

The most common use of mysqldump is probably for making a backup ofentire databases. In this case instead of spooling the mysqldump commandoutput on the screen, we will save it into a file. This is similar to the Oracle“exp” utility where DBA can take a full backup of a single database:

# mysqldump --opt TESTDB > backup-file.sql

You can import (load) the dump file back into the MySQL server with:

# mysql TESTDB < backup-file.sql

Or:

# mysql -e "source /path-to-backup/backup-file.sql" TESTDB

mysqldump is also very useful for populating databases by copying datafrom one MySQL server to another:

# mysqldump --opt TESTDB | mysql --host=remote_host -C TESTDB1

MySQL Administration Workshop

15

It is possible to dump several databases with one command:

# mysqldump --databases TESTDB [test...] > my_databases.sql

If you want to dump all databases, use the --all-databases option:

# mysqldump --all-databases > all_databases.sql

2.2.1.4 mysqlimport – Loads ables from text files in various formats

This MySQL binary is similar to the Oracle “imp” utility.

The base name of the text file must be the name of the table that should beused. If one uses sockets to connect to the MySQL server, the server willopen and read the text file directly. In other cases the client will open thetext file.

# mysqlimport -? for options list

Usage: mysqlimport [OPTIONS] database textfile...

-#, --debug[=...] Output debug log. Often this is 'd:t:o,filename` -?, --help Displays this help and exits. -C, --compress Use compression in server/client protocol -d, --delete Deletes first all rows from table. -f, --force Continue even if we get an sql-error. -h, --host=... Connect to host. -i, --ignore If duplicate unique key was found, keep old row. -l, --lock-tables Lock all tables for write. -L, --local Read all files through the client -p, --password[=...] Password to use when connecting to server.

If password is not given it's asked from the tty. -P, --port=... Port number to use for connection. -r, --replace If duplicate unique key was found, replace old row. -s, --silent Be more silent. -S, --socket=... Socket file to use for connection. -u, --user=# User for login if not current user. -v, --verbose Print info about the various stages.

MySQL Administration Workshop

16

-V, --version Output version information and exit. --fields-terminated-by=... Fields in the textfile are terminated by ... --fields-enclosed-by=... Fields in the importfile are enclosed by ... --fields-optionally-enclosed-by=... Fields in the i.file are opt. enclosed by ... --fields-escaped-by=... Fields in the i.file are escaped by ... --lines-terminated-by=... Lines in the i.file are terminated by ...

We have created a test import file to import into “TESTDB” MySQLdatabase, table “test1”

# cat /tmp/test1.dat

100 55000 10101 65000 12102 45000 20103 75000 25

Check if there is any data in th e”test1” table:

# mysql -u root -e "select * from test1;" TESTDB#

Import the data from /tmp/test1.dat file into the “test1” table in “TESTDB”MySQL database:

# mysqlimport -u root TESTDB /tmp/test1.dat

TESTDB.test1: Records: 4 Deleted: 0 Skipped: 0 Warnings: 12

Check the imported data:

# mysql -u root -e "select * from test1;" TESTDB

MySQL Administration Workshop

17

+-------+-----------+-------------------+ | id | salary | department |+-------+-----------+-------------------+ | 100 | NULL | NULL | | 101 | NULL | NULL | | 102 | NULL | NULL | | 103 | NULL | NULL |+-------+------------+------------------+

# mysqlimport -u root TESTDB /tmp/test1.dat

TESTDB.test1: Records: 4 Deleted: 0 Skipped: 0 Warnings: 12

# mysql -u root -e "select * from test1;" TESTDB

+-------+-----------+------------------+ | id | salary | department |+-------+-----------+------------------+ | 100 | NULL | NULL | | 101 | NULL | NULL | | 102 | NULL | NULL | | 103 | NULL | NULL | | 100 | NULL | NULL | | 101 | NULL | NULL | | 102 | NULL | NULL | | 103 | NULL | NULL |+-------+-----------+------------------+

We can also truncate the base table before importing the data:

# mysqlimport -d -u root TESTDB /tmp/test1.dat

TESTDB.test1: Records: 4 Deleted: 0 Skipped: 0 Warnings: 12

# mysql -u root -e "select * from test1;" TESTDB

MySQL Administration Workshop

18

+-------+-----------+-------------------+ | id | salary | department |+-------+-----------+-------------------+ | 100 | NULL | NULL | | 101 | NULL | NULL | | 102 | NULL | NULL | | 103 | NULL | NULL |+-------+------------+------------------+

2.2.2 MySQL data files

By default the MySQL data files are located in /home/y/var/mysql/datadirectory:

# ls -la /home/y/var/mysql/data/TESTDB

-rw-rw---- 1 mysql users 60 Sep 21 11:18 test.MYD-rw-rw---- 1 mysql users 2048 Sep 21 11:18 test.MYI-rw-rw---- 1 mysql users 8586 Sep 21 11:18 test.frm-rw-rw---- 1 mysql users 80 Sep 21 11:40 test1.MYD-rw-rw---- 1 mysql users 1024 Sep 21 11:57 test1.MYI-rw-rw---- 1 mysql users 8614 Sep 21 11:23 test1.frm

Every time new MySQL database is created, a sub-directory is created underthis root directory:

If we create a new MySQL database:

# mysqladmin -u root create TESTDB1

Database "TESTDB1" created.

We can see the newly created TESTDB1 sub-directory:

# ls -la /home/y/var/mysql/data

drwx------ 2 mysql users 512 Sep 21 11:23 TESTDBdrwx------ 2 mysql users 512 Sep 21 11:55 TESTDB1

MySQL Administration Workshop

19

Once the database is dropped, the sub-directory and the data files areremoved automatically:

# mysqladmin -u root drop TESTDB1

Dropping the database is potentially a very bad thing to do.Any data stored in the database will be destroyed.

Do you really want to drop the 'TESTDB1' database [y/N]yDatabase "TESTDB1" dropped

# ls -la /home/y/var/mysql/data

drwx------ 2 mysql users 512 Sep 21 11:23 TESTDB

2.2.3 MySQL log files

MySQL logs by default are located under /home/y/logs/mysql directory:

# ls -la /home/y/logs/mysql/

-rw-r--r-- 1 mysql users 666 Sep 16 15:32 mysql.log-rw-rw---- 1 mysql users 4621 Sep 21 11:58 mysqld.err

The MySQL startup/shutdown and error messages are recorded in thesefiles:

# cat /home/y/logs/mysql/mysqld.err

040921 11:57:37 InnoDB: Starting shutdown...040921 11:57:40 InnoDB: Shutdown completed040921 11:57:40 /home/y/libexec/mysqld: Shutdown Complete

040921 11:57:40 mysqld ended040921 11:58:23 mysqld started

MySQL Administration Workshop

20

040921 11:58:28 InnoDB: Started040921 11:58:28 Can't open shared library 'udf_example.so' (errno: 0 /home/y/lib/udf_example.so: Undefined symbol "default_charset_in)040921 11:58:28 Can't open shared library 'udf_example.so' (errno: 0 /home/y/lib/udf_example.so: Undefined symbol "default_charset_in)040921 11:58:28 Can't open shared library 'udf_example.so' (errno: 0 /home/y/lib/udf_example.so: Undefined symbol "default_charset_in)040921 11:58:28 Can't open shared library 'udf_example.so' (errno: 0 /home/y/lib/udf_example.so: Undefined symbol "default_charset_in)/home/y/libexec/mysqld: ready for connections.Version: '4.0.22-Yahoo-SMP-log' socket: '/tmp/mysql.sock' port: 3306Yahoo SMP

# ls -la /home/y/logs/mysql/

-rw-r--r-- 1 mysql users 666 Sep 16 15:32 mysql.log-rw-rw---- 1 mysql users 4621 Sep 21 11:58 mysqld.err

Shutdown the MySQL server (daemon):

# yinst stop mysql_server

yinst: mysql_server-4.0.22_1: stopping ...

Startup the MySQL server (daemon):

# yinst start mysql_server

yinst: mysql_server-4.0.22_1: starting ...

# ls -la /home/y/logs/mysql/

-rw-r--r-- 1 mysql users 727 Sep 21 12:02 mysql.log-rw-rw---- 1 mysql users 5592 Sep 21 12:02 mysqld.err

MySQL Administration Workshop

21

# cat /home/y/logs/mysql/mysql.log

...

...Killing mysqld with pid 51345Wait for mysqld to exit.. done

2.3 MySQL status check, runtime and variables information

2.3.1 MySQL status check

There are many ways to check the MySQL database status

2.3.1.1 mysqladmin

mysqladmin is similar to functionality to Oracle's “svrmgrl” and could beused for MySQL administration tasks as create or drop database, flush logs,flush tables from the cache, flush privileges, ping and shutdown the MySQLserver.

Usage: mysqladmin [OPTIONS] command command....

-#, --debug=... Output debug log. Often this is 'd:t:o,filename` -f, --force Don't ask for confirmation on drop database; with

multiple commands, continue even if an error occurs -?, --help Display this help and exit -C, --compress Use compression in server/client protocol -h, --host=# Connect to host -p, --password[=...] Password to use when connecting to server

If password is not given it's asked from the tty -P --port=... Port number to use for connection -i, --sleep=sec Execute commands again and again with a sleep between -r, --relative Show difference between current and previous values when used with -i. Currently works only

MySQL Administration Workshop

22

with extended-status -s, --silent Silently exit if one can't connect to server -S, --socket=... Socket file to use for connection -t, --timeout=... Timeout for connection to the mysqld server -u, --user=# User for login if not current user -V, --version Output version information and exit -w, --wait[=retries] Wait and retry if connection is down

If MySQL servers is started and accepts connections the mysqladmincommand will have the following output:

# mysqladmin status

Uptime: 418890 Threads: 1 Questions: 766 Slow queries: 0 Opens: 156Flush tables: 1 Open tables: 130 Queries per second avg: 0.002

If MySQL server is not running – the output looks like:

# mysqladmin status

mysqladmin: connect to server at 'localhost' failederror: 'Can't connect to local MySQL server through socket'/tmp/mysql.sock' (2)'Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!

To query the MySQL server version, one ca use the next command:

# mysqladmin version

mysqladmin Ver 8.0 Distrib 3.22.32, for -freebsd4.1 on i386TCX Datakonsult AB, by Monty

Server version 4.0.22-Yahoo-SMP-logProtocol version 10Connection Localhost via UNIX socketUNIX socket /tmp/mysql.sockUptime: 4 days 20 hours 21 min 38 sec

MySQL Administration Workshop

23

Threads: 1 Questions: 767 Slow queries: 0 Opens: 156 Flush tables: 1Open tables: 130 Queries per second avg: 0.002

For a quick check the MySQL server status “ping” option will do a ping andreturn back the result to the STDOUT device:

# mysqladmin ping

mysqld is alive

If the MySQL server is not running the result of the ping will be the same asfrom the “mysqladmin status” command:

First we will shutdown MySQL daemon:

# mysqladmin shutdown

and check the status via the “ping” option:

# mysqladmin ping

mysqladmin: connect to server at 'localhost' failederror: 'Can't connect to local MySQL server through socket'/tmp/mysql.sock' (2)'Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!

To start back the MySQL server, you can use the yinst command option“start”:

# yinst start mysql_serveryinst: mysql_server-4.0.22_1: starting ...

and now the “mysqladmin ping” succeeds:

# mysqladmin ping

mysqld is alive

MySQL Administration Workshop

24

2.3.1.2 mysql

If MySQL servers is started and accepts connections the mysqladmincommand will have the following output:

# mysql -u root mysql

Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2 to server version: 4.0.22-Yahoo-SMP-log

Type 'help' for help.

mysql> status;

mysql Ver 12.22 Distrib 4.0.22, for unknown-freebsd4.3 (i386)

Connection id: 2Current database: mysqlCurrent user: root@localhostSSL: Not in useCurrent pager: lessUsing outfile: ''Server version: 4.0.22-Yahoo-SMP-logProtocol version: 10Connection: Localhost via UNIX socketClient characterset: latin1Server characterset: latin1UNIX socket: /tmp/mysql.sockUptime: 3 min 43 sec

Threads: 1 Questions: 20 Slow queries: 0 Opens: 12 Flush tables: 1 Open tables: 6 Queries per second avg: 0.090

MySQL Administration Workshop

25

If MySQL server is not running – the output looks like: # mysql -u root TESTDB

ERROR 2002: Can't connect to local MySQL server through socket'/tmp/mysql.sock' (2)

2.3.1.3 OS command “ps”

When MySQL server is started, there is the “mysqld_safe” process and the“mysqld” processesrunning:

# ps -auxw | grep mysql

mysql 53684 0.0 0.1 648 252 p0 I 12:02PM 0:00.03 /bin/sh /home/y/bin/mysqld_safe --datadir=/home/y/var/mysql/data --pid-file=/home/y/var/mysql/mysqld.pid

mysql 53785 0.0 10.9 110284 28308 p0 SN 12:02PM 0:00.42 /home/y/libexec/mysqld --basedir=/home/y –datadir=/home/y/var/mysql/data

mysql 53786 0.0 10.9 110284 28308 p0 SN 12:02PM 0:00.00 /home/y/libexec/mysqld --basedir=/home/y –datadir=/home/y/var/mysql/data

mysql 53787 0.0 10.9 110284 28308 p0 IN 12:02PM 0:00.00 /home/y/libexec/mysqld --basedir=/home/y –datadir=/home/y/var/mysql/data

mysql 53788 0.0 10.9 110284 28308 p0 IN 12:02PM 0:00.00 /home/y/libexec/mysqld --basedir=/home/y –datadir=/home/y/var/mysql/data

mysql 53789 0.0 10.9 110284 28308 p0 IN 12:02PM 0:00.03 /home/y/libexec/mysqld --basedir=/home/y –datadir=/home/y/var/mysql/data

mysql 53790 0.0 10.9 110284 28308 p0 IN 12:02PM 0:00.00 /home/y/libexec/mysqld --basedir=/home/y –datadir=/home/y/var/mysql/data

mysql 53805 0.0 10.9 110284 28308 p0 SN 12:02PM 0:00.27 /home/y/libexec/mysqld --basedir=/home/y –datadir=/home/y/var/mysql/data

mysql 53806 0.0 10.9 110284 28308 p0 SN 12:02PM 0:00.29 /home/y/libexec/mysqld --basedir=/home/y –datadir=/home/y/var/mysql/data

MySQL Administration Workshop

26

mysql 53807 0.0 10.9 110284 28308 p0 IN 12:02PM 0:00.00 /home/y/libexec/mysqld --basedir=/home/y –datadir=/home/y/var/mysql/data

mysql 53808 0.0 10.9 110284 28308 p0 IN 12:02PM 0:00.00 /home/y/libexec/mysqld --basedir=/home/y –datadir=/home/y/var/mysql/data

There is also the process ID file (mysqld.pid) created for the mysqld in the /home/y/var/mysql/ directory:

# ls -la /home/y/var/mysql/mysqld.pid

-rw-rw---- 1 mysql mysql 6 Sep 21 17:17 /home/y/var/mysql/mysqld.pid

If the MySQL server is not running, then this file is not present:

# yinst stop mysql_server

yinst: mysql_server-4.0.22_1: stopping ...

# ls -la /home/y/var/mysql/mysqld.pid

ls: /home/y/var/mysql/mysqld.pid: No such file or directory

# yinst start mysql_server

yinst: mysql_server-4.0.22_1: starting ...

2.3.2 MySQL runtime information

The runtime statistics about the MySQL server could be reviewed with thefollowing command:

MySQL Administration Workshop

27

# mysql -u root -e "show status" TESTDB

+----------------------------------------+-------------+ | Variable_name | Value |+----------------------------------------+-------------+| Aborted_clients | 0 || Aborted_connects | 0 || Bytes_received | 595 || Bytes_sent | 10679 || Com_admin_commands | 1 || Com_alter_table | 0 || Com_analyze | 0 || Com_backup_table | 0 || Com_begin | 0 | ...............

| Table_locks_immediate | 6 || Table_locks_waited | 0 || Threads_cached | 0 || Threads_created | 13 || Threads_connected | 1 || Threads_running | 1 || Uptime | 491 |+-----------------------------------------+------------+

The MySQL server “uptime” information:

# mysql -u root -e "show status like 'Uptime'" TESTDB

+-----------------------+---------+ | Variable_name | Value |+-----------------------+---------+ | Uptime | 343 |+-----------------------+---------+

MySQL Administration Workshop

28

The MySQL server “threads” information:

# mysql -u root -e "show status like 'Threads%'" TESTDB

+------------------------------+--------+ | Variable_name | Value |+------------------------------+--------+ | Threads_cached | 0 | | Threads_created | 7 | | Threads_connected | 1 | | Threads_running | 1 |+-------------------------------+--------+

# mysql -u root -e "show status like 'Connections'" TESTDB

+-----------------------+---------+ | Variable_name | Value |+-----------------------+---------+ | Connections | 9 |+-----------------------+---------+

2.3.3 MySQL variables information

The current values of the MySQL server can be listed with the followingcommand:

# mysql -u root -e "show variables" TESTDB

The long list can also be made more specific with the following predicate:

# mysql -u root -e "show variables like 'version%'" TESTDB

+-----------------------------+-----------------------------------+ | Variable_name | Value |+-----------------------------+-----------------------------------+ | version | 4.0.22-Yahoo-SMP-log | | version_comment | Yahoo SMP | | version_compile_os | unknown-freebsd4.3 |+------------------------------+----------------------------------+

MySQL Administration Workshop

29

Since MySQL version 4.0.x you can change the MySQL variable at run time,without restarting the MySQL deamon:

mysql> show variables like 'net%';

+----------------------------+--------------+ | Variable_name | Value |+----------------------------+--------------+ | net_buffer_length | 8192 | | net_read_timeout | 30 | | net_retry_count | 1000000 | | net_write_timeout | 60 |+----------------------------+--------------+

4 rows in set (0.01 sec)

mysql> set net_write_timeout=3600;

Query OK, 0 rows affected (0.05 sec)

mysql> show variables like 'net%';

+---------------------------+---------------+ | Variable_name | Value |+----------------------------+--------------+ | net_buffer_length | 8192 | | net_read_timeout | 30 | | net_retry_count | 1000000 | | net_write_timeout | 3600 |+----------------------------+--------------+

4 rows in set (0.01 sec)

MySQL Administration Workshop

30

5.1 MySQL master – slave replication setup

The following example outlines the steps required to setup and configure theMySQL master-slave replication. In this example we will use these twoFreeBSD machines:

a-int.corp.yahoo.com - MySQL master (database “TESTDB”)p-int.corp.yahoo.com - MySQL slave (database “TESTDB”)

master

a-int 12:03:20 ~ $ mysql -u root TESTDB

mysql> show tables;+----------------------------+| Tables_in_TESTDB |+----------------------------+| test1 || test2 |+----------------------------+2 rows in set (0.00 sec)

5.1.1 Enable the MySQL replication on the master machines

Edit the /etc/my.cnf file and make these changes:

## Uncomment these if this server will be a master.

log-binserver-id = 1

5.1.2 Restart the MySQL server

a-int 12:05:16 ~ $ yinst stop mysql_server

yinst: mysql_server-4.0.22_1: stopping ...

MySQL Administration Workshop

31

a-int 12:05:35 ~ $ yinst start mysql_server

yinst: mysql_server-4.0.22_1: starting ...

5.1.3 Check the binary logs generation

After we enabled the binary logs generation in the /etc/my.cnf file (log-binline), the binary logs used in the replication should be generated. In our testcase this was already enabled, so we have older binary log files, but still thea-int-bin.010 was generated after the MySQL server restart

a-int 12:05:41 ~ $ ls -la /home/y/var/mysql/data

-rw-rw---- 1 mysql users 929 Sep 16 11:16 a-int-bin.001-rw-rw---- 1 mysql users 311 Sep 16 15:14 a-int-bin.002-rw-rw---- 1 mysql users 11582 Sep 21 11:56 a-int-bin.003-rw-rw---- 1 mysql users 79 Sep 21 11:58 a-int-bin.004-rw-rw---- 1 mysql users 79 Sep 21 12:02 a-int-bin.005-rw-rw---- 1 mysql users 79 Sep 21 17:18 a-int-bin.006-rw-rw---- 1 mysql users 79 Sep 21 17:33 a-int-bin.007-rw-rw---- 1 mysql users 1563 Sep 27 17:44 a-int-bin.008-rw-rw---- 1 mysql users 4 Sep 27 17:44 a-int-bin.009-rw-rw---- 1 mysql users 79 Oct 6 12:06 a-int-bin.010-rw-rw---- 1 mysql users 160 Oct 6 12:06 a-int-bin.index

List of all existing binary logs (after the binary logs purge) are kept in the *-bin.index file. In our case this file has the following entries:

a-int 12:06:19 ~ $ cat /home/y/var/mysql/data/a-int-bin.index

./a-int-bin.001

./a-int-bin.002

./a-int-bin.003

./a-int-bin.004

./a-int-bin.005

./a-int-bin.006

./a-int-bin.007

./a-int-bin.008

./a-int-bin.009

./a-int-bin.010

MySQL Administration Workshop

32

5.1.4 Take backup of the master database

We use the mysqldump utility to take a hot backup of the MySQL database“TESTDB”

# mysqldump -u root --add-locks --extended-insert --flush-logs --lock-tables --quick TESTDB > /var/tmp/TESTDB-backup.sql

a-int 12:09:14 ~ $ ls -la /var/tmp

-rw-r--r-- 1 root wheel 492 Oct 6 12:09 TESTDB-backup.sql

The backup file generated from mysqldump containt the SQL statementsto create the “TESTDB” tables and the INSERT INTO ... statements for allrows in these tables

a-int 12:09:17 ~ $ cat /var/tmp/TESTDB-backup.sql

# MySQL dump 7.1## Host: localhost Database: TESTDB#--------------------------------------------------------# Server version 4.0.22-Yahoo-SMP-log

## Table structure for table 'test1'#CREATE TABLE test1 ( id int(11), salary int(11), department text);

## Dumping data for table 'test1'#

LOCK TABLES test1 WRITE;INSERT INTO test1 VALUES (100,NULL,NULL),(101,NULL,NULL),(102,NULL,NULL),(103,NULL,NULL);UNLOCK TABLES;

MySQL Administration Workshop

33

## Table structure for table 'test2'#CREATE TABLE test2 ( id int(11), salary int(11), department text);

## Dumping data for table 'test2'#

LOCK TABLES test2 WRITE;INSERT INTO test2 VALUES (100,NULL,NULL),(101,NULL,NULL),(102,NULL,NULL),(103,NULL,NULL);UNLOCK TABLES;

5.1.5 Check the newly created binary log after the backup

The mysqldump command generates a new binary log after its success,which will be the beginning of the slave replication. All the data in themaster MySQL database “TESTDB” was captured in the backup file and allthe changes to the master database will be recorded in this (and thefollowing) binary log(s):

a-int 12:09:48 ~ $ ls -la /home/y/var/mysql/data

-rw-rw---- 1 mysql users 929 Sep 16 11:16 a-int-bin.001-rw-rw---- 1 mysql users 311 Sep 16 15:14 a-int-bin.002-rw-rw---- 1 mysql users 11582 Sep 21 11:56 a-int-bin.003-rw-rw---- 1 mysql users 79 Sep 21 11:58 a-int-bin.004-rw-rw---- 1 mysql users 79 Sep 21 12:02 a-int-bin.005-rw-rw---- 1 mysql users 79 Sep 21 17:18 a-int-bin.006-rw-rw---- 1 mysql users 79 Sep 21 17:33 a-int-bin.007-rw-rw---- 1 mysql users 1563 Sep 27 17:44 a-int-bin.008-rw-rw---- 1 mysql users 4 Sep 27 17:44 a-int-bin.009-rw-rw---- 1 mysql users 119 Oct 6 12:09 a-int-bin.010-rw-rw---- 1 mysql users 4 Oct 6 12:09 a-int-bin.011-rw-rw---- 1 mysql users 176 Oct 6 12:09 a-int-bin.index

MySQL Administration Workshop

34

5.1.6 Copy the MySQL master database “TESTDB” backup from themaster to the slave machines

a-int 12:10:24 ~ $ scp /var/tmp/TESTDB-backup.sql trifon@p-int:/var/tmp

trifon@p-int's password:

TESTDB-backup.sql 100% | ******************************| 492 00:00

5.1.7 Create slave replication account

mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.*TO repl@"216.145.53.174" IDENTIFIED BY 'repl';

Query OK, 0 rows affected (0.00 sec)

mysql> show grants for 'repl'@'216.145.53.174';

+------------------------------------------------------------------------------------------------------------------------------------------+| Grants for [email protected] |+------------------------------------------------------------------------------------------------------------------------------------------+| GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl'@'216.145.53.174'IDENTIFIED BY PASSWORD '5ec3db8f603fcb03' |+------------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

slave

5.1.8 Create the slave “TESTDB” which will host the replicateddatabase from the master machine

We will use the same database name, but this could be any database name

<root@p-int>/root # mysqladmin create TESTDB

MySQL Administration Workshop

35

See the list of the existing MySQL databases:

<root@p-int>/root # mysqlshow -u root

+---------------+| Databases |+---------------+| TESTDB || mysql || test |+---------------+

<root@p-int>/root # ls -la /var/tmp

-rw-r--r-- 1 trifon wheel 492 Oct 6 12:11 TESTDB-backup.sql

5.1.9 Import the master database backup

<root@p-int>/root # mysql -u root TESTDB < /var/tmp/TESTDB-backup.sql

Check the master database backup restore into the slave database

<root@p-int>/root # mysql -u root -e "show tables" TESTDB

+----------------------------+| Tables_in_TESTDB |+----------------------------+| test1 || test2 |+----------------------------+

MySQL Administration Workshop

36

<root@p-int>/root # mysql -u root -e "select count(*) from test1;"TESTDB

+------------+| count(*) |+------------+| 4 |+------------+

<root@p-int>/root # mysql -u root -e "select count(*) from test2;"TESTDB

+------------+| count(*) |+------------+| 4 |+------------+

5.1.10 Enable the MySQL slave replication

To do that we have edit the /etc/my.cnf file and add the MySQL masterdatabase information

## Uncomment these if this server will be a master.

#log-binserver-id = 2

## Replication

master-host = 216.145.53.174master-user = replmaster-password = replmaster-port = 3306

5.1.11 Restart the MySQL slave database server

<root@p-int>/root # yinst stop mysql_server

MySQL Administration Workshop

37

yinst: mysql_server-4.0.17: stopping ...

<root@p-int>/root # yinst start mysql_server

yinst: mysql_server-4.0.17: starting ...

5.1.12 Check the MySQL slave database server log

To confirm that the MySQL slave replication is running, we have to checkthe error log after the mysqld restart. If the replication is running, we willsee the appropriate messages. The same is valid in case there is an error

<root@p-int>/root # cat /home/y/logs/mysql/mysqld.err

/home/y/logs/mysql/mysqld.err log 041006 14:24:48 /home/y/libexec/mysqld: Normal shutdown

041006 14:24:48 InnoDB: Starting shutdown...041006 14:24:50 InnoDB: Shutdown completed041006 14:24:50 /home/y/libexec/mysqld: Shutdown Complete

041006 14:24:50 mysqld ended

041006 14:24:54 mysqld startedWarning: Ignoring user change to 'mysql' because the user was set to'mysql' earlier on the command line041006 14:24:54 InnoDB: Started/home/y/libexec/mysqld: ready for connections.Version: '4.0.17-Yahoo-SMP' socket: '/tmp/mysql.sock' port: 3306041006 14:24:54 Slave I/O thread: connected to master '[email protected]:3306', replication started in log 'FIRST' at position 4

ERROR: 1146 Table 'test.t1' doesn't exist041006 14:24:54 Slave: Error 'Table 'test.t1' doesn't exist' on query'insert into t1 values (10,'t''set')'. Default database: 'test', Error_code: 1146041006 14:24:54 Error running query, slave SQL thread aborted. Fixthe problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'a-int-bin.001' position 79

MySQL Administration Workshop

38

As shown above the replication encountered an error. Few points from theabove errors:

– the replication started from a-int-bin.001binary log and position 79– the “test.t1” table is the reason for the failure

However our slave replication should start from a-int-bin.011 binary logand position 4 as mentioned before

To review the binary logs (which are binary files and not readable withstandard text editor) we can use the mysqlbinlog utility

a-int 13:39:38 /home/y/bin $ ./mysqlbinlog /home/y/var/mysql/data/a-int-bin.011

/*!40019 SET @@session.max_insert_delayed_threads=0*/;# at 4#041006 13:03:09 server id 1 log_pos 4 Query thread_id=2exec_time=0 error_code=0use mysql;SET TIMESTAMP=1097092989;GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TOrepl@"216.145.53.145/255.255.255.0" IDENTIFIED BY 'repl';# at 150#041006 13:05:03 server id 1 log_pos 150 Query thread_id=2

exec_time=0 error_code=0SET TIMESTAMP=1097093103;GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TOrepl@"216.145.53.145/255.255.255.0" IDENTIFIED BY 'repl';# at 296#041006 13:06:32 server id 1 log_pos 296 Query thread_id=2

exec_time=0 error_code=0SET TIMESTAMP=1097093192;GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TOrepl@"216.145.53.145" IDENTIFIED BY 'repl';# at 428#041006 13:16:27 server id 1 log_pos 428 Rotate to a-int-bin.012pos: 4

MySQL Administration Workshop

39

master

To view which is the current binary log and its position, we can use theSHOW MASTER STATUS command

mysql> show master status;

+---------------+----------+--------------+------------------+| File | Position | Binlog_do_db | Binlog_ignore_db |+---------------+----------+--------------+------------------+| a-int-bin.014 | 741 | | |+---------------+----------+--------------+------------------+1 row in set (0.00 sec)

The current binary log in the a-int-bin.014 and its position is 741

slave

To view the slave replication status we can use the SHOW SLAVE STATUScommand

mysql> show slave status\G

*************************** 1. row *************************** Master_Host: a-int.corp.yahoo.com Master_User: repl Master_Port: 3306 Connect_retry: 60 Master_Log_File: a-int-bin.014 Read_Master_Log_Pos: 741 Relay_Log_File: p-int-relay-bin.022 Relay_Log_Pos: 119Relay_Master_Log_File: a-int-bin.001 Slave_IO_Running: Yes Slave_SQL_Running: No Replicate_do_db: Replicate_ignore_db: Last_errno: 1146 Last_error: Error 'Table 'test.t1' doesn't exist' on query 'insertinto t1 values (10,'t''set')'. Default database: 'test' Exec_master_log_pos: 79 Relay_log_space: 18288

MySQL Administration Workshop

40

the current binary logs on the master machine

a-int 14:29:18 /home/y/bin $ ls -la /home/y/var/mysql/data/

-rw-rw---- 1 mysql users 929 Sep 16 11:16 a-int-bin.001-rw-rw---- 1 mysql users 311 Sep 16 15:14 a-int-bin.002-rw-rw---- 1 mysql users 11582 Sep 21 11:56 a-int-bin.003-rw-rw---- 1 mysql users 79 Sep 21 11:58 a-int-bin.004-rw-rw---- 1 mysql users 79 Sep 21 12:02 a-int-bin.005-rw-rw---- 1 mysql users 79 Sep 21 17:18 a-int-bin.006-rw-rw---- 1 mysql users 79 Sep 21 17:33 a-int-bin.007-rw-rw---- 1 mysql users 1563 Sep 27 17:44 a-int-bin.008-rw-rw---- 1 mysql users 4 Sep 27 17:44 a-int-bin.009-rw-rw---- 1 mysql users 119 Oct 6 12:09 a-int-bin.010-rw-rw---- 1 mysql users 468 Oct 6 13:16 a-int-bin.011-rw-rw---- 1 mysql users 666 Oct 6 13:36 a-int-bin.012-rw-rw---- 1 mysql users 1081 Oct 6 14:07 a-int-bin.013-rw-rw---- 1 mysql users 741 Oct 6 14:24 a-int-bin.014-rw-rw---- 1 mysql users 224 Oct 6 14:11 a-int-bin.index

a-int 14:29:06 /home/y/bin $ ./mysqlbinlog /home/y/var/mysql/data/a-int-bin.011

/*!40019 SET @@session.max_insert_delayed_threads=0*/;# at 4#041006 13:03:09 server id 1 log_pos 4 Query thread_id=2exec_time=0 error_code=0use mysql;SET TIMESTAMP=1097092989;GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TOrepl@"216.145.53.145/255.255.255.0" IDENTIFIED BY 'repl';# at 150#041006 13:05:03 server id 1 log_pos 150 Query thread_id=2

exec_time=0 error_code=0SET TIMESTAMP=1097093103;GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TOrepl@"216.145.53.145/255.255.255.0" IDENTIFIED BY 'repl';# at 296#041006 13:06:32 server id 1 log_pos 296 Query thread_id=2

exec_time=0 error_code=0

MySQL Administration Workshop

41

SET TIMESTAMP=1097093192;GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TOrepl@"216.145.53.145" IDENTIFIED BY 'repl';# at 428#041006 13:16:27 server id 1 log_pos 428 Rotate to a-int-bin.012pos: 4

a-int 14:29:12 /home/y/bin $ ./mysqlbinlog /home/y/var/mysql/data/a-int-bin.012

/*!40019 SET @@session.max_insert_delayed_threads=0*/;# at 4#041006 13:24:25 server id 1 log_pos 4 Query thread_id=7exec_time=0 error_code=0use mysql;SET TIMESTAMP=1097094265;GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TOrepl@"216.145.53.145" IDENTIFIED BY 'repl';# at 136#041006 13:25:04 server id 1 log_pos 136 Query thread_id=7

exec_time=0 error_code=0SET TIMESTAMP=1097094304;GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TOrepl@"216.145.53.174" IDENTIFIED BY 'repl';# at 268#041006 13:32:31 server id 1 log_pos 268 Query thread_id=17

exec_time=0 error_code=0SET TIMESTAMP=1097094751;GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TOrepl@"216.145.53.174" IDENTIFIED BY 'repl';# at 400#041006 13:36:14 server id 1 log_pos 400 Query thread_id=17

exec_time=0 error_code=0SET TIMESTAMP=1097094974;delete from user where host = 'p-int.corp.yahoo.com';# at 488#041006 13:36:26 server id 1 log_pos 488 Query thread_id=17

exec_time=0 error_code=0SET TIMESTAMP=1097094986;delete from user where host = '216.145.53.145/255.255.255.0';

MySQL Administration Workshop

42

# at 584#041006 13:36:49 server id 1 log_pos 584 Query thread_id=17

exec_time=0 error_code=0SET TIMESTAMP=1097095009;delete from user where host = '216.145.53.145';

a-int 14:29:30 /home/y/bin $ ./mysqlbinlog /home/y/var/mysql/data/a-int-bin.013

/*!40019 SET @@session.max_insert_delayed_threads=0*/;# at 4#041006 13:45:58 server id 1 log_pos 4 Start: binlog v 3, server v4.0.22-Yahoo-SMP-log created 041006 13:45:58 at startup# at 79#041006 13:47:42 server id 1 log_pos 79 Query thread_id=3exec_time=0 error_code=0use mysql;SET TIMESTAMP=1097095662;delete from user where user = 'repl';# at 151#041006 13:48:54 server id 1 log_pos 151 Query thread_id=3

exec_time=0 error_code=0SET TIMESTAMP=1097095734;GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TOrepl@"216.145.53.174" IDENTIFIED BY 'repl';# at 283#041006 13:50:02 server id 1 log_pos 283 Query thread_id=3

exec_time=0 error_code=0SET TIMESTAMP=1097095802;GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO repl@"p-int" IDENTIFIED BY 'repl';# at 406#041006 13:50:39 server id 1 log_pos 406 Query thread_id=3

exec_time=0 error_code=0SET TIMESTAMP=1097095839;GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO repl@"a-int" IDENTIFIED BY 'repl';# at 529#041006 14:01:28 server id 1 log_pos 529 Query thread_id=3

exec_time=0 error_code=0SET TIMESTAMP=1097096488;delete from user where user = 'repl';

MySQL Administration Workshop

43

# at 601#041006 14:03:39 server id 1 log_pos 601 Query thread_id=19

exec_time=0 error_code=0SET TIMESTAMP=1097096619;GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TOrepl@"216.145.53.174" IDENTIFIED BY 'repl';# at 733#041006 14:05:28 server id 1 log_pos 733 Query thread_id=19

exec_time=0 error_code=0SET TIMESTAMP=1097096728;GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO repl@"p-int.corp.yahoo.com" IDENTIFIED BY 'repl';# at 871#041006 14:07:45 server id 1 log_pos 871 Query thread_id=19

exec_time=0 error_code=0SET TIMESTAMP=1097096865;delete from user where user = 'repl';# at 943#041006 14:07:49 server id 1 log_pos 943 Query thread_id=19

exec_time=1 error_code=0SET TIMESTAMP=1097096869;GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO repl@"p-int.corp.yahoo.com" IDENTIFIED BY 'repl';

To demonstrate the staled master-slave replication resolution, we will makesome changes on the master machine first and then resolve the slavereplication failure and include the already made changes since the last slavedatabase update from the master database backup

master

a-int 14:32:13 /home/y/bin $ mysql -u root TESTDB

mysql> desc test1;

+-----------------+-----------+--------+-----+------------+---------+| Field | Type | Null | Key | Default | Extra |+-----------------+-----------+--------+-----+-------------+--------+| id | int(11) | YES | | NULL | || salary | int(11) | YES | | NULL | || department | text | YES | | NULL | |+-----------------+-----------+--------+------+------------+--------+

MySQL Administration Workshop

44

mysql> insert into test1 values (100, 25000, 'MFT');

Query OK, 1 row affected (0.24 sec)

mysql> insert into test1 values (101, 35000, 'MFT');

Query OK, 1 row affected (1.15 sec)

mysql> commit;

Query OK, 0 rows affected (0.00 sec)

mysql> connect mysql;

Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -A

Connection id: 24Current database: mysql

mysql> GRANT all privileges ON *.* TO testdbl@"a-int.corp.yahoo.com" IDENTIFIED BY 'test';

Query OK, 0 rows affected (0.00 sec)

mysql> GRANT all privileges ON *.* TO testdb2@"a-int.corp.yahoo.com" IDENTIFIED BY 'test1';

Query OK, 0 rows affected (0.00 sec)

mysql> connect TESTDB

Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -A

Connection id: 25Current database: TESTDB

MySQL Administration Workshop

45

mysql> insert into test1 values (102, 45000, 'MFT');

Query OK, 1 row affected (0.00 sec)

mysql> insert into test1 values (103, 45000, 'MFT');

Query OK, 1 row affected (0.02 sec)

mysql> commit;

Query OK, 0 rows affected (0.00 sec)

Now the position in the master binary log was moved ahead to 1445

mysql> show master status;

+-------------------+-------------+--------------------+---------------------------+| File | Position | Binlog_do_db | Binlog_ignore_db |+-------------------+-------------+--------------------+---------------------------+| a-int-bin.014 | 1445 | | |+-------------------+-------------+--------------------+---------------------------+

mysql> exit

The changes on the MySQL master database are now recorded in the a-int-bin.014 binary log

a-int 14:35:05 /home/y/bin $ ./mysqlbinlog /home/y/var/mysql/data/a-int-bin.014

..

..

# at 442#041006 14:22:45 server id 1 log_pos 442 Query thread_id=2

exec_time=0 error_code=0SET TIMESTAMP=1097097765;grant all privileges on TESTDB.* to repl@"216.145.50.174";

MySQL Administration Workshop

46

# at 535#041006 14:22:56 server id 1 log_pos 535 Query thread_id=2

exec_time=0 error_code=0SET TIMESTAMP=1097097776;update user set password=PASSWORD('repl') where user='repl';# at 630#041006 14:24:35 server id 1 log_pos 630 Query thread_id=2

exec_time=0 error_code=0SET TIMESTAMP=1097097875;GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TOrepl@"216.145.50.174";

# at 741#041006 14:32:48 server id 1 log_pos 741 Querythread_id=23 exec_time=0 error_code=0use TESTDB;SET TIMESTAMP=1097098368;insert into test1 values (100, 25000, 'MFT');

# at 822#041006 14:32:57 server id 1 log_pos 822 Querythread_id=23 exec_time=0 error_code=0SET TIMESTAMP=1097098377;insert into test1 values (101, 35000, 'MFT');

# at 903#041006 14:34:18 server id 1 log_pos 903 Querythread_id=24 exec_time=0 error_code=0use mysql;SET TIMESTAMP=1097098458;GRANT all privileges ON *.* TO testdbl@"a-int.corp.yahoo.com"IDENTIFIED BY 'test';

# at 1021#041006 14:34:27 server id 1 log_pos 1021 Querythread_id=24 exec_time=0 error_code=0SET TIMESTAMP=1097098467;GRANT all privileges ON *.* TO testdb2@"a-int.corp.yahoo.com"IDENTIFIED BY 'test1';

MySQL Administration Workshop

47

# at 1140#041006 14:34:53 server id 1 log_pos 1140 Querythread_id=25 exec_time=0 error_code=0use TESTDB;SET TIMESTAMP=1097098493;insert into test1 values (102, 45000, 'MFT');

# at 1221#041006 14:35:01 server id 1 log_pos 1221 Querythread_id=25 exec_time=0 error_code=0SET TIMESTAMP=1097098501;insert into test1 values (103, 45000, 'MFT');

To fix the MySQL slave replication errors, we have to first stop the slavereplication

mysql> stop slave;

Query OK, 0 rows affected (0.01 sec)

Then we roll forward the slave replication from the a-int-bin.001, position79 to the a-int-bin.011, position 741

mysql> change master to master_log_file='a-int-bin.014',master_log_pos=741;

Query OK, 0 rows affected (0.02 sec)

Now start the slave replication from the new master binary log and position

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

Check again the slave replication status

MySQL Administration Workshop

48

mysql> show slave status\G

*************************** 1. row *************************** Master_Host: a-int.corp.yahoo.com Master_User: repl Master_Port: 3306 Connect_retry: 60 Master_Log_File: a-int-bin.014 Read_Master_Log_Pos: 1302 Relay_Log_File: p-int-relay-bin.001 Relay_Log_Pos: 605Relay_Master_Log_File: a-int-bin.014 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_do_db: Replicate_ignore_db: Last_errno: 0 Last_error: Skip_counter: 0 Exec_master_log_pos: 1302 Relay_log_space: 6051 row in set (0.00 sec)

The slave replication was forwarded to the new master binary log andposition and the both replication threads (Slave_IO_Running andSlave_SQL_Running) are working

To validate if all the data from the master is now available in the slavedatabase

mysql> connect TESTDB; Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -A

Connection id: 67Current database: TESTDB

mysql> select * from test1;

MySQL Administration Workshop

49

+------+------------+-------------------+| id | salary | department |+------+------------+-------------------+| 100 | NULL | NULL || 101 | NULL | NULL || 102 | NULL | NULL || 103 | NULL | NULL || 102 | 45000 | MFT || 103 | 45000 | MFT || 100 | 25000 | MFT || 101 | 35000 | MFT || 102 | 45000 | MFT || 103 | 45000 | MFT |+------+------------+--------------------+

10 rows in set (0.00 sec)

Now the rows added to the master database before exist in the slavedatabase

Another useful command when troubleshooting or setting master-slavereplication is the following command

SET GLOBAL SQL_SLAVE_SKIP_COUNTER = n

This command skip the next n events from the master on the slave. This isuseful for recovering from replication stops caused by a statement. This statement is valid only when the slave thread is not running. Otherwise, itproduces an error.

To demonstrate the use of this command we will use the following demo

current slave:

mysql> show slave status\G

*************************** 1. row *************************** Master_Host: a-int.corp.yahoo.com Master_User: repl Master_Port: 3306 Connect_retry: 60 Master_Log_File: a-int-bin.014

MySQL Administration Workshop

50

Read_Master_Log_Pos: 1302 Relay_Log_File: p-int-relay-bin.001 Relay_Log_Pos: 605Relay_Master_Log_File: a-int-bin.014 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_do_db: Replicate_ignore_db: Last_errno: 0 Last_error: Skip_counter: 0 Exec_master_log_pos: 1302 Relay_log_space: 6051 row in set (0.00 sec)

mysql> stop slave;

Query OK, 0 rows affected (0.01 sec)

master

a-int 15:02:15 /home/y/bin $ mysql -u root TESTDB

mysql> create table test3 (id integer);

Query OK, 0 rows affected (0.01 sec)

mysql> update test1 set salary = salary * 1.5;Query OK, 4 rows affected (0.00 sec)Rows matched: 8 Changed: 4 Warnings: 0

mysql> commit;Query OK, 0 rows affected (0.00 sec)

check the master binary log:

a-int 15:08:17 /home/y/bin $ ./mysqlbinlog /home/y/var/mysql/data/a-int-bin.014..

MySQL Administration Workshop

51

..

# at 1302#041006 15:06:21 server id 1 log_pos 1302 Query thread_id=29

exec_time=0 error_code=0SET TIMESTAMP=1097100381;create table test3 (id integer);

# at 1370#041006 15:09:56 server id 1 log_pos 1370 Query thread_id=31

exec_time=0 error_code=0SET TIMESTAMP=1097100596;update test1 set salary = salary * 1.5;

slave

To introduce an error on the slave replication machine, we will skip the nextentry in the current master binary log a-int-bin.014 (position 1302)

mysql> set global sql_slave_skip_counter = 1;

Query OK, 0 rows affected (0.00 sec)

and restart the MySQL slave replication

mysql> start slave;

Query OK, 0 rows affected (0.01 sec)

Once we check the slave replication status, we can see the position 1302was skipped and position 1370 was applied. Now the replication is the onthe 1445 position

mysql> show slave status\G

*************************** 1. row *************************** Master_Host: a-int.corp.yahoo.com Master_User: repl Master_Port: 3306 Connect_retry: 60

MySQL Administration Workshop

52

Master_Log_File: a-int-bin.014 Read_Master_Log_Pos: 1445 Relay_Log_File: p-int-relay-bin.001 Relay_Log_Pos: 788Relay_Master_Log_File: a-int-bin.014 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_do_db: Replicate_ignore_db: Last_errno: 0 Last_error: Skip_counter: 0 Exec_master_log_pos: 1445 Relay_log_space: 788

1 row in set (0.00 sec)

In order to introduce a master-slave replication error, we will insert recordsin the test3 table on the master. This table doesn't exists on the slave asseen from the examples above, so the slave replication should fail at thispoint

master

mysql> insert into test3 values (100);

Query OK, 1 row affected (0.00 sec)

mysql> insert into test3 values (101);

Query OK, 1 row affected (0.00 sec)

mysql> insert into test3 values (102);Query OK, 1 row affected (0.00 sec)

mysql> insert into test3 values (103);

Query OK, 1 row affected (0.00 sec)

MySQL Administration Workshop

53

mysql> insert into test3 values (104);

Query OK, 1 row affected (1.19 sec)

mysql> insert into test2 values (107, 45000, 'SDS');

Query OK, 1 row affected (0.00 sec)

mysql> commit;

Query OK, 0 rows affected (0.00 sec)

slave

mysql> show slave status\G

*************************** 1. row *************************** Master_Host: a-int.corp.yahoo.com Master_User: repl Master_Port: 3306 Connect_retry: 60 Master_Log_File: a-int-bin.014 Read_Master_Log_Pos: 1780 Relay_Log_File: p-int-relay-bin.001 Relay_Log_Pos: 788Relay_Master_Log_File: a-int-bin.014 Slave_IO_Running: Yes Slave_SQL_Running: No Replicate_do_db: Replicate_ignore_db: Last_errno: 1146 Last_error: Error 'Table 'TESTDB.test3' doesn't exist' on query'insert into test3 values (100)'. Default database: 'TESTDB' Skip_counter: 0 Exec_master_log_pos: 1445 Relay_log_space: 1123

1 row in set (0.00 sec)

MySQL Administration Workshop

54

As we can see the Slave_SQL_Running thread is not running because themaster transaction cannot be completed on the slave database. To fix thisproblem we have two choices:

– create the test3 table and restart the replication– skip the transactions, which refer to the test3 table

In the sake of demonstrating the SET GLOBALSQL_SLAVE_SKIP_COUNTER = n command we will use the secondoption.

mysql> stop slave;

Query OK, 0 rows affected (0.00 sec)

master

When reviewing the binary log on the master we can see that up to position1780 all transaction involve the test3 table.

a-int 15:26:10 /home/y/bin $ ./mysqlbinlog /home/y/var/mysql/data/a-int-bin.014

..

..

...# at 1445#041006 15:23:23 server id 1 log_pos 1445 Query thread_id=34

exec_time=0 error_code=0SET TIMESTAMP=1097101403;insert into test3 values (100);

# at 1512#041006 15:23:26 server id 1 log_pos 1512 Query thread_id=34

exec_time=0 error_code=0SET TIMESTAMP=1097101406;insert into test3 values (101);

MySQL Administration Workshop

55

# at 1579#041006 15:23:28 server id 1 log_pos 1579 Query thread_id=34

exec_time=0 error_code=0SET TIMESTAMP=1097101408;insert into test3 values (102);

# at 1646#041006 15:23:31 server id 1 log_pos 1646 Query thread_id=34

exec_time=0 error_code=0SET TIMESTAMP=1097101411;insert into test3 values (103);

# at 1713#041006 15:23:36 server id 1 log_pos 1713 Query thread_id=34

exec_time=0 error_code=0SET TIMESTAMP=1097101416;insert into test3 values (104);

# at 1780#041006 15:24:57 server id 1 log_pos 1780 Query thread_id=34

exec_time=0 error_code=0SET TIMESTAMP=1097101497;insert into test2 values (107, 45000, 'SDS');

To skip these test3 transactions

mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 5;

or

mysql> change master to master_log_file='a-int-bin.014',master_log_pos=1780;

mysql> start slave;

MySQL Administration Workshop

56

5.2 Purging the master binary logs

To purge the master binary logs we have to find, which is the current masterbinary log on the master database and which is the latest master binary logused on each slave database. All binary logs older than the current binarylog are safe to be deleted

a-int 14:58:33 /home/y/bin $ mysql -u root mysql

mysql> show master status;

+-------------------+-------------+--------------------+---------------------------+| File | Position | Binlog_do_db | Binlog_ignore_db |+-------------------+-------------+--------------------+---------------------------+| a-int-bin.014 | 1302 | | |+-------------------+-------------+--------------------+---------------------------+

1 row in set (0.00 sec)

slave

mysql> show slave status\G

*************************** 1. row *************************** Master_Host: a-int.corp.yahoo.com Master_User: repl Master_Port: 3306 Connect_retry: 60 Master_Log_File: a-int-bin.014 Read_Master_Log_Pos: 1302 Relay_Log_File: p-int-relay-bin.001 Relay_Log_Pos: 605Relay_Master_Log_File: a-int-bin.014 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_do_db: Replicate_ignore_db: Last_errno: 0

MySQL Administration Workshop

57

Last_error: Skip_counter: 0 Exec_master_log_pos: 1302 Relay_log_space: 6051 row in set (0.00 sec)

the current binary logs:

a-int 14:29:18 /home/y/bin $ ls -la /home/y/var/mysql/data/

-rw-rw---- 1 mysql users 929 Sep 16 11:16 a-int-bin.001-rw-rw---- 1 mysql users 311 Sep 16 15:14 a-int-bin.002-rw-rw---- 1 mysql users 11582 Sep 21 11:56 a-int-bin.003-rw-rw---- 1 mysql users 79 Sep 21 11:58 a-int-bin.004-rw-rw---- 1 mysql users 79 Sep 21 12:02 a-int-bin.005-rw-rw---- 1 mysql users 79 Sep 21 17:18 a-int-bin.006-rw-rw---- 1 mysql users 79 Sep 21 17:33 a-int-bin.007-rw-rw---- 1 mysql users 1563 Sep 27 17:44 a-int-bin.008-rw-rw---- 1 mysql users 4 Sep 27 17:44 a-int-bin.009-rw-rw---- 1 mysql users 119 Oct 6 12:09 a-int-bin.010-rw-rw---- 1 mysql users 468 Oct 6 13:16 a-int-bin.011-rw-rw---- 1 mysql users 666 Oct 6 13:36 a-int-bin.012-rw-rw---- 1 mysql users 1081 Oct 6 14:07 a-int-bin.013-rw-rw---- 1 mysql users 741 Oct 6 14:24 a-int-bin.014-rw-rw---- 1 mysql users 224 Oct 6 14:11 a-int-bin.index

mysql> purge master logs to 'a-int-bin.014';

Query OK, 0 rows affected (0.00 sec)

after the purge, all binary logs older than a-int-bin.014 are deleted

a-int 15:02:02 /home/y/bin $ ls -la /home/y/var/mysql/data

-rw-rw---- 1 mysql users 1302 Oct 6 14:35 a-int-bin.014-rw-rw---- 1 mysql users 16 Oct 6 15:01 a-int-bin.index

MySQL Administration Workshop