企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
### 4.2.1 Master配置 1)my.cnf 配置 ~~~ #vi /etc/mysql/my.cnf [mysqld] log-bin=mysql-bin //[必须]启用二进制日志 server-id=140 //[必须]服务器唯一ID,默认是1,一般取IP最后一段 ~~~ 2)重启mysql ~~~ sudo /etc/init.d/mysql restart ~~~ 3)在主服务器上建立帐户并授权slave ~~~ #mysql –u root –p 123456 mysql>GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by '123456'; //一般不用root帐号,“%”表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如192.168.245.139,加强安全。 ~~~ 4) 登录mysql,查询master的状态 ~~~ mysql>show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000004 | 308 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) ~~~ 注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化。 ### 4.2.2 Slave配置 1)my.cnf配置 ~~~ #vi /etc/mysql/my.cnf [mysqld] log-bin=mysql-bin //[必须]启用二进制日志 server-id=139 //[必须]服务器唯一ID,默认是1,一般取IP最后一段 ~~~ 2)重启mysql ~~~ sudo /etc/init.d/mysql restart ~~~ 3)配置从服务器Slave: ~~~ mysql>change master to master_host='192.168.245.140',master_user='mysync',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=308; //注意不要断开,“308”无单引号。M ysql>start slave; //启动从服务器复制功能 ~~~ 4) 检查从服务器复制功能状态: ~~~ mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.245.140 Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 5669 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 5482 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: 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: 5669 Relay_Log_Space: 5639 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: Replicate_Ignore_Server_Ids: Master_Server_Id: 140 1 row in set (0.00 sec) ~~~ 注:Slave\_IO及Slave\_SQL进程必须正常运行,即YES状态,否则都是错误的状态。 ## 4.3 主从服务器测试    主服务器Mysql,建立数据库,并在这个库中建表插入一条数据,观看从库是否也增加了相应的数据库、数据表、数据。