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

测试连接:

相关推荐
SendTomo2 小时前
【技术交流】从0到1构建一个汉字学习平台:汉字吧(hanzi8.com)的技术架构猜想
redis·mysql·nginx·php·个人开发
IT大白鼠2 小时前
MySQL 分布式集群系列 · 第四篇——实操部署指南:从零搭建生产级 MySQL NDB 集群
数据库·分布式·mysql
IT大白鼠2 小时前
MySQL 分布式集群系列 · 第三篇——核心原理精讲:NDB 自动分片、多主写入与数据同步
数据库·分布式·mysql
初願致夕霞4 小时前
MySQL_索引
数据库·mysql
一只小李郁vickie6 小时前
mysql 开启压缩传输3402条数据2.1秒压缩到毫秒级
数据库·mysql
前端兰博7 小时前
04-数据库-MySQL
后端·mysql
这个DBA有点耶7 小时前
COUNT慢不是因为用了*,是这5个原因——1000万行数据实测+执行计划深度解析
数据库·mysql·算法
kyrie_sakura7 小时前
MySQL数据库学习笔记2--系统函数(分组,单行,窗口函数)
数据库·学习·mysql
大牧师8 小时前
TypeORM 入门教程
后端·sql·mysql·orm·nest·typeorm
oliver_sys_log8 小时前
绕过 Yearning 查询体验限制:我做了一个 DataGrip 只读 SQL 代理
后端·mysql