MySQL 连接方式
TCP/IP 连接
# TCP/IP 连接
mysql -uroot -p -h127.0.0.1
mysql -uroot -p -h127.0.0.1 -S /tmp/mysql.sock
# 使用命令查看连接方式
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.6.46, for linux-glibc2.12 (x86_64) using EditLine wrapper
Connection id: 10
Current database:
Current user: root@127.0.0.1
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.6.46-log MySQL Community Server (GPL)
Protocol version: 10
Connection: 127.0.0.1 via TCP/IP
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
TCP port: 3307
Uptime: 4 min 19 sec
Threads: 1 Questions: 13 Slow queries: 0 Opens: 70 Flush tables: 1 Open tables: 63 Queries per second avg: 0.050
--------------
mysql>
Socket 连接
# Socket 连接
mysql -uroot -p -hlocalhost
mysql -uroot -p
# 使用命令查看连接方式
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.6.46, for linux-glibc2.12 (x86_64) using EditLine wrapper
Connection id: 8
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.6.46-log MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /service/3307/mysql.sock
Uptime: 3 min 49 sec
Threads: 1 Questions: 8 Slow queries: 0 Opens: 70 Flush tables: 1 Open tables: 63 Queries per second avg: 0.034
--------------
mysql> Ctrl-C -- exit!
# 注意:
1)不一定 -h 就是 TCP/IP 连接,如果是 -h localhost 就是 Socket 连接
2)socket 连接比 TCP/IP 连接快,TCP/IP 连接需要经过三次握手,四次挥手,速度慢
3)socket 连接只能本地连接,远程不能使用
MySQL 连接工具
自带连接工具
mysql 命令
# mysql 常用的参数
-u: # 指定用户 mysql -uroot
-p: # 指定用户的密码 mysql -uroot -p123
-h: # 指定连接的主机 mysql -uroot -p123 -h172.16.1.51
-S: # 指定socket文件 mysql -uroot -p123 -S /tmp/mysql.sock
-P: # 指定端口 mysql -uroot -p123 -P3307
-e: # 库外执行sql语句 mysql -uroot -p123 -e 'show databases'
第三方连接工具
- Navicat
- SQLyog