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

测试连接:

相关推荐
合作小小程序员小小店24 分钟前
web网页开发,在线短视频管理系统,基于Idea,html,css,jQuery,java,springboot,mysql。
java·前端·spring boot·mysql·vue·intellij-idea
猿小喵34 分钟前
浅谈MySQL主从复制
数据库·mysql
Dxy12393102161 小时前
MySQL如何修改最大连接数
数据库·mysql
UCoding1 小时前
我们来学mysql -- delete undo log的形成
数据库·mysql·delete的undo log
百***35511 小时前
TypeScript 与后端开发Node.js
javascript·typescript·node.js
q***46521 小时前
最新最详细的配置Node.js环境教程
node.js
TDengine (老段)1 小时前
TDengine 字符串函数 REGEXP_IN_SET 用户手册
数据库·物联网·mysql·时序数据库·tdengine·涛思数据
宇余1 小时前
ES2025新特性实战:5分钟get前端高频实用语法
前端·typescript
jenchoi4131 小时前
【2025-11-15】软件供应链安全日报:最新漏洞预警与投毒预警情报汇总
前端·网络·安全·网络安全·npm·node.js
合作小小程序员小小店2 小时前
web开发,在线%小区,物业%管理系统,基于idea,html,jsp,java,ssm,mysql数据库
java·数据库·mysql·jdk·intellij-idea