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

 更新时间:2023年05月05日 11:45:02   作者:小浪zoom  
新使用MySQL,说起来是个简单的事情,,但是却费了些周折,下面这篇文章主要给大家介绍了关于Mysql命令行连接远程/本地数据库的相关资料,文中介绍的非常详细,需要的朋友可以参考下

Mysql 命令行 连接本地数据库

MySQL登录

  • mysql -uroot -p密码
  • mysql -hip -uroot -p连接目标的密码
  • 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作为密码

/*例如 */
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命令行连接远程/本地数据库的文章就介绍到这了,更多相关Mysql命令行连接数据库内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • linux下讲解MySQL安装与登录方法

    linux下讲解MySQL安装与登录方法

    MySQL安装文件已被广泛应用但是也在不断的更新,这里介绍MySQL安装文件设置使用,帮助大家安装更新MySQL安装文件系统。
    2010-11-11
  • 浅谈Mysql主键索引与非主键索引区别

    浅谈Mysql主键索引与非主键索引区别

    本文主要介绍了ysql主键索引与非主键索引区别,文中介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-09-09
  • MySQL死锁日志的实例分析技巧总结

    MySQL死锁日志的实例分析技巧总结

    MySQL死锁是线上经常遇到的现象,MySQL死锁日志分析方法有助于研发快速提取信息,提高分析效率,通过了解死锁触发条件、检测机制及锁类型,结合日志分析工具,可以更有效地解决死锁问题
    2024-11-11
  • MySQL配置文件my.cnf与my.ini的区别

    MySQL配置文件my.cnf与my.ini的区别

    在使用MySQL时,我们需要对其进行配置,以满足我们的需求,本文主要介绍了MySQL配置文件my.cnf与my.ini的区别,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • 多次执行mysql_fetch_array()的指针归位问题探讨

    多次执行mysql_fetch_array()的指针归位问题探讨

    多次执行mysql_fetch_array(),在第二次执行的时候,如果不加处理,就不会输出任何内容,这种情况下只需要对循环指针进行复位即可,感兴趣的朋友可以了解下啊,或许对你有所帮助
    2013-01-01
  • 同时运行多个MySQL服务器的方法

    同时运行多个MySQL服务器的方法

    在同一台机器上运行多个有些情况下你可能想要在同一台机器上运行多个服务器。例如,你可能想要测试一个新的MySQL版本而让你现有生产系统的设置不受到干扰, 或你可能是想要为不同的客户提供独立的MySQL安装一个因特网服务供应商。
    2008-05-05
  • MySQL中使用distinct单、多字段去重方法

    MySQL中使用distinct单、多字段去重方法

    多个字段拼接去重是指将多个字段的值按照一定的规则进行拼接,并去除重复的拼接结果,本文主要介绍了MySQL中使用distinct单、多字段去重方法,感兴趣的可以了解一下
    2024-05-05
  • 教你解决往mysql数据库中存入汉字报错的方法

    教你解决往mysql数据库中存入汉字报错的方法

    这篇文章主要介绍了Mysql基础之教你解决往数据库中存入汉字报错的方法,文中有非常详细的代码示例,对正在学习mysql的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-05-05
  • MySQL库表名大小写的选择

    MySQL库表名大小写的选择

    一般在数据库使用规范中,我们都会看到这么一条:库名及表名一律使用小写英文。你有没有思考过,为什么推荐使用小写呢?库表名是否应该区分大小写呢?带着这些疑问,我们一起来看下本篇文章。
    2021-06-06
  • ORM模型框架操作mysql数据库的方法

    ORM模型框架操作mysql数据库的方法

    ORM 全称是(Object Relational Mapping)表示对象关系映射; 通俗理解可以理解为编程语言的虚拟数据库;这篇文章主要介绍了ORM模型框架操作mysql数据库的方法,需要的朋友可以参考下
    2021-07-07

最新评论