Mysql命令行連接遠(yuǎn)程/本地?cái)?shù)據(jù)庫詳解
Mysql 命令行 連接本地?cái)?shù)據(jù)庫
MySQL登錄
- mysql -uroot -p密碼
- mysql -hip -uroot -p連接目標(biāo)的密碼
- mysql --host=ip --user=root --password=連接目標(biāo)的密碼
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 命令行 連接遠(yuǎn)程數(shù)據(jù)庫
連接 遠(yuǎn)程的數(shù)據(jù)庫
mysql --host=ip --user=root --password=連接目標(biāo)的密碼
┌──(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
有兩個(gè)方法
如果 你的 mysql 數(shù)據(jù)庫沒有 密碼 最好創(chuàng)建一個(gè)一密碼
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.改表法
是因?yàn)?root 帳號不允許從遠(yuǎn)程登陸,只能在localhost。這個(gè)時(shí)候只要在localhost的那臺電腦,登入mysql后,更改 “mysql” 數(shù)據(jù)庫里的 “user” 表里的 “host” 項(xiàng),從"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; # 使用數(shù)據(jù)庫 Database changed mysql> select user,password,host from user where user='root'; # 先查詢下 有權(quán)限的 用戶 的 host 是什么 +------+-------------------------------------------+-----------+ | user | password | host | +------+-------------------------------------------+-----------+ | root | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | localhost | | root | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | 127.0.0.1 | | root | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | ::1 | +------+-------------------------------------------+-----------+ 3 rows in set (0.00 sec) # 修改 "mysql" 數(shù)據(jù)庫里的 "user" 表里的 "host" 項(xiàng),從"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)
執(zhí)行操作之后 重啟服務(wù)器
如果有需要 改回來 在使用 數(shù)據(jù)庫之后 把 “mysql” 數(shù)據(jù)庫里的 “user” 表里的 “host” 項(xiàng) 從"%“改為"localhost” 之后刷新一下緩存之后 重啟 mysql 服務(wù) 即可
mysql> use mysql; # 使用數(shù)據(jù)庫 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. 授權(quán)法。例如,你想 某個(gè)用戶名 比如 myuser 和對應(yīng)的密碼 從任何主機(jī)連接到mysql服務(wù)器的話。
/*myuser mypassword 為對應(yīng)的 用戶迷宮和密碼 */ GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
如果你想允許用戶myuser從ip為192.168.1.3的主機(jī)連接到mysql服務(wù)器,并使用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)
取消授權(quán)
# 查看授權(quán)的所有用戶 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 服務(wù)
總結(jié)
到此這篇關(guān)于Mysql命令行連接遠(yuǎn)程/本地?cái)?shù)據(jù)庫的文章就介紹到這了,更多相關(guān)Mysql命令行連接數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL Shell import_table數(shù)據(jù)導(dǎo)入的實(shí)現(xiàn)
這篇文章主要介紹了MySQL Shell import_table數(shù)據(jù)導(dǎo)入的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08淺析刪除表的幾種方法(delete、drop、truncate)
這篇文章主要介紹了刪除表的幾種方法,需要的朋友可以參考下2014-05-05MySQL數(shù)據(jù)庫查詢性能優(yōu)化策略
這篇文章主要介紹了MySQL數(shù)據(jù)庫查詢性能優(yōu)化的策略,幫助大家的工作學(xué)習(xí)提高M(jìn)ySQL數(shù)據(jù)庫的性能,感興趣的朋友可以了解下2020-08-08MySQL修改數(shù)據(jù)表存儲引擎的3種方法介紹
這篇文章主要介紹了MySQL修改數(shù)據(jù)表存儲引擎的3種方法介紹,分別是直接修改、導(dǎo)出導(dǎo)入、創(chuàng)建插入3種方法,需要的朋友可以參考下2014-07-07Servermanager啟動連接數(shù)據(jù)庫錯(cuò)誤如何解決
這篇文章主要介紹了Servermanager啟動連接數(shù)據(jù)庫錯(cuò)誤如何解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10一文學(xué)習(xí)MySQL?意向共享鎖、意向排他鎖、死鎖
這篇文章主要介紹了MySQL?意向共享鎖、意向排他鎖、死鎖,包括InnoDB表級鎖,意向共享鎖和意向排他鎖及操作方法,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03