Connecting to the MySQL Server

本文详细介绍了MySQL客户端程序连接服务的方法,包括不指定连接参数、指定主机名和用户名密码、避免直接在命令行输入密码、指定IP和协议、配置ipv6、使用TCP/IP连接远程服务器等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

客户端程序连接MySql服务

1. 不指定连接参数

因为没有指定参数,将使用默认值
语法:shell> mysql
默认主机名为:localhost
[root@root ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.6.24 MySQL Community Server (GPL)


Copyright (c) 2000, 2015, 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> 

2. 指定主机名和用户名和密码

语法:
shell> mysql --host=localhost --user=myname --password=mypass mydb
shell> mysql -h localhost -u myname -pmypass mydb

host:主机名  user:登录的用户 password:密码 mydb:访问的数据库


[root@qingmeng001 ~]# mysql --host=localhost --user=chenwx --password=chenwx mysql
Warning: Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 5.6.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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> 

[root@qingmeng001 ~]# mysql -h localhost -u chenwx -pchenwx mysql
Warning: Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 5.6.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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> 

note:使用-p或 --password指定密码,在-p或-password后不能有空格

3.避免因安全问题,指定-p或-password选项不直接跟密码

语法:
shell> mysql --host=localhost --user=myname --password mydb
shell> mysql -h localhost -u myname -p mydb

[root@qingmeng001 ~]# mysql --host=localhost --user=chenwx --password mysql
Enter password:    --输入密码,不显示输入内容及输入个数
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 31
Server version: 5.6.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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> 

4.指定IP和协议

语法:
shell> mysql --host=127.0.0.1
shell> mysql --protocol=TCP
--protocol={TCP|SOCKET|PIPE|MEMORY}

在一些unix系统中需要强制指定TCP/IP,否则指定的端口号将呗忽略

5. 配置ipv6

--host=::1.

6. 使用TCP/IP连接到远程服务器

语法:
shell> mysql --host=remote.example.com
默认端口:3306

7.指定端口选项--port -P

语法:
shell> mysql --host=remote.example.com --port=13306

8. windows使用named-pipe连接

语法:
--pipe
--protocol=PIPE


--protocol                
Value            Connection Protocol                                                    Permissible Operating  Systems
TCP                 TCP/IPconnection to local or remote server                                All
SOCKET            Unix socket file connection to local server Unix                          only
PIPE                Named-pipe connection to local or remote serverWindows         only
MEMORY          Shared-memory connection to local serverWindows                     only

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28282660/viewspace-1579988/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28282660/viewspace-1579988/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值