1. 安装 MySQL

2. 安全设置

[[email protected]]# cat /etc/my.cnf
[mysqld]
bind-address = 127.0.0.1

3. 查看所有数据库

[[email protected]]# mysql -uroot -p
MariaDB [(none)]> show databases;

4. 修改用户密码

[[email protected]]# mysql -uroot -p
MariaDB [mysql]> set password for [email protected] = password('password');

5. 授权用户远程登录

[[email protected]]# mysql -uroot -ppassword
MariaDB [(none)]> use mysql;
CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypass';
GRANT ALL ON *.* TO 'myuser'@'%';

6. 创建数据库

[[email protected]]# CREATE DATABASE IF NOT EXISTS test default charset utf8 COLLATE utf8_general_ci;

7. 通过 GORM 使用 MYSQL

[[email protected]]# cat test_mysql.go
url = "myuser:[email protected](127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local"
db, err = gorm.Open("mysql", url)
if err != nil {
    return err
}

8. 清空一张表

删除表信息的方式有两种 :

[[email protected]]# truncate table table_name;
[[email protected]]# delete * from table_name;

区别 :