Mysql root用户远程连接失败解决方案

最近,踩坑云服务器通过root用户远程连接Mysql数据库失败,Mysql 版本为 5.7.44,原因如下,因为root用户权限过大,可能会有风险操作,可以新增其他用户来解决此问题,如果一定要用root用户,必须给其设置密码并且赋予远程访问权限

启动数据库
ssh 复制代码
systemctl start mysql
连接数据库,空密码
ssh 复制代码
mysql -u root -p
查看数据库用户列表
ssh 复制代码
use mysql;
select user,host from user;


localhost代表只能本地访问,%代表无限制访问

修改 root 密码,权限,5.7版本之后的密码为authentication_string
ssh 复制代码
update user set host = '%' where user = 'root';
update user set authentication_string = '123456' where user = 'root';
刷新
ssh 复制代码
flush privileges
重启数据库
ssh 复制代码
systemctl restart mysql

测试下连接数据库
db.config.yaml

javascript 复制代码
db:
  user: root
  password: '******'
  host: 49.232.141.80
  port: 3306
  database: chat

db.ts

javascript 复制代码
import {injectable} from "inversify"
import knex from "knex"
import fs from "node:fs";
import path from "node:path";
import jsyaml from 'js-yaml'

const yaml = fs.readFileSync(path.resolve(__dirname, '../../db.config.yaml'), 'utf8')
const config = jsyaml.load(yaml)

@injectable()
export class DB {
    database: any
    constructor() {
        this.database = null
        this.init()
    }
    public init() {
        this.database = knex({
            client: 'mysql',
            connection: config.db
        })
    }

    public createUserSchema() {
        // 创建用户表
        this.database.schema.createTableIfNotExists('user', table => {
            table.increments('id') // 自增ID
            table.string('name')
            table.string('email')
            table.string('phone')
            table.integer('age')
            table.string('avatar')
            table.timestamps(true, true) // 创建时间、更新时间
        })
            .then(res => {
                console.log('创建用户表成功!')
            })
    }
}

测试连接:

相关推荐
2601_962065252 小时前
[MySQL] SQL优化之性能分析
java·sql·mysql
yi.Ist2 小时前
数据定义语言-DDL操作
数据库·学习·mysql·oracle·大海豚
阿kun要赚马内2 小时前
MySQL 索引基础
后端·mysql
2601_962177134 小时前
【MySQL篇】聚合查询,联合查询
android·java·mysql
云贝教育-郑老师4 小时前
MySQL 的“黑匣子“:Performance Schema 把数据库内部变成一张可查的表
数据库·mysql
青春之我_XP6 小时前
MySQL 常用日期函数 实战指南
数据库·sql·mysql·数据分析·数据库开发·日期函数
麻瓜code8 小时前
【MySQL】SQL优化实战:从慢查询定位到查询改写
数据库·sql·mysql
BillKu8 小时前
TypeScript中,字符串字面量联合类型(Union Type)、enum的用法说明
前端·javascript·typescript
这个DBA有点耶9 小时前
RANGE/LIST/HASH/二级分区怎么选?分区表适用边界与隐藏代价
数据库·mysql·dba
还是大剑师兰特10 小时前
MySQL8.0 Windows完整安装教程
windows·mysql·大剑师