Mysql master-slave replication
1, environment
Master master: 192.168.10.23
From the server slave: 192.168.10.18
2. Install Mysql
That may arise after installation to avoid abnormal, please clean out of history MySQL installation files,
yum installed the MySQL
1.1 input terminal yum list installed mysql*
to view the history list to install;
1.2 yum remove [name]
command to unmount the installation package;
rpm installed the MySQL
2.1 input terminal rpm -qa | grep -i mysql
command to view the history list to install;
2.2 rpm -ev [name]
command to unmount the installation package;
重点
3.1 the terminal input find / -name mysql
command to view the dispersed mysql installation files or folders;
3.2 rm -rvf [name]
command to delete all files msyql residue;
3.3 input find / -name my.cnf
command to check the residual mysql configuration file, then rm -rvf [name]
delete these profiles.
In the master node from the MySQL server is installed separately, and to ensure consistent time the two services.
# yum -y install mysql mysql-devel mysql-server Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Install Process Loading mirror speeds from cached hostfile * base: mirrors.cn99.com * extras: mirrors.aliyun.com * updates: mirrors.cn99.com base | 3.7 kB 00:00 extras | 3.4 kB 00:00 updates | 3.4 kB 00:00 Package mysql-5.1.73-8.el6_8.x86_64 already installed and latest version Package mysql-devel-5.1.73-8.el6_8.x86_64 already installed and latest version Package mysql-server-5.1.73-8.el6_8.x86_64 already installed and latest version Nothing to do # mysql --version mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1 #
3. Configuration
Configure Master of mysql configuration file, / etc / my.cnf
CAT # / etc / the my.cnf [mysqld] DATADIR = / Data / MySQL database address of the directory # Socket = / var / lib / MySQL / mysql.sock User = MySQL #mysql user operation #disabling symbolic -links IS Recommended to Prevent Assorted Risks Security symbolic -links = 0 # 0 disables symbolic link log -bin = MySQL- bin # enable binary logging server -id = . 1 # server ID, may 1- any integer between auto_increment_offset = . 1 auto_increment_increment = 2 [the mysqld_safe] log -error = / var / log / mysqld.log PID -file = / var / RUN / mysqld / mysqld.pid replicate - do -db = All database synchronization # #
Create a database directory just set, and give mysql user privileges
# mkdir /data/mysql/ -p # chown -R mysql:mysql /data/mysql/ # ll -d /data/mysql/ drwxr-xr-x. 6 mysql mysql 4096 Sep 18 18:42 /data/mysql/ #
Turn off the firewall, selinx, or will start being given mysql
[Warning] Can't create test file /data/mysql/server2.lower-test
If you have an old mysql database, and then start being given:
Table 'mysql.plugin' doesn't exist
Use mv -r / var / lib / mysql / destination to move the database under the existing / var / lib / mysql to somewhere else, or directly rm -rf / var / lib / mysql delete, and then restart service mysqld restart it.
Restart mysql service
# /etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
# ps -ef | grep mysql
root 15393 7145 0 16:42 pts/1 00:00:00 mysql
root 17368 1 0 18:42 pts/0 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/data/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
mysql 17485 17368 0 18:42 pts/0 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/data/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root 17543 16800 0 18:45 pts/0 00:00:00 grep mysql
#
After the start mysql, mysql create an account and enter the authorization slave, check out the master file and position parameters.
# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.1.73-log Source distribution mysql> mysql> mysql> grant replication slave on *.* to 'tongbu'@'%' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000008 | 255 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) mysql>
Configuring from the slave storage server mysql
CAT # / etc / the my.cnf [mysqld] DATADIR = / Data / MySQL database address # consistent with the main server Socket = / var / lib / MySQL / mysql.sock User = MySQL #disabling symbolic -links IS Recommended Security to Prevent Assorted Risks symbolic -links = 0 log -bin = MySQL- bin server -id = 2 # server ID, a replacement auto_increment_offset = 2 auto_increment_increment = 2 [the mysqld_safe] log-error = /var/log/mysqld.log pid-file = /var/run/mysqld/mysqld.pid master-host = 192.168.10.23 master-user = tongbu master-pass = 123456 master-port = 3306 master-connect-retry = 60 replicate-do-db = all
Also create a database directory just set, and give mysql user privileges.
Turn off the firewall, selinx, start mysql
[root@nagios usr]# service mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
#
Enter mysql, set the synchronization source, start the synchronization, and query the status
# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.1.73-log Source distribution mysql> change master to master_host='192.168.10.23',master_user='tongbu',master_password='123456',master_log_file='mysql-bin.000008',master_log_pos=255; Query OK, 0 rows affected (0.03 sec) mysql> mysql> slave start; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.23 Master_User: tongbu Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000008 Read_Master_Log_Pos: 255 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000008 Slave_IO_Running: Yes # Slave_SQL_Running: Yes #YES表示已与主服务器数据库连接成功 Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 255 Relay_Log_Space: 407 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: 1 row in set (0.00 sec) mysql>
4. Test
Add a database table on the primary database, there is no view from the database synchronization
mysql> create database test01; Query OK, 1 row affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | | test01 | +--------------------+ 4 rows in set (0.00 sec) mysql>
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | | test01 | +--------------------+ 4 rows in set (0.00 sec) mysql>
Such a simple Mysql master-slave service build better.