Mysql添加一个新的用户并赋予权限

添加一个自己的用户到mysql

首先我们需要先用root用户登录mysql,但是刚安装完没有密码,我们先跳过密码

ailumiyana@ailumiyana:~/Git_Project/Go_Test$ sudo mysqld_safe --skip-grant-tables
2019-01-07T01:35:51.559420Z mysqld_safe Logging to syslog.
2019-01-07T01:35:51.563797Z mysqld_safe Logging to '/var/log/mysql/error.log'.

root登陆

ailumiyana@ailumiyana:~/Git_Project/Go_Test$ sudo mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.24-0ubuntu0.18.04.1 (Ubuntu) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

切换数据库, use mysql

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed

查看mysql下的表单.

mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
| user_info |
+---------------------------+
32 rows in set (0.00 sec)

查看user表

mysql> desc user;

+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(32) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Reload_priv | enum('N','Y') | NO | | N | |
| Shutdown_priv | enum('N','Y') | NO | | N | |
| Process_priv | enum('N','Y') | NO | | N | |
| File_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Show_db_priv | enum('N','Y') | NO | | N | |
| Super_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Lock_tables_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Repl_slave_priv | enum('N','Y') | NO | | N | |
| Repl_client_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Show_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Create_user_priv | enum('N','Y') | NO | | N | |
| Event_priv | enum('N','Y') | NO | | N | |
| Trigger_priv | enum('N','Y') | NO | | N | |
| Create_tablespace_priv | enum('N','Y') | NO | | N | |
| ssl_type | enum('','ANY','X509','SPECIFIED') | NO | | | |
| ssl_cipher | 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,
-> name char(20) not null);
Query OK, 0 rows affected (0.29 sec)

插入两个数据到user_info表中,然后查询此表。

package main

import (
"github.com/astaxie/beego/logs"
"database/sql"
_ "github.com/go-sql-driver/mysql"
) func main() {
logs.Debug("main()")
db, err := sql.Open("mysql", "ailumiyana:qwedsa@tcp(127.0.0.1:3306)/mysql")
if err != nil {
logs.Error("sql Open() err", err)
} stmt, err := db.Prepare("Insert user_info set id=?,name=?")
if err != nil {
logs.Error("sql Prepare() err", err)
} stmt.Exec(1, "sola")
stmt.Exec(2, "ailumiyana") rows, err :=db.Query("SELECT * FROM user_info")
if err != nil {
logs.Error("sql Query() err", err)
} for rows.Next() {
var uid int
var username string err = rows.Scan(&uid, &username)
if err != nil {
logs.Error("sql rows.Scan() err", err)
}
logs.Debug(uid, username)
} }

其中sql.Open中的 第二个从参数是DSN格式数据 :

DSN(数据源名称)

数据源名称有一种常见的格式,例如PEAR DB使用的它,但是没有类型前缀(可选部分用方括号标记):

DSN的完整形式:

另外此例用了beego 的日志模块,和go-sql-driver驱动,使用前需要先用go get 下载。

go get可以根据要求和实际情况从互联网上下载或更新指定的代码包及其依赖包,并对它们进行编译和安装。在上面这个示例中,我们从著名的代码托管站点Github上下载了一个项目(或称代码包),并安装到了环境变量GOPATH中包含的第一个工作区中。

结果

ailumiyana@ailumiyana:~/Git_Project/Go_Test$ go run mysql.go
2019/01/07 14:38:08.438 [D] main()
2019/01/07 14:38:08.443 [D] 1 sola
2019/01/07 14:38:08.443 [D] 2 ailumiyana
mysql> select * from user_info
-> ;
+----+-------------+
| id | name |
+----+-------------+
| 1 | sola |
| 2 | ailumiyana |
+----+-------------+
2 rows in set (0.02 sec)

