Replicação de base MySQL no FreeBSD
- Nome do servidor master: mysql01.douglasqsantos.com.br
- Ip do servidor master: 10.0.0.24
- Nome do servidor slave: mysql02.douglasqsantos.com.br
- Ip do servidor slave: 10.0.0.25
Agora vamos acertar o rc.conf para o MySQL nos dois servidores
vim /etc/rc.conf [...] mysql_enable="YES" mysql_dbdir="/var/db/mysql"
Vamos procurar o ports a localização do nosso mysql nos dois servidores
whereis mysql55-server mysql55-server: /usr/ports/databases/mysql55-server
Vamos então instalar ele nos dois servidores
cd /usr/ports/databases/mysql55-server make WITH_CHARSET=latin1 WITH_XCHARSET=all WITH_PROC_SCOPE_PTH="Use threads" BUILD_OPTMIZES=yes BUILD_STATIC=yes \ WITHOUT_INNODB=yes SKIP_DNS_CHECK=yes WITH_OPENSSL=yes \ WITH_COLLATION=latin1_swedish_ci WITH_PROC_SCOPE_PTH=yes WITH_NDB=yes install clean
Pode responder as opções padrões
Caso esteja utilizando o csh recarregue as variáveis de ambiente
source /root/.cshrc
Agora vamos testar o nosso mysql
/usr/local/etc/rc.d/mysql-server start Starting mysql.
Agora vamos definir a senha do root
mysqladmin password 'senha' -u root
Agora vamos testar a autenticação
mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.5.17 Source distribution Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> quit Bye
Faça o mesmo procedimento nos dois servidores
Agora vamos acertar os dois servidores para podermos configurar a replicação.
/usr/local/etc/rc.d/mysql-server stop
Agora vamos copiar o arquivo com as configurações padrões do mysql faça o procedimento nos dois servidores
cp /usr/local/share/mysql/my-large.cnf /usr/local/etc/my.cnf chmod 644 /usr/local/etc/my.cnf
Agora no servidor mysql01 vamos acertar a configuração do arquivo my.cnf para a replicação
vim /usr/local/etc/my.cnf [...] [mysqld] bind-address = 0.0.0.0 [...] server-id = 1
Agora no servidor mysql02 vamos acertar a configuração do arquivo my.cnf para a replicação
vim /usr/local/etc/my.cnf [...] [mysqld] bind-address = 0.0.0.0 [...] server-id = 2
Agora nos dois servidores podemos reiniciar o serviço do mysql
/usr/local/etc/rc.d/mysql-server restart Stopping mysql. Waiting for PIDS: 36144. Starting mysql.
Agora vamos configuar o servidor mysql01 para ser o master da replica
mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.17-log Source distribution Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> GRANT REPLICATION SLAVE,SUPER,RELOAD ON *.* TO replica@'%' IDENTIFIED BY 'senha'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 337 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) mysql> quit; Bye
Aqui definimos em GRANT as permissões para a replicação da base utilizando o usuário replicador depois mandamos reler as permissões com FLUSH e por ultimo visualizamos o status do servidor master com SHOW
Agora vamos configurar o nosso servidor mysql02 como slave
O valor de MASTER_LOG_FILE é o valor do File que é mostrado em SHOW MASTER STATUS no servidor MASTER e o MASTER_LOG_POS é o valor retirado do mesmo comando só que no campo Position.
mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.17-log Source distribution Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> change master to -> master_host='10.0.0.24', -> master_user='replica', -> master_password='senha', -> master_log_file='mysql-bin.000001', -> master_log_pos=337; Query OK, 0 rows affected (0.01 sec)
Agora vamos iniciar o serviço de slave
mysql> start slave; Query OK, 0 rows affected (0.00 sec)
Agora vamos consultar o status da nossa replicação
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.24 Master_User: replica Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 337 Relay_Log_File: mysql02-relay-bin.000002 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000001 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: 337 Relay_Log_Space: 411 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: 1 1 row in set (0.00 sec) mysql> quit Bye
Como pode ser notado não temos nem um erro em nossa replicação.
Vamos fazer um teste, no servidor mysql01 crie uma base de dados
mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.5.17-log Source distribution Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> CREATE DATABASE mysql01; Query OK, 1 row affected (0.00 sec) mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | mysql01 | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) mysql>
Agora no servidor mysql02 vamos ver se foi replicado a nossa base de dados
mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.5.17-log Source distribution Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | mysql01 | | performance_schema | | test | +--------------------+ 5 rows in set (0.01 sec) mysql>
Como pode ser notado temos a replicação trabalhando normalmente.
Aqui efetuamos testes de replicação de um servidor MySQL do zero, porém caso já tenha um servidor MySQL em funcionamento teriamos que efetuar o DUMP do servidor que seria o master e importar no servidor que seria o SLAVE e após isso efetuar o procedimento de replicação