Java 如何找出我的 MySQL URL、主机、端口和用户名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4093603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:32:15  来源:igfitidea点击:

How do I find out my MySQL URL, host, port and username?

javamysqldatabasejdbcdatabase-connection

提问by

I need to find my MySQL username. When I open the MySQL command line client, it only asks me for my password. I don't remember my username. And for connectivity with JDBC, I need the URL, host and port number. Where do I find all of these?

我需要找到我的 MySQL 用户名。当我打开 MySQL 命令行客户端时,它只要求我输入密码。我不记得我的用户名了。为了与 JDBC 连接,我需要 URL、主机和端口号。我在哪里可以找到所有这些?

采纳答案by Asaph

If you're already logged into the command line client try this:

如果您已经登录到命令行客户端,请尝试以下操作:

mysql> select user();

It will output something similar to this:

它将输出类似于以下内容:

+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.41 sec)

In my example above, I was logged in as rootfrom localhost.

在上面的示例中,我是rootlocalhost.

To find port number and other interesting settings use this command:

要查找端口号和其他有趣的设置,请使用以下命令:

mysql> show variables;

回答by Adeel Ansari

default-username = root
password = you-know-it-better
url for localhost =  jdbc:mysql://localhost
default-port = 3306

回答by Nikhil Agrawal

If you want to know the port number of your local host on which Mysqlis running you can use this query on MySQL Command line client --

如果您想知道运行Mysql 的本地主机端口号,您可以在 MySQL 命令行客户端上使用此查询——

SHOW VARIABLES WHERE Variable_name = 'port';


mysql> SHOW VARIABLES WHERE Variable_name = 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)

It will give you the port number on which MySQL is running.

它将为您提供运行 MySQL 的端口号。



If you want to know the hostname of your Mysqlyou can use this query on MySQL Command line client --

如果你想知道你的 Mysql主机名,你可以在 MySQL 命令行客户端上使用这个查询——

SHOW VARIABLES WHERE Variable_name = 'hostname';


mysql> SHOW VARIABLES WHERE Variable_name = 'hostname';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| hostname          | Dell  |
+-------------------+-------+
1 row in set (0.00 sec)

It will give you the hostname for mysql.

它将为您提供 mysql 的主机名。



If you want to know the username of your Mysqlyou can use this query on MySQL Command line client --

如果你想知道你的 Mysql用户名,你可以在 MySQL 命令行客户端上使用这个查询——

select user();   


mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

It will give you the username for mysql.

它将为您提供 mysql 的用户名。

回答by TheKarateKid

If you use phpMyAdmin, click on Home, then Variableson the top menu. Look for the portsetting on the page. The value it is set to is the port your MySQL server is running on.

如果您使用 phpMyAdmin,请单击Home,然后单击Variables顶部菜单。port在页面上查找设置。它设置的值是您的 MySQL 服务器运行的端口。

回答by Laxman G

mysql> SHOW VARIABLES WHERE Variable_name = 'hostname';
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| hostname      | karola-pc |
+---------------+-----------+
1 row in set (0.00 sec)

For Example in my case : karola-pcis the host name of the box where my mysql is running. And it my local PC host name.

例如,在我的情况下:karola-pc是运行我的 mysql 的机器的主机名。它是我的本地 PC 主机名。

If it is romote box than you can ping that host directly if, If you are in network with that box you should be able to ping that host.

如果它是远程盒子,那么您可以直接 ping 那个主机,如果您与那个盒子在网络中,您应该能够 ping 那个主机。

If it UNIX or Linux you can run "hostname" commandin terminal to check the host name. if it is windows you can see same value in MyComputer-> right click -> properties ->Computer Nameyou can see ( i.e System Properties)

如果它UNIX or Linux you can run "hostname" command在终端检查主机名。如果是 Windows,您可以看到相同的值MyComputer-> right click -> properties ->Computer Name(即系统属性)

Hope it will answer your Q.

希望它能回答你的问题。

回答by Dmitry Matrosov

For example, you can try:

例如,您可以尝试:

//If you want to get user, you need start query in your mysql:
SELECT user(); // output your user: root@localhost
SELECT system_user(); // --

//If you want to get port your "mysql://user:pass@hostname:port/db"
SELECT @@port; //3306 is default

//If you want hostname your db, you can execute query
SELECT @@hostname;

回答by Inzimam Tariq IT

Here are the default settings

这是默认设置

 default-username is root
default-password is null
default-url is localhost or 127.0.0.1 for apache and     
       localhost:/phpmyadmin for mysql           // if you are using xampp
default-port = 3306

回答by Mallikarjun Pasunkili

If you don't know the exact variable name use like, as the result may contain more than 500 rows:

如果您不知道确切的变量名称,请使用like,因为结果可能包含 500 多行:

mysql> show variables like "%port%";

回答by Jacob Stamm

If using MySQL Workbench, simply look in the Session tab in the Information pane located in the sidebar.

如果使用 MySQL Workbench,只需查看侧边栏中信息窗格中的会话选项卡。

enter image description here

在此处输入图片说明

回答by Igor Lopatka

Easiest way is probably using command status;In the output you'll find database, user, host and port:

最简单的方法可能是使用命令状态;在输出中,您将找到数据库、用户、主机和端口:

mysql> status;
--------------
mysql  Ver 8.0.13 for Win64 on x86_64 (MySQL Community Server - GPL)

Connection id:          43
Current database:       mysql
Current user:           user@localhost
SSL:                    Cipher in use is DHE-RSA-AES128-GCM-SHA256
Using delimiter:        ;
Server version:         8.0.13 MySQL Community Server - GPL
Protocol version:       10
Connection:             localhost via TCP/IP
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    cp852
Conn.  characterset:    cp852
TCP port:               3306
Uptime:                 3 hours 32 min 40 sec

Threads: 3  Questions: 197  Slow queries: 0  Opens: 214  Flush tables: 2  Open tables: 190  Queries per second avg: 0.015
--------------