1. 程式人生 > >mysql5.1.73 主從複製的設定套路

mysql5.1.73 主從複製的設定套路

2、修改主伺服器master:
   #vi /etc/my.cnf
 

[client]
#password       = your_password
port            = 3306
socket          = /tmp/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port            = 3306
socket          = /tmp/mysql.sock
skip-locking
key_buffer_size = 384M
max_allowed_packet = 128M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M

# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 8


server-id       = 1


[mysqldump]
quick
max_allowed_packet = 128M


[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
 

   


3、修改從伺服器slave:
   #vi /etc/my.cnf
    

[client]
#password       = your_password
port            = 3306
socket          = /tmp/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port            = 3306
socket          = /tmp/mysql.sock
skip-locking
key_buffer_size = 384M
max_allowed_packet = 128M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
 

# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 8

log-bin=mysql-bin

server-id       = 2


[mysqldump]
quick
max_allowed_packet = 128M

[mysql]
no-auto-rehash


[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
 

 

改完之後,兩臺雞雞都要重啟一下mysql

service mysqld restart

登入一下主機的MYSQL

 mysql -u root -p

GRANT REPLICATION SLAVE ON *.* TO 'root' @'%' IDENTIFIED BY '密碼';


SHOW MASTER STATUS;
檢視一下position 的值

然後再另一個從伺服器上配置

CHANGE MASTER TO MASTER_HOST='172.16.108.71',MASTER_USER='root',MASTER_PASSWORD='',MASTER_LOG_FILE='mysql-bin.000005',MASTER_LOG_POS=position的ID;

然後啟動

START SLAVE;

檢視狀態,主要看看有沒有什麼錯誤

 SHOW DATABASES;

SHOW SLAVE STATUS;

 SHOW VARIABLES LIKE 'server_id'; --這個是檢查ID是否重複
 

下面是測試工作:

主伺服器Mysql,建立資料庫,並在這個庫中建表插入一條資料:


  mysql> create database t_test;
  Query OK, 1 row affected (0.00 sec)


  mysql> use t_test;
  Database changed


  mysql>create table t_test(id int(3),name varchar(20));
  Query OK, 0 rows affected (0.00 sec)
 
  mysql> insert into t_test values(23,'zhangs');
  Query OK, 1 row affected (0.00 sec)


  mysql> show databases;
   +--------------------+
   | Database           |
   +--------------------+
   | information_schema |
   | t_test                |
   | mysql                |
   | test                 |
   +--------------------+
   4 rows in set (0.00 sec)


在從伺服器Mysql查詢:


   mysql> show databases;


   +--------------------+
   | Database               |
   +--------------------+
   | information_schema |
   | t_test                 |      
   | mysql                 |
   | test          |
   +--------------------+
   4 rows in set (0.00 sec)


   mysql> use t_test
   Database changed
   mysql> select * from t_test;      //在從伺服器上檢視主伺服器上新增的具體資料
   +------+-------+
   | id   | name  |
   +------+-------+
   |    23|zhangs|
   +------+------+
   1 row in set (0.00 sec)
 
關閉slave伺服器: stop slave/start slave 
    再停止mysql 服務,service mysqld stop



    編寫一shell指令碼,用nagios監控slave的兩個yes(Slave_IO及Slave_SQL程序),如發現只有一個或零個yes,就表明主從有問題了,發簡訊警報