设为首页 加入收藏

TOP

Mysql学习(一)添加一个新的用户并用golang操作Mysql(二)
2019-01-07 16:09:04 】 浏览:460
Tags:Mysql 学习 添加 一个 用户 并用 golang 操作
| blob | NO | | NULL | | | x509_issuer | blob | NO | | NULL | | | x509_subject | blob | NO | | NULL | | | max_questions | int(11) unsigned | NO | | 0 | | | max_updates | int(11) unsigned | NO | | 0 | | | max_connections | int(11) unsigned | NO | | 0 | | | max_user_connections | int(11) unsigned | NO | | 0 | | | plugin | char(64) | NO | | mysql_native_password | | | authentication_string | text | YES | | NULL | | | password_expired | enum('N','Y') | NO | | N | | | password_last_changed | timestamp | YES | | NULL | | | password_lifetime | smallint(5) unsigned | YES | | NULL | | | account_locked | enum('N','Y') | NO | | N | | +------------------------+-----------------------------------+------+-----+-----------------------+-------+ 45 rows in set (0.03 sec)

查看user中的已存在的用户和主机

mysql> select Host, User from user;
+-----------+------------------+
| Host      | User             |
+-----------+------------------+
| localhost | debian-sys-maint |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
4 rows in set (0.00 sec)

接着我们新增一个自己的账户使用

mysql> CREATE USER '填用户名'@'localhost' IDENTIFIED BY '填密码';
Query OK, 0 rows affected (0.04 sec)

再次查看user表

mysql> select Host, User from user;
+-----------+------------------+
| Host      | User             |
+-----------+------------------+
| localhost | ailumiyana       |
| localhost | debian-sys-maint |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
5 rows in set (0.00 sec)

给新建的用户添加权限,刷新权限后, 查看新增用户的权限是否已经加进去.

mysql> grant insert,select,delete,update,create,drop on *.* to ailumiyana@"localhost" identified by 'qwedsa';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> show grants for ailumiyana@localhost;
+---------------------------------------------------------------------------------------+
| Grants for ailumiyana@localhost                                                       |
+---------------------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON *.* TO 'ailumiyana'@'localhost' |
+---------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

当然我们自己用,当然设置所有权限,更为便捷,那么可以改成这样子
all privileges 表示所有权限.

mysql> grant all privileges on *.* to ailumiyana@"localhost" identified by 'qwedsa';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> show grants for ailumiyana@localhost;
+---------------------------------------------------------+
| Grants for ailumiyana@localhost                         |
+---------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'ailumiyana'@'localhost' |
+---------------------------------------------------------+
1 row in set (0.00 sec)

退出,重启服务.

mysql> exit
Bye
ailumiyana@ailumiyana:~/Git_Project/Go_Test$ service mysql restart
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'mysql.service'.
Authenticating as: ailumiyana,,, (ailumiyana)
Password: 
==== AUTHENTICATION COMPLETE ===

这样新用户就添加进去了,也配置了适当的权限,接下来用golang简单测试一下。

使用go-sql-driver驱动测试Mysql

先在mysql中创建一个表

mysql> create table user_info(
    -> id int(4) not null primary key auto_increment,
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇golang简单实现jwt验证(beego、xo.. 下一篇golang 防SQL注入 基于反射、TAG..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目