1024programmer Mysql canal to achieve mysql data synchronization

canal to achieve mysql data synchronization

Mysql Video TutorialThe column introduces data synchronization based on canal.

Based on the above explanation, before implementing canal, we simply do a master-slave replication. One master, one slave

  • First download the mysql image and start
docker pull mysql:latest
 docker run -itd --name mysql-1 -p 23306:3306 -e MYSQL_ROOT_PASSWORD=root mysql
 docker run -itd --name mysql-2 -p 23307:3306 -e MYSQL_ROOT_PASSWORD=root mysql 
  • Related commands explain:

name xxx :xxx is the container name
p 111:222 where 111 is the host port and 222 is the container port
MYSQL_ROOT_PASSWORD=root Set the root account password to root

  • Set mysql-1 as the master and mysql-2 as the slave library
  • Modify the configuration of mysql and install the vim editor
apt-get update
 apt-get install vim 
  • Create a mysql account in the main library for use by the slave library
CREATE USER  & # 39; slave & # 39; @ & # 39; % & # 39; IDENTIFIED BY & # 39; 123456 & # 39;;
 GRANT REPLICATION SLAVE, REPLICATION CLIENT ON . TO 'slave'@'%';
 FLUSH PRIVILEGES; 

  • Modify the slave server
[mysqld]
 pid-file = /var/run/mysqld/mysqld.pid
 socket = /var/run/mysqld/mysqld.sock
 datadir = /var/lib/mysql
 secure-file-priv= NULL
 server_id=100
 log-bin=mysql-slave-bin
 relay_log=edu-mysql-relay-bin 
  • Exit and restart slave server docker
  • Enter slave server execution
mysql> change master to master_host='172.17.0.4', master_user='slave', master_password='123456', master_port=  3306, master_log_file='edu-mysql-bin.000001', master_log_pos= 877, master_connect_retry=30; 

Related command explanation
master_port: The port number of the Master, which refers to the port number of the container
master_user: user for data synchronization
master_password: the password of the user used for synchronization
master_log_file: Specifies the log file from which Slave starts to copy data, that is, the value of the File field mentioned above
master_log_pos: which Position to read from, that is, the value of the Position field mentioned above
master_connect_retry: If the connection fails, the time interval between retries, in seconds, the default is 60 seconds
Execute show slave status \\\\G on the mysql terminal in the Slave; to view the master-slave synchronization status.

  • There is a message indicating that the configuration is successful

Why do you have to first Speaking of master-slave replication, in fact, canal disguises itself as a slave server to read logs and get data;

Use docker to deploy canal

Reference link

docker pull canal/canal-server:latest
 # download script
 wget https://raw.githubusercontent.com/alibaba/canal/master/docker/run.sh

 # Build a queue whose destination name is test, address corresponds to the database ip+port, dbUsername corresponds to the database user name, dbPassword corresponds to the database password, pay attention to modify it to your own
 sh run.sh -e canal.auto.scan=false \\\\
 -e canal.destinatiOns=test \\\\
 -e canal.instance.master.address=172.17.0.4:3306 \\\\
 -e canal.instance.dbUsername=canal \\\\
 -e canal.instance.dbPassword=canal \\\\
 -e canal.instance.cOnnectionCharset=UTF-8 \\\\
 -e canal.instance.tsdb.enable=true \\\\
 -e canal.instance.gtidon=false \\\\ 
  • You can enter the container after startup, look at the log inside, if there is a red message, it means success, otherwise check inside The error message! It’s not difficult
  • Using component installation, here I have a laravel framework, which is installed and used directly in laravel, and the relevant code is posted
 # Install component canal-php
  composer require xingwenge/canal_php
 # Write a script to monitor
 connect("172.17.0.5", 11111);//Change here to your own configuration
             $client->checkValid();
             $client->subscribe("1001", "test", ".*\\\\\\\\..*");//corresponding to the queue name of test when starting the container
             while (true) {
                 $message = $client->get(100);
                 if ($entries = $message->getEntries()) {
                     foreach ($entries as $entry) {
                         Fmt::println($entry);
                     }
                 }
                 sleep(1);
             }

             $client->disConnect();
         } catch (\\\\Exception $e) {
             echo $e->getMessage(), PHP_EOL;
         }

     }

 } 
  • Change data

Related free learning recommendation:mysql Video Tutorial

The above is the detailed content of canal to realize mysql data synchronization, please pay attention to other related articles on 1024programmer.com for more!

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/canal-to-achieve-mysql-data-synchronization/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索