添加一個本身的用戶到mysql
mysql
首先咱們須要先用root用戶登陸mysql,可是剛安裝完沒有密碼,咱們先跳過密碼git
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登錄github
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 mysqlgolang
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下的表單.sql
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中的已存在的用戶和主機ubuntu
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)
接着咱們新增一個本身的帳戶使用bash
mysql> CREATE USER '填用戶名'@'localhost' IDENTIFIED BY '填密碼'; Query OK, 0 rows affected (0.04 sec)
再次查看user表session
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)
給新建的用戶添加權限,刷新權限後, 查看新增用戶的權限是否已經加進去.tcp
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簡單測試一下。
先在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)