Jelajahi Sumber

Setup for binlog streaming.

Josh Brickner 7 tahun lalu
induk
melakukan
d7fe8c2104
5 mengubah file dengan 31 tambahan dan 27 penghapusan
  1. 5 6
      binlog/binlog.go
  2. 22 16
      binlog/connection.go
  3. 0 0
      conf/mysql.cnf
  4. 1 1
      config.json
  5. 3 4
      docker-compose.yaml

+ 5 - 6
binlog/binlog.go

@@ -21,12 +21,11 @@ func (c *Conn) listenForBinlog() error {
 	}
 
 	fmt.Printf("res = %+v\n", res)
-	fmt.Println("hello")
-	err = c.listenForBinlog()
-	fmt.Println("test")
-	if err != nil {
-		return err
-	}
+
+	// err = c.listenForBinlog()
+	// if err != nil {
+	// 	return err
+	// }
 
 	return nil
 }

+ 22 - 16
binlog/connection.go

@@ -168,24 +168,30 @@ func (d Driver) Open(dsn string) (driver.Conn, error) {
 	}
 
 	// Listen for auth response.
-	_, err = c.listen()
-	if err != nil {
-		// Auth failed.
-		return nil, err
-	}
-
-	// Auth completed successfully, move to command phase.
-	c.sequenceId = 0
-
-	// Start binlog stream
-	err = c.startBinLogStream()
-	if err != nil {
-		return nil, err
-	}
+	r, err := c.listen()
+	switch r.(type) {
+	case *OKPacket: // Login successful.
+		// Reset sequence to 0 now that connection phase is over
+		// and command phase has started.
+		c.sequenceId = 0
+
+		// Tell server to stream binlog.
+		err = c.startBinLogStream()
+		if err != nil {
+			return nil, err
+		}
 
-	err = c.listenForBinlog()
-	if err != nil {
+		err = c.listenForBinlog()
+		if err != nil {
+			return nil, err
+		}
+	case *ErrorPacket: // Bad login.
+		ep := r.(*ErrorPacket)
+		em := fmt.Sprintf("Error %d: %s", ep.ErrorCode, ep.ErrorMessage)
+		err = errors.New(em)
 		return nil, err
+	case *AuthMoreDataPacket:
+		panic(errors.New("Unexpected AuthMoreDataPacket"))
 	}
 
 	return c, err

+ 0 - 0
conf/replicate.cnf → conf/mysql.cnf


+ 1 - 1
config.json

@@ -3,7 +3,7 @@
   "port": 3316,
   "user": "root",
   "password": "root",
-  "database": "information_schema",
+  "database": "sample",
   "ssl": false,
   "ssl-key": "",
   "ssl-cer": "",

+ 3 - 4
docker-compose.yaml

@@ -9,7 +9,7 @@ services:
       - "3318:3306"
     restart: always
     volumes:
-        -  "./conf/replicate.cnf:/etc/mysql/replicate.cnf"
+        - "./conf/mysql.cnf:/etc/mysql/conf.d/mysql.cnf"
   mysql57:
     image: mysql:5.7
     environment:
@@ -19,7 +19,7 @@ services:
       - "3317:3306"
     restart: always
     volumes:
-        -  "./conf/replicate.cnf:/etc/mysql/replicate.cnf"
+        - "./conf/mysql.cnf:/etc/mysql/conf.d/mysql.cnf"
   mysql56:
     image: mysql:5.6
     environment:
@@ -29,5 +29,4 @@ services:
       - "3316:3306"
     restart: always
     volumes:
-        -  "./conf/replicate.cnf:/etc/mysql/replicate.cnf"
-
+        - "./conf/mysql.cnf:/etc/mysql/conf.d/mysql.cnf"