Mysql学习(一)添加一个新的用户并用golang操作Mysql的更多相关文章

  1. RK平台Android4.4 添加一个新的遥控器支持以及添加特殊按键【转】

    本文转载自:http://blog.csdn.net/coding__madman/article/details/52904063 版权声明:本文为博主原创文章,未经博主允许不得转载. 瑞芯微平台 ...

  2. Flink资料(6) -- 如何添加一个新的Operator

    false false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTable {mso-style-n ...

  3. 012.Adding a New Field --【添加一个新字段】

    Adding a New Field 添加一个新字段 2016-10-14 3 分钟阅读时长 作者 By Rick Anderson In this section you'll use Entity ...

  4. Linux 在添加一个新账号后却没有权限怎么办

    当添加一个新账号后,我们可能会发现新账号sudo 时会报告不在sudoers中,使用su -s时输入密码后也会认证失败 上网搜索大部分都要求修改/etc/sudoers中的内容,但修改这个文件必须需要 ...

  5. 【IntelliJ IDEA】添加一个新的tomcat,tomcat启动无法访问欢迎页面,空白页,404

    ===================================第一部分,添加一个tomcat================================================== ...

  6. [转帖]golang操作mysql使用总结

    golang操作mysql使用总结 https://www.cnblogs.com/hanyouchun/ 讲解的很详细~ 前言 Golang 提供了database/sql包用于对SQL数据库的访问 ...

  7. golang操作mysql数据库

    golang操作mysql数据库 代码: mysql的增.删.改.查 package main import ( "database/sql" "fmt" &q ...

  8. linux采用模块方法,添加一个新的设备

    该文转载自:http://rangercyh.blog.51cto.com/1444712/521244 系统调用是操作系统内核和应用程序之间的接口,而设备驱动程序是操作系统内核和机器硬件之间的接口. ...

  9. Android4.0 添加一个新的Android 键值

    这里添加新的键值,不是毫无凭据凭空创造的一个键值,而是根据kernel中检测到的按键值,然后转化为Android所需要的数值: 以添加一个Linux键值为217,把它映射为android的键值Brow ...

随机推荐

  1. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(12)-系统日志和异常的处理②

    系列目录 上一讲我们做了日志与异常的结果显示列表,这一节我们讲要把他应用系统中来. 首先我们在App.Common类库中创建一个通用类ResultHelper,这个类里面写了,获取一个GUID,获取当 ...

  2. 【HDU 4352】 XHXJ's LIS (数位DP+状态压缩+LIS)

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. ops

    consists several key projects separately stand-alone connected entities massive scalability massive ...

  4. Python内置函数(32)——all

    英文文档: all(iterable) Return True if all elements of the iterable are true (or if the iterable is empt ...

  5. 【webpack学习笔记】a08-缓存

    一般要优化网站的加载速度可以从缓存入手,利用缓存可以降低网络流量,减少访问请求,从而可以达到加快访问速度的目的. 但是,如果一旦将文件缓存,那么有更新的时候就不能及时更新,若要看到最新内容,那么就需要 ...

  6. C# 自动程序 windows 无法启动 XXXX 服务 错误5 拒绝访问

    遇到过两次 这样的问题了,所以记录一下 原因可能是服务所在文件的目录权限不够 解决方法: 1是查看服务对应的程序所在的目录 2是设置目录的安全权限 右击–属性–安全–添加相应的帐号,给予除完全控制外的 ...

  7. R语言与机器学习学习笔记

    人工神经网络(ANN),简称神经网络,是一种模仿生物神经网络的结构和功能的数学模型或计算模型.神经网络由大量的人工神经元联结进行计算.大多数情况下人工神经网络能在外界信息的基础上改变内部结构,是一种自 ...

  8. STM32 3.3V参考电压 TL431/MC1403/LM385

    TL431作为一个高性价比的常用分流式电压基准,有很广泛的用途. 图(1)是TL431的典型接法,输出一个固定电压值,计算公式是: Vout = ( (R1+R2) / R2 ) * 2.5 V 同时 ...

  9. jQuery时间轴

    常见的时间轴导航 横向时间轴

  10. xheditor

    完整按钮表 |:分隔符 /:强制换行 Cut:剪切 Copy:复制 Paste:粘贴 Pastetext:文本粘贴 Blocktag:段落标签 Fontface:字体 FontSize:字体大小 Bo ...