Mysql 命令行 连接远程/本地数据库

这篇具有很好参考价值的文章主要介绍了Mysql 命令行 连接远程/本地数据库。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Mysql 命令行 连接远程/本地数据库

Mysql 命令行 连接本地数据库

  • MySQL登录
    1. mysql -uroot -p密码
    2. mysql -hip -uroot -p连接目标的密码
    3. mysql --host=ip --user=root --password=连接目标的密码
C:\Users\Administrator>mysql -h 127.0.0.1 -uroot --port=3306 -p                                                                                                      
Enter password: *****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.17 MySQL Community Server (GPL)      

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.  

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye

C:\Users\Administrator>mysql -h 127.0.0.1 -uroot --port=3308 -p                                                                                                      
Enter password: *****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.5.61 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.  

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit 
Bye

C:\Users\Administrator>mysql -h 127.0.0.1 -uroot --port=3307 -p                                                                                                      
Enter password: *****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.13 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye

Mysql 命令行 连接远程数据库

  • 连接 远程的数据库
  • mysql --host=ip --user=root --password=连接目标的密码
┌──(root㉿kali)-[~]
└─# mysql -h 69.45.123.1 -uroot --port=3307 -p
Enter password: 
ERROR 1130 (HY000): Host '69.45.123.128' is not allowed to connect to this MySQL server
  • 有两个方法
  • 如果 你的 mysql 数据库没有 密码 最好创建一个一密码
update mysql.user set authentication_string=password('新密码') where user='用户名' and Host ='localhost';
update mysql.user set authentication_string=password('admin') where user='用户名' and Host ='localhost';
1.改表法
  • 是因为 root 帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入mysql后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从"localhost"改为"%"
C:\Users\Administrator>mysql -uroot -p -P3306
Enter password: *****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.17 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql; # 使用数据库
Database changed

mysql> select user,password,host from user where user='root'; # 先查询下 有权限的 用户 的 host 是什么
+------+-------------------------------------------+-----------+
| user | password                                  | host      |
+------+-------------------------------------------+-----------+
| root | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | localhost |
| root | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | 127.0.0.1 |
| root | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | ::1       |
+------+-------------------------------------------+-----------+
3 rows in set (0.00 sec)

# 修改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"localhost"改为"%"

mysql> update user set host = '%' where user = 'root' and host='localhost'; 
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql> flush privileges; # 刷新一下 mysql的缓存
Query OK, 0 rows affected (0.00 sec)
  • 执行操作之后 重启服务器
  • 如果有需要 改回来 在使用 数据库之后 把 “mysql” 数据库里的 “user” 表里的 “host” 项 从"%“改为"localhost” 之后刷新一下缓存之后 重启 mysql 服务 即可
mysql> use mysql; # 使用数据库
Database changed

mysql> update user set host = 'localhost' where user = 'root' and host='%'; 
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql> flush privileges; # 刷新一下 mysql的缓存
Query OK, 0 rows affected (0.00 sec)
2. 授权法。例如,你想 某个用户名 比如 myuser 和对应的密码 从任何主机连接到mysql服务器的话。
/*myuser mypassword 为对应的 用户迷宫和密码 */
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;

如果你想允许用户myuser从ip为192.168.1.3的主机连接到mysql服务器,并使用mypassword作为密码文章来源地址https://www.toymoban.com/news/detail-468793.html

/*例如 */
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.3' IDENTIFIED BY '1235' WITH GRANT OPTION;
mysql> flush privileges; # 刷新一下 mysql的缓存
Query OK, 0 rows affected (0.00 sec)
  • 取消授权
# 查看授权的所有用户
mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;  
+---------------------------+
| query                     |
+---------------------------+
| User: 'root'@'127.0.0.1'; |
| User: 'root'@'::1';       |
| User: ''@'localhost';     |
| User: 'root'@'localhost'; |
+---------------------------+
4 rows in set (0.00 sec)

# revoke all on *.* from 'username'@'%';  username为指定的用户,%为任意登录的地址。
mysql> revoke all on *.* from 'root'@'192.168.1.3';     
Query OK, 0 rows affected (0.00 sec)

# 然后再次 
mysql> flush privileges; # 刷新一下 mysql的缓存
Query OK, 0 rows affected (0.00 sec)
  • 然后 重启 mysql 服务

