1. 安装 MySQL

    1. [root@liqiang.io]# docker run --name mariadb -e MYSQL_ROOT_PASSWORD=password -d mariadb:10.4
  2. 授权用户远程登录

    1. [root@liqiang.io]# mysql -uroot -ppassword
    2. CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypass';
    3. GRANT ALL ON *.* TO 'myuser'@'%';
  3. 创建数据库:

    1. [root@liqiang.io]# CREATE DATABASE IF NOT EXISTS test default charset utf8 COLLATE utf8_general_ci;
  4. 通过 GORM 使用 MYSQL

    1. [root@liqiang.io]# cat test_mysql.go
    2. url = "myuser:password@tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local"
    3. db, err = gorm.Open("mysql", url)
    4. if err != nil {
    5. return err
    6. }