允许root远程连接数据库

开放root远程连接数据库的权限(Linux系统)

环境:centos7,关闭防火墙(没关要开放数据库的端口)
一、进入数据库,查看权限表信息

MariaDB [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> select Host,User from user;
+-----------+------+
| Host      | User 
允许 MariaDB 10.6 的 root 用户进行远程连接,可按以下步骤操作: 1. **启动并设置开机自启**:安装完成 MariaDB 后,启动 MariaDB 并设置开机启动,可以使用以下命令: ```bash systemctl enable --now mariadb ``` 该命令等同于先启动 MariaDB `systemctl start mariadb`,再设置开机启动 `systemctl enable mariadb` [^1]。 2. **修改 root 密码**:默认情况下,root 密码为空,使用以下命令修改密码: ```bash mysqladmin -u root password 'Root123456!2' ``` 将 `Root123456!2` 替换为你想设置的密码 [^1]。 3. **配置允许远程访问**:使用以下命令配置允许 root 用户从任意地址远程访问: ```bash mysql<<'EOF' grant all privileges on *.* to 'root'@'%' identified by "Root123456!2"; flush privileges; EOF ``` 这里将 `Root123456!2` 替换为你设置的密码。`'root'@'%'` 表示 root 用户可以从任意地址访问,`grant all privileges on *.*` 表示授予该用户对所有数据库和表的所有权限,`flush privileges` 用于刷新权限使设置生效 [^1]。 4. **开启服务器端口**:记得去服务提供商开启 3306 端口允许远程访问,否则远程登录会失败。一般线上服务器不使用 3306 端口,若要配置自定义端口,可编辑 `/etc/my.cnf` 文件,配置好端口后,使用 `systemctl restart mariadb` 重新启动 MariaDB 服务 [^2]。 5. **测试远程连接**:使用以下命令测试远程连接: ```bash mysql -h 192.168.2.14 -P3306 -uroot -p'Root123456!2' ``` 将 `192.168.2.14` 替换为服务器的 IP 地址,`3306` 替换为实际使用的端口,`Root123456!2` 替换为设置的密码 [^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值