mysql localhost登入和tcp/ip登入
小結:
1 mysql的賬戶名格式為 user@host,host為%時表示匹配任意主機或ip,但是localhost除外,也就是說必須為localhost顯示建立賬號;
2 登入mysql時若指定localhost選項則採用unix socket,而非tcp/ip協議;
以下為驗證過程,
1 localhost VS % 的登入許可權
先建立一個使用者,其host為%,當前資料庫沒有localhost匿名使用者
嘗試以unix socket方式登入,均失敗
另外,即便指定了socket選項,-h127.0.0.1也會強制採用tcp/ip連線登入
建立基於localhost的使用者,
再次以unix socket方式登入,成功
1 mysql的賬戶名格式為 user@host,host為%時表示匹配任意主機或ip,但是localhost除外,也就是說必須為localhost顯示建立賬號;
2 登入mysql時若指定localhost選項則採用unix socket,而非tcp/ip協議;
以下為驗證過程,
1 localhost VS % 的登入許可權
先建立一個使用者,其host為%,當前資料庫沒有localhost匿名使用者
點選(此處)摺疊或開啟
-
mysql> grant select on *.* to 'test1'@'%' identified by 'test1';
-
Query OK, 0 rows affected (0.00 sec)
-
mysql> select host,user,password from mysql.user order by 1 desc ,2;
-
+-----------------------------------------+--------------+-------------------------------------------+
-
| host | user | password |
-
+-----------------------------------------+--------------+-------------------------------------------+
-
| localhost | root | |
-
| ::1 | root | |
-
| 127.0.0.1 | root | |
-
| % | root | *96B86EC8D3F883B0C55D488A951E11B037E74816 |
-
| % | test1 | *06C0BF5B64ECE2F648B5F048A71903906BA08E5C |
-
+-----------------------------------------+--------------+-------------------------------------------+
- 11 rows in set (0.00 sec)
點選(此處)摺疊或開啟
-
[root@mysqlslave3 ~]# /data1-new/app/services/mysql55d/bin/mysql --socket=/data1-new/app/tmp/mysql55d.sock -utest1 -ptest1
-
ERROR 1045 (28000): Access denied for user 'test1'@'localhost' (using password: YES)
-
[root@mysqlslave3 ~]# /data1-new/app/services/mysql55d/bin/mysql -utest1 -ptest1 --socket=/data1-new/app/tmp/mysql55d.sock -hlocalhost
- ERROR 1045 (28000): Access denied for user 'test1'@'localhost' (using password: YES)
點選(此處)摺疊或開啟
-
[root@mysqlslave3 ~]# /data1-new/app/services/mysql55d/bin/mysql --socket=/data1-new/app/tmp/mysql55d.sock -utest1 -ptest1 -h127.0.0.1
-
mysql> status
-
--------------
-
/data1-new/app/services/mysql55d/bin/mysql Ver 14.14 Distrib 5.5.31, for Linux (x86_64) using readline 5.1
-
Connection id:1449940
-
Current database:
-
Current user:test1@127.0.0.1
-
SSL:Not in use
-
Current pager:stdout
-
Using outfile:''
-
Using delimiter:;
-
Server version:5.5.31-log Source distribution
-
Protocol version:10
-
Connection:127.0.0.1 via TCP/IP
-
Server characterset:utf8
-
Db characterset:utf8
-
Client characterset:utf8
-
Conn. characterset:utf8
-
TCP port:3306
-
Uptime:79 days 12 hours 22 min 29 sec
-
-
Threads: 1 Questions: 4746519297 Slow queries: 144823 Opens: 18675 Flush tables: 79 Open tables: 24 Queries per second avg: 690.890
-
-
mysql> select user(),current_user();
-
+-----------------+----------------+
-
| user() | current_user() |
-
+-----------------+----------------+
-
| test1@127.0.0.1 | test1@% |
-
+-----------------+----------------+
- 1 row in set (0.00 sec)
點選(此處)摺疊或開啟
-
mysql> grant select on *.* to 'test1'@'localhost' identified by 'test1';
-
Query OK, 0 rows affected (0.00 sec)
-
mysql> select host,user,password from mysql.user order by 1 desc ,2;
-
+-----------------------------------------+--------------+-------------------------------------------+
-
| host | user | password |
-
+-----------------------------------------+--------------+-------------------------------------------+
-
| localhost | root | |
-
| localhost | test1 | *06C0BF5B64ECE2F648B5F048A71903906BA08E5C |
-
| ::1 | root | |
-
| 127.0.0.1 | root | |
-
| % | root | *96B86EC8D3F883B0C55D488A951E11B037E74816 |
-
| % | test1 | *06C0BF5B64ECE2F648B5F048A71903906BA08E5C |
-
+-----------------------------------------+--------------+-------------------------------------------+
- 11 rows in set (0.00 sec)
再次以unix socket方式登入,成功
點選(此處)摺疊或開啟
-
[root@mysqlslave3 ~]# /data1-new/app/services/mysql55d/bin/mysql -utest1 -ptest1 --socket=/data1-new/app/tmp/mysql55d.sock
-
mysql> status
-
--------------
-
/data1-new/app/services/mysql55d/bin/mysql Ver 14.14 Distrib 5.5.31, for Linux (x86_64) using readline 5.1
-
Connection id:8
-
Current database:
-
Current user:test1@localhost
-
SSL:Not in use
-
Current pager:stdout
-
Using outfile:''
-
Using delimiter:;
-
Server version:5.5.31-log Source distribution
-
Protocol version:10
-
Connection:Localhost via UNIX socket
-
Server characterset:utf8
-
Db characterset:utf8
-
Client characterset:utf8
-
Conn. characterset:utf8
-
UNIX socket:/data1-new/app/tmp/mysql55d.sock
-
Uptime:9 min 44 sec
-
-
Threads: 1 Questions: 87 Slow queries: 4 Opens: 38 Flush tables: 2 Open tables: 2 Queries per second avg: 0.148
-
-
mysql> select current_user(),user();
-
+-----------------+-----------------+
-
| current_user() | user() |
-
+-----------------+-----------------+
-
| test1@localhost | test1@localhost |
-
+-----------------+-----------------+
- 1 row in set (0.00 sec)
2 localhost的登入協議
-
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option. For example:
-
shell> mysql --host=127.0.0.1
-
shell> mysql --protocol=TCP
- http://dev.mysql.com/doc/refman/5.6/en/connecting.htm
當指定-h localhost選項時,採用unix socket方式連線,
點選(此處)摺疊或開啟
-
[root@mysqlslave3 ~]# strace /data1/app/services/mysql55a/bin/mysql --socket=/data1/app/tmp/mysql55a.sock -hlocalhost -utest1 -ptest1
-
execve("/data1/app/services/mysql55a/bin/mysql", ["/data1/app/services/mysql55a/bin"..., "--socket=/data1/app/tmp/mysql55a"..., "-hlocalhost", "-utest1", "-ptest1", "-o", "/tmp/tmp12.log"], [/* 30 vars */]) = 0
-
......
-
stat("/etc/my.cnf", 0x7ffeb7006980) = -1 ENOENT (No such file or directory)
-
stat("/etc/mysql/my.cnf", 0x7ffeb7006980) = -1 ENOENT (No such file or directory)
-
stat("/data1/app/services/mysql55/etc/my.cnf", 0x7ffeb7006980) = -1 ENOENT (No such file or directory)
-
stat("/root/.my.cnf", 0x7ffeb7006980) = -1 ENOENT (No such file or directory)
-
socket(PF_LOCAL, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0) = 3
-
connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
-
close(3) = 0
-
socket(PF_LOCAL, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0) = 3
-
connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
-
close(3) = 0
-
......
-
socket(PF_LOCAL, SOCK_STREAM, 0) = 3
-
fcntl(3, F_SETFL, O_RDONLY) = 0
-
fcntl(3, F_GETFL) = 0x2 (flags O_RDWR)
-
connect(3, {sa_family=AF_LOCAL, sun_path="/data1/app/tmp/mysql55a.sock"}, 110) = 0
-
setsockopt(3, SOL_SOCKET, SO_RCVTIMEO, "\2003\341\1\0\0\0\0\0\0\0\0\0\0\0\0", 16) = 0
-
setsockopt(3, SOL_SOCKET, SO_SNDTIMEO, "\2003\341\1\0\0\0\0\0\0\0\0\0\0\0\0", 16) = 0
-
setsockopt(3, SOL_IP, IP_TOS, [8], 4) = -1 EOPNOTSUPP (Operation not supported)
-
setsockopt(3, SOL_SOCKET, SO_KEEPALIVE, [1], 4) = 0
-
read(3, "N\0\0\0\n5.5.31-log\0\372f\25\0rvz&Gs04\0\377\367!"..., 16384) = 82
-
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 4
-
fstat(4, {st_mode=S_IFREG|0644, st_size=106065056, ...}) = 0
-
mmap(NULL, 106065056, PROT_READ, MAP_PRIVATE, 4, 0) = 0x7f21b9ad9000
-
close(4) = 0
-
stat("/data1/app/services/mysql55/share/charsets/Index.xml", 0x7ffeb7006100) = -1 ENOENT (No such file or directory)
-
futex(0x90d540, FUTEX_WAKE_PRIVATE, 2147483647) = 0
-
write(3, "`\0\0\1\215\246\17\0\0\0\0\1!\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 100) = 100
-
read(3, "I\0\0\2\377\25\4#28000Access denied for u"..., 16384) = 77
-
shutdown(3, SHUT_RDWR) = 0
-
close(3) = 0
-
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
-
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f21c8956000
-
write(2, "ERROR 1045 (28000): ", 20ERROR 1045 (28000): ) = 20
-
write(2, "Access denied for user 'test1'@'"..., 64Access denied for user 'test1'@'localhost' (using password: YES)) = 64
-
write(2, "\n", 1
-
) = 1
-
write(1, "\7", 1) = 1
-
gettimeofday({1436522608, 908777}, NULL) = 0
- exit_group(1) = ?
採用TCP/IP協議連線
點選(此處)摺疊或開啟
-
[root@mysqlslave3 ~]# strace /data1/app/services/mysql55a/bin/mysql -h127.0.0.1 -utest1 -ptest1
-
execve("/data1/app/services/mysql55a/bin/mysql", ["/data1/app/services/mysql55a/bin"..., "-h127.0.0.1", "-utest1", "-ptest1"], [/* 30 vars */]) = 0
-
......
-
socket(PF_LOCAL, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0) = 3
-
connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
-
close(3) = 0
-
socket(PF_LOCAL, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0) = 3
-
connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
-
close(3) = 0
-
......
-
socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) = 3
-
connect(3, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = 0
-
fcntl(3, F_SETFL, O_RDONLY) = 0
-
fcntl(3, F_GETFL) = 0x2 (flags O_RDWR)
-
setsockopt(3, SOL_SOCKET, SO_RCVTIMEO, "\2003\341\1\0\0\0\0\0\0\0\0\0\0\0\0", 16) = 0
-
setsockopt(3, SOL_SOCKET, SO_SNDTIMEO, "\2003\341\1\0\0\0\0\0\0\0\0\0\0\0\0", 16) = 0
-
setsockopt(3, SOL_IP, IP_TOS, [8], 4) = 0
-
setsockopt(3, SOL_TCP, TCP_NODELAY, [1], 4) = 0
-
setsockopt(3, SOL_SOCKET, SO_KEEPALIVE, [1], 4) = 0
-
read(3, "N\0\0\0\n5.5.31-log\0\7g\25\0r2>$6$PH\0\377\367!"..., 16384) = 82
-
......
-
poll([{fd=3, events=POLLIN|POLLPRI}], 1, 0) = 0 (Timeout)
-
write(3, "!\0\0\0\3select @@version_comment li"..., 37) = 37
-
read(3, "\1\0\0\1\1'\0\0\2\3def\0\0\0\21@@version_comme"..., 16384) = 90
-
write(1, "Your MySQL connection id is 1402"..., 83Your MySQL connection id is 1402631
-
Server version: 5.5.31-log Source distribution
-
) = 83
- ......
我們再看一下分別透過unix socket和TCP/IP連線資料庫時,mysqld都做了什麼事情
點選(此處)摺疊或開啟
-
[root@mysqlslave3 ~]# strace -p 23465
-
[root@mysqlslave3 ~]# /data1-new/app/services/mysql55d/bin/mysql -uroot -p -h127.0.0.1 -P3309
-
Process 23465 attached
-
restart_syscall(<... resuming interrupted call ...>) = 1
-
fcntl(14, F_GETFL) = 0x2 (flags O_RDWR)
-
fcntl(14, F_SETFL, O_RDWR|O_NONBLOCK) = 0
-
accept(14, {sa_family=AF_INET, sin_port=htons(6771), sin_addr=inet_addr("127.0.0.1")}, [16]) = 17
-
fcntl(14, F_SETFL, O_RDWR) = 0
-
getsockname(17, {sa_family=AF_INET, sin_port=htons(3309), sin_addr=inet_addr("127.0.0.1")}, [16]) = 0
-
fcntl(17, F_SETFL, O_RDONLY) = 0
-
fcntl(17, F_GETFL) = 0x2 (flags O_RDWR)
-
setsockopt(17, SOL_SOCKET, SO_RCVTIMEO, "\36\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16) = 0
-
setsockopt(17, SOL_SOCKET, SO_SNDTIMEO, ", 16) = 0
-
fcntl(17, F_SETFL, O_RDWR|O_NONBLOCK) = 0
-
setsockopt(17, SOL_IP, IP_TOS, [8], 4) = 0
-
setsockopt(17, SOL_TCP, TCP_NODELAY, [1], 4) = 0
-
futex(0xf444a4, FUTEX_WAKE_OP_PRIVATE, 1, 1, 0xf444a0, {FUTEX_OP_SET, 0, FUTEX_OP_CMP_GT, 1}) = 1
-
futex(0xf438c0, FUTEX_WAKE_PRIVATE, 1) = 1
-
poll([{fd=14, events=POLLIN}, {fd=16, events=POLLIN}], 2, 4294967295
-
-
-
[root@mysqlslave3 ~]# strace -p 23465
-
[root@mysqlslave3 ~]# /data1-new/app/services/mysql55d/bin/mysql --socket=/data1-new/app/tmp/mysql55d.sock -uroot -P3309
-
Process 23465 attached
-
restart_syscall(<... resuming interrupted call ...>) = 1
-
fcntl(16, F_GETFL) = 0x2 (flags O_RDWR)
-
fcntl(16, F_SETFL, O_RDWR|O_NONBLOCK) = 0
-
accept(16, {sa_family=AF_LOCAL, NULL}, [2]) = 17
-
fcntl(16, F_SETFL, O_RDWR) = 0
-
getsockname(17, {sa_family=AF_LOCAL, sun_path="/data1-new/app/tmp/mysql55d.sock"}, [35]) = 0
-
fcntl(17, F_SETFL, O_RDONLY) = 0
-
fcntl(17, F_GETFL) = 0x2 (flags O_RDWR)
-
setsockopt(17, SOL_SOCKET, SO_RCVTIMEO, "\36\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16) = 0
-
setsockopt(17, SOL_SOCKET, SO_SNDTIMEO, ", 16) = 0
-
fcntl(17, F_SETFL, O_RDWR|O_NONBLOCK) = 0
-
setsockopt(17, SOL_IP, IP_TOS, [8], 4) = -1 EOPNOTSUPP (Operation not supported)
-
futex(0xf444a4, FUTEX_WAKE_OP_PRIVATE, 1, 1, 0xf444a0, {FUTEX_OP_SET, 0, FUTEX_OP_CMP_GT, 1}) = 1
-
futex(0xf438c0, FUTEX_WAKE_PRIVATE, 1) = 1
- poll([{fd=14, events=POLLIN}, {fd=16, events=POLLIN}], 2, 4294967295
2 UNIX domain sockets have explicit knowledge that they're executing on
the same system. They avoid the extra context switch through the
netisr, and a sending thread will write the stream or datagrams directly
into the receiving socket buffer. No checksums are calculated, no
headers are inserted, no routing is performed, etc.
3 In general, the argument for implementing over TCP is that it gives you
location independence and immediate portability -- you can move the client
or the daemon, update an address, and it will "just work". The sockets
layer provides a reasonable abstraction of communications services, so
it's not hard to write an application so that the connection/binding
portion knows about TCP and UNIX domain sockets, and all the rest just
uses the socket it's given. So if you're looking for performance locally,
I think UNIX domain sockets probably best meet your need. Many people
will code to TCP anyway because performance is often less critical, and
the network portability benefit is substantial.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15480802/viewspace-1755100/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- TCP合法登入TCP
- mysql使用者本地登入localhost和127.0.0.1區別MySqllocalhost127.0.0.1
- ORACLE限制IP登入Oracle
- 【網頁登入】QQ 登入、微信登入、微博登入、GitHub 登入網頁Github
- mysql密碼和登入問題MySql密碼
- mysql怎麼登入MySql
- MySQL安全登入策略MySql
- shell禁止非法IP ssh登入
- 解決Mysql中只能通過localhost登陸不能通過ip登陸的問題MySqllocalhost
- 修改 MySQL 登入密碼MySql密碼
- MySQL登入驗證方式MySql
- Linux中登入mysqlLinuxMySql
- 登入Facebook和Twitter
- 您的登入IP不在管理員配置的登入掩碼範圍內
- [API 寫法] QQ 登入、微信登入、Facebook、google、蘋果登入APIGo蘋果
- 聊聊“密碼登入”、“手機快捷登入”和“第三方聯合登入”密碼
- MySQL 登入資料庫報錯'ERROR 1045 (28000): Access denied for user'..'@'localhost''MySql資料庫Errorlocalhost
- mysql在Linux下登入正常,但是用Navicat登入報錯MySqlLinux
- 如何檢視手機登入IP地址
- ssh禁止root登入及遮蔽指定ip
- 透過TRIGGER限制IP登入
- Mysql安裝和遠端登入--Centos7MySqlCentOS
- unbuntu16.04 伺服器的 免密登入、祕鑰登入和禁止密碼登入 配置伺服器密碼
- unbuntu16.04 伺服器的 免密登入、秘鑰登入和禁止密碼登入 配置伺服器密碼
- uniapp 完成兩種方式登入 驗證碼登入 密碼登入APP密碼
- sqlplus 可以登入 plsql 不能登入SQL
- mysql 8.0 使用 navicat 登入報錯MySql
- MYSQL 連線登入過程分析MySql
- JavaScript登入JavaScript
- flask 登入Flask
- 登入功能
- 谷歌登入谷歌
- Spring Security 一鍵接入驗證碼登入和小程式登入Spring
- 關於Oracle和MySQL中的無密碼登入OracleMySql密碼
- linux 免登入以及配置別名登入Linux
- 第三方登入 (微博登入 web)Web
- 為爬蟲獲取登入cookies: 使用Charles和requests模擬微博登入爬蟲Cookie
- vnc登入工具,好用的vnc登入工具,具體登入vnc客戶端使用教程VNC客戶端