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('创建用户表成功!')
            })
    }
}

测试连接:

相关推荐
小石头 100866 分钟前
MySQL 视图:把复杂变简单的“虚拟化”艺术
数据库·mysql
DB虚空行者1 小时前
MySQL误删/批量更新数据恢复实战:基于Flashback工具的完整方案
数据库·mysql
九皇叔叔1 小时前
MySQL Next-Key Lock 锁表事故全拆解(从现象到根治)
数据库·mysql
疯狂踩坑人1 小时前
【Nodejs】Http异步编程从EventEmitter到AsyncIterator和Stream
前端·javascript·node.js
WangHappy2 小时前
面试官:如何优化批量图片上传?队列机制+分片处理+断点续传三连击!
前端·node.js
短剑重铸之日2 小时前
7天读懂MySQL|Day 4:锁与并发控制
数据库·mysql·架构
Java水解2 小时前
MySQL定时任务详解 - Event Scheduler 事件调度器从基础到实战
后端·mysql
岁月宁静2 小时前
AI 多模态全栈项目实战:Vue3 + Node 打造 TTS+ASR 全家桶!
vue.js·人工智能·node.js
爱学习的小可爱卢3 小时前
数据库MySQL——MySQL 可重复读隔离级别:Read View 底层原理与幻读问题深度剖析(面试必知)
数据库·mysql
咖啡の猫4 小时前
TypeScript 开发环境搭建
前端·javascript·typescript