解决
Ubuntu
首次安装Mysql
之后,首次登录出现ERROR 1698 (28000): Access denied for user 'root'@'localhost'
的方法解决步骤:
查看
mysql
版本1
2char@senyas:~/GoLand-2021.1.3/bin$ mysql --version
mysql Ver 8.0.26-0 ubuntu0.20.04.2 for Linux on aarch64 ((Ubuntu))打开终端,输入:
1
sudo vi /etc/mysql/debian.cnf
打开
/etc/mysql/debian.cnf
文件,显示如下:1
2
3
4
5
6
7
8
9
10
11# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = HszR9Tt89bS0Pt57
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = HszR9Tt89bS0Pt57
socket = /var/run/mysqld/mysqld.sock打开
mysql
使用如下命令打开
Mysql
:1
mysql -udebian-sys-maint -p
输入密码为上图中
password
字段1
2
3
4
5
6
7
8
9
10
11
12
13
14
15char@senyas:~/GoLand-2021.1.3/bin$ mysql -udebian-sys-maint -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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.
mysql>修改
root
密码1
2mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)刷新并退出
1
2
3
4
5mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye重新登录
输入修改后的密码:1234561
2
3
4
5
6
7
8
9
10
11
12
13
14
15char@senyas:~/GoLand-2021.1.3/bin$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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.
mysql>执行成功
安装
Mysql
按照如下命令,按顺序执行即可完成Mysql
安装:1
2
3
4
5
6
7
8
9
10# 更新软件源
sudo apt-get update
# 安装 mysql 客户端
sudo apt install mysql-client-core-8.0
# 安装 mysql 服务端
sudo apt-get install mysql-server
# 查看是否安装成功
systemctl status mysql
# 登录 mysql
sudo mysql -u root -p定义
Model
和 初始化数据库链接案例1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29package models
import (
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
)
/// 1.结构体,表设计
type UserInfo struct {
Id int
Name string
}
/// 2.初始化语句
func init() {
/// 1.注册表
orm.RegisterModel(new(UserInfo))
/// 2.注册 mysql driver
orm.RegisterDriver("mysql", orm.DRMySQL)
/// 3.链接数据库
orm.RegisterDataBase("default", "mysql", "root:123456@tcp(127.0.0.1:3306)/class1?charset=utf8")
/// 4.生成表
orm.RunSyncdb("default", false, true)
}
Go mysql Tips
坚持原创技术分享,您的支持将鼓励我继续创作!
- 本文链接: https://www.xuebaonline.com/Go mysql Tips/
- 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
欢迎关注我的其它发布渠道