Mysql Master/Slaver 搭建

来源:互联网 发布:简单的java小程序 编辑:程序博客网 时间:2024/05/22 14:33

【学习笔记】

1  Master/Slave 架构的实现是基于Master的二进制日志来进行的,必须开启主服务器的二进制log.

2  可以考虑如下方案:Master采用INNODB(稳定,安全),Slave采用MyISAM(速度),充分利用不同引擎的性能。

3 在主服务器和从服务器上,均必须使用server-id选项为每个服务器建立唯一的复制ID。
  你应为每个主服务器和从服务器从1到232–1的范围挑一个唯一的正整数。例如:server-id=3

4 不能从使用新二进制日志格式的主服务器向使用旧二进制日志格式的从服务器复制
  (例如,从MySQL 5.0到MySQL 4.1)。。这样操作在复制设置升级服务器时后果严重.
  我们推荐使用最近的MySQL版本,因为复制功能在不断地改进中。
  我们还推荐主服务器和从服务器使用相同的版本。
  我们建议升级主服务器和从服务器,运行alpha或beta版本到新的(产品)版本。
  在许多情况下,从新的主服务器向旧的从服务器复制将会失败。
  一般原则,运行MySQL 5.1.x的从服务器可以与旧的主服务器(可以运行MySQL 3.23、4.0或者4.1)一起使用, 但不能反过来。

 

【具体操作】

主服务器(192.168.8.1):
# /usr/local/mysql/bin/mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 74 to server version: 5.0.41-enterprise-gpl-log

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> GRANT REPLICATION SLAVE ON *.*
    -> TO
'repl'@'192.168.8.2' IDENTIFIED BY 'repl';             
Query OK, 0 rows affected (0.08 sec)

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000018 |     1483 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.03 sec)

如果打算在slave上执行 LOAD TABLE FROM MASTER 或 LOAD DATA FROM MASTER 语句,
那么必须给该帐户授予附加权限:

授予全局 SUPER 和 RELOAD 权限。

授予对想要加载的所有表上的 SELECT 权限。在master上任何没有 SELECT 权限的表都会被 LOAD DATA FROM MASTER 略过。

 

从属服务器(192.168.8.2):
退到shell模式,关闭mysql:
1 mysqladmin -u root shutdown;

根据手册修改my.cnf如下:

server-id = 2

# The replication master for this slave - required
master-host     =   192.168.8.1
#
# The username the slave will use for authentication when connecting
# to the master - required
master-user     =   repl
#
# The password the slave will authenticate with when connecting to
# the master - required
master-password =   repl
#
# The port the master is listening on.
# optional - defaults to 3306
master-port     =  3306
replicate-do-db=master_slave

修改完成之后.保存.

 

3 主服务器reset master;重设二进制日志.

 

4 /usr/local/mysql/bin/mysqld_safe &
查看是否有启动slave进程:
SHOW PROCESSLIST/G

 

5 从服务器登录mysql后:
show slave status;
SLAVE START;


ok.在主服务器上写入数据,查看从属服务器,测试通过。

 

补充:

上面配置虽然ok了,但在后续的实践中,发现使用下述命令才够全面,并更有灵活性:
mysql> CHANGE MASTER TO
    ->     MASTER_HOST='master_host_name',
    ->     MASTER_USER='replication_user_name',
    ->     MASTER_PASSWORD='replication_password',
    ->     MASTER_LOG_FILE='recorded_log_file_name',
    ->     MASTER_LOG_POS=recorded_log_position;

原创粉丝点击