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

测试连接:

相关推荐
玄斎7 小时前
MySQL 单表操作通关指南:建库 / 建表 / 插入 / 增删改查
运维·服务器·数据库·学习·程序人生·mysql·oracle
编程小Y8 小时前
MySQL 与 MCP 集成全解析(核心原理 + 实战步骤 + 应用场景)
数据库·mysql·adb
lvbinemail9 小时前
Grafana模板自动复制图表
数据库·mysql·zabbix·grafana·监控
weixin_448119949 小时前
Datawhale Hello-Agents入门篇202512第1次作业
数据库·sql·mysql
皮皮林5519 小时前
有了开源的 MySQL,为什么还要选择 PostgreSQL?
mysql
廋到被风吹走11 小时前
【数据库】【MySQL】分库分表策略 分类、优势与短板
数据库·mysql·分类
五阿哥永琪12 小时前
MySQL 慢查询定位与 SQL 性能优化实战指南
sql·mysql·性能优化
WangHappy13 小时前
出海不愁!用Vue3 + Node.js + Stripe实现全球支付
前端·node.js
该用户已不存在13 小时前
Node.js后端开发必不可少的7个核心库
javascript·后端·node.js
LYFlied14 小时前
TypeScript 常见面试问题
ubuntu·面试·typescript