准备好MySQL5.7,Navicat Premium软件,并安装好
1. 安装mysql驱动
(1).安装驱动
go get -u github.com/go-sql-driver/mysql
(mysql驱动)
go get -u github.com/jmoiron/sqlx
(基于mysql驱动的封装)
(2).导入包
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
2. 链接mysql
//sqlx.Open("数据库类型", "用户名:密码@tcp(地址:端口)/数据库名")
database, err := sqlx.Open("mysql", "root:xxxx@tcp(192.168.0.109:xxx)/xxxx")
if err != nil {
fmt.Println("open mysql failed,", err)
return
}
fmt.Println(database)