到了这里,关于Mysql 命令行 连接远程/本地数据库的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 公网环境使用navicat图形化工具远程连接本地MariaDB数据库

    本篇教程将使用cpolar内网穿透本地MariaDB数据库,并实现在外公网环境下使用navicat图形化工具远程连接本地内网的MariaDB数据库。 1. 配置MariaDB数据库 1.1 安装MariaDB数据库 进入MariaDB数据库官网https://mariadb.com/downloads/community/,然后下载相应的windows版本 下载好后点击安装,出现设置

    2024年02月04日
    浏览(15)
  • MariaDB数据库本地部署结合cpolar内网穿透实现远程连接

    本篇教程将使用cpolar内网穿透本地MariaDB数据库,并实现在外公网环境下使用navicat图形化工具远程连接本地内网的MariaDB数据库。 1. 配置MariaDB数据库 1.1 安装MariaDB数据库 进入MariaDB数据库官网https://mariadb.com/downloads/community/,然后下载相应的windows版本 下载好后点击安装,出现设置

    2024年02月03日
    浏览(15)
  • 如何搭建MariaDB并实现无公网ip环境远程连接本地数据库

    🌈个人主页: Aileen_0v0 🔥热门专栏: 华为鸿蒙系统学习|计算机网络|数据结构与算法 ​ 💫个人格言:“没有罗马,那就自己创造罗马~” 本篇教程将使用cpolar内网穿透本地MariaDB数据库,并实现在外公网环境下使用navicat图形化工具远程连接本地内网的MariaDB数据库。 1. 配置MariaD

    2024年01月22日
    浏览(15)
  • 如何在Linux用Docker部署MySQL数据库并远程访问本地数据库

    本文主要介绍如何使用Docker部署MySQL,并结合cpolar内网穿透工具实现远程访问本地数据库。 Docker提供了一个轻量级的容器化解决方案,可以更好的简化数据库的部署过程。让创建和管理MySQL数据库变得更简单快捷。下面就来分享一下具体的部署过程,并提出一些需要注意的事

    2024年03月10日
    浏览(11)
  • Navicat 连接远程数据库 Postgresql、MySQL

    不管什么数据库,只要用Navicat连接远程,下面的方法均奏效。 环境: 服务器:远程服务器 操作系统: : linux 数据库:PostgreSQL14 数据库客户端:Navicat 主要分为两步: 第一步:点击Navicat左上角的“连接”,选择Postgresql。先连接SSH服务器。  勾选“使用SSH通道”,在下面输

    2024年02月05日
    浏览(22)
  • Windows远程连接linux中mysql数据库

     我没有mysql并且没有把mysql配置到环境变量中,所以现在我要下载mysql Mysql官网下载地址:https://downloads.mysql.com/archives/installer 1. 选择设置类型 双击运行mysql-installer-community-8.0.26.msi,这里选择是开发者默认模式,所以直接选择“Developer Default”,点击“Next”      默认3306的端

    2024年02月12日
    浏览(13)
  • 微信小程序开发之连接本地MYSQL数据库

    1)下载安装Node.js 网址:https://nodejs.org/en 右边是长期维护版本,左边是尝鲜版,推荐下载长期维护版本 2)安装完成后本地创建文件夹,文件夹名字随便,我的文件夹名称是nodeMysqlDemo 3)打开命令行 搜索node,用管理员身份打开node.js command prompt 4)进入D盘,进入刚创建的

    2024年02月12日
    浏览(20)
  • 本地MySQL数据库允许用任意ip连接访问

    1、进入本地mysql,输入下面命令,然后输入密码 mysql -uroot -proot 2、进入到mysql。然后选择mysql这个数据库 use mysql 3、 找到user这个表,然后查询一下里面的user和host字段   select user,host from user; 看到里面有一个root的用户对应的host值是一个localhost或者是127.0.0.1。 接下来要把这个

    2023年04月08日
    浏览(18)
  • 无公网IP,外网远程连接MySQL数据库

    哈喽~大家好,这篇来看看无公网IP,外网远程连接MySQL数据库。 作为网站运行必备组件之一的数据库,免不了随时对其进行管理维护。若我们没有在安装数据库的电脑旁,但又需要立即对数据库进行管理时,应该如何处理?这时我们可以使用cpolar对内网进行穿透,远程管理和

    2023年04月19日
    浏览(23)
  • 解决MySQL数据库拒绝远程计算机连接问题

    错误信息:Host is not allowed to connect to this mysql server 以前MySQL数据库部署在云服务器上,程序服务端也部署在云服务器上,连接服务器从没出现过问题。最近有一次需要做一个完全局域网的环境部署,我把数据库和程序服务端部署在里两台不同的电脑上,访问数据库的时候出现

    2024年02月09日
    浏览(12)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包