MySQL 127.0.0.1和localhost本质区别
登录方式:
[root@10-4-14-168 ~]# mysql -uroot -p
Enter password:
查看权限表
mysql> SELECT user,host,password FROM mysql.user;
+------+-------------+-------------------------------------------+
| user | host | password |
+------+-------------+-------------------------------------------+
| root | localhost | *11B9ACA21786F766739D0EB1483C5F64212B81AC |
| root | 10.4.14.168 | *11B9ACA21786F766739D0EB1483C5F64212B81AC |
| root | 127.0.0.1 | *11B9ACA21786F766739D0EB1483C5F64212B81AC |
| root | ::1 | *11B9ACA21786F766739D0EB1483C5F64212B81AC |
+------+-------------+-------------------------------------------+
4 rows in set (0.00 sec)
查看当前的IP地址:
inet addr:10.4.14.168
验证一下三种登陆方式:
# mysql -h localhost -uroot -p
可以登陆
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.5.39, for Linux (x86_64) using readline 5.1 Connection id: 6
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.5.39-cll-lve MySQL Community Server (GPL) by Atomicorp
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 19 min 8 sec Threads: 1 Questions: 24 Slow queries: 0 Opens: 33 Flush tables: 1 Open tables: 26 Queries per second avg: 0.020
--------------
# mysql -h 127.0.0.1 -uroot -p
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.5.39, for Linux (x86_64) using readline 5.1 Connection id: 4
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.5.39-cll-lve MySQL Community Server (GPL) by Atomicorp
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: 3306
Uptime: 2 min 24 sec Threads: 2 Questions: 21 Slow queries: 0 Opens: 33 Flush tables: 1 Open tables: 26 Queries per second avg: 0.145
--------------
# mysql -h 10.4.14.168 -uroot -p
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.5.39, for Linux (x86_64) using readline 5.1 Connection id: 5
Current database:
Current user: root@10-4-14-168
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.5.39-cll-lve MySQL Community Server (GPL) by Atomicorp
Protocol version: 10
Connection: 10.4.14.168 via TCP/IP
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
TCP port: 3306
Uptime: 3 min 9 sec Threads: 2 Questions: 30 Slow queries: 0 Opens: 33 Flush tables: 1 Open tables: 26 Queries per second avg: 0.158
--------------
# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.5.39-cll-lve MySQL Community Server (GPL) by Atomicorp
Copyright (c) 2000, 2014, 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> status;
--------------
mysql  Ver 14.14 Distrib 5.5.39, for Linux (x86_64) using readline 5.1
Connection id:          7
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.5.39-cll-lve MySQL Community Server (GPL) by Atomicorp
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /var/lib/mysql/mysql.sock
Uptime:                 22 min 31 sec
Threads: 1  Questions: 31  Slow queries: 0  Opens: 33  Flush tables: 1  Open tables: 26  Queries per second avg: 0.022
--------------
下面尝试修改下host域
mysql> select user,host from mysql.user;
+------+-------------+
| user | host |
+------+-------------+
| root | 10.4.14.168 |
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
+------+-------------+
4 rows in set (0.00 sec) mysql> UPDATE mysql.user SET host ='10.4.5.9' WHERE host ='::1';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> SELECT user,host FROM mysql.user;
+------+-------------+
| user | host |
+------+-------------+
| root | 10.4.14.168 |
| root | 10.4.5.9 |
| root | 127.0.0.1 |
| root | localhost |
+------+-------------+
4 rows in set (0.00 sec)
现在如果10.4.5.9这台机器和这台10.4.14.168的连通性没问题的话就可以这种方式登陆:
inet addr:10.4.5.9
[root@10-4-5-9 ~]# mysql -h 10.4.14.168 -uroot -P 3306 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.39-cll-lve MySQL Community Server (GPL) by Atomicorp Copyright (c) 2000, 2013, 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>
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1 Connection id: 3
Current database:
Current user: root@10.4.5.9
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.5.39-cll-lve MySQL Community Server (GPL) by Atomicorp
Protocol version: 10
Connection: 10.4.14.168 via TCP/IP
Server characterset: latin1
Db characterset: latin1
Client characterset: latin1
Conn. characterset: latin1
TCP port: 3306
Uptime: 1 min 13 sec Threads: 2 Questions: 14 Slow queries: 0 Opens: 33 Flush tables: 1 Open tables: 26 Queries per second avg: 0.191
--------------
由此:引出了user表中host列的IP地址和localhost的区别:
IP地址:-h IP 是通过TCP/IP连接方式连接的。
localhost :-h localhost 或者 直接忽略-h 是通过socket连接方式连接的。
一般远程连接或者PHP、C等连接MySQL的时候都要指定IP地址,通过TCP/IP 方式连接
可以通过修改配置文件my.cnf
protocol=tcp 强制使用TCP/IP 连接
skip-networking 强制使用socket来连接。
MySQL 127.0.0.1和localhost本质区别的更多相关文章
- MySQL主机127.0.0.1与localhost区别总结
		1. mysql -h 127.0.0.1 的时候,使用TCP/IP连接, mysql server 认为该连接来自于127.0.0.1或者是"localhost.localdomain&q ... 
- mysql开启skip-name-resolve 导致root@127.0.0.1(localhost)访问引发的ERROR 1045 (28000)错误解决方案
		为什么配置skip-name-resolve? 由于mysql -h${ip} 远程访问速度过慢, mysql -uroot -p123456 根据网友经验(https://www.cnblogs.c ... 
- mysql -h localhost和mysql -h 127.0.0.1的区别
		今天早上同事说MySQL root账号登录不上了.我试了一下 #mysql -u root -p 提示”Access denied for user ‘root’@’localhost’ (using ... 
- [日常] 解决mysql localhost可以连接但是127.0.0.1不能连接
		在测试mysql的过程中遇到使用localhost可以连接但是127.0.0.1不能连接,原因是localhost使用的本地socket连接,127.0.0.1使用使用的tcp连接 在mysql库的u ... 
- linux7可以通过远程和localhost访问mysql,但是127.0.0.1不能访问
		网上搜索的其他方法都试过,不行 比如设置权限,开放端口,配置数据库... 最好偶然一个搜索查看可能原因是防火墙端口问题: vim /etc/sysconfig/iptables 在文件中添加下面语句 ... 
- linux下使用localhost和127.0.0.1都不能连接的解决思路
		linux下刚安装了mysql,尝试写了程序连接mysql,出现了只有用本地ip地址才能连接,而127.0.0.1和localhost都不能访问 解决这个问题主要查看3个方向 .hosts中是否有ip ... 
- 127.0.0.1与localhost与ip的区别
		127.0.0.1与localhost与ip的区别 May 18, 2014 localhost 不联网不使用网卡,不受防火墙和网卡限制本机访问 一般使用 本地套接字文件AF_UNIX 应用程序一般约 ... 
- Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://127.0.0.1:3306/test'
		原来的配置如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ... 
- localhost简介、localhost与 127.0.0.1 及 本机IP 的区别
		localhost是什么意思? 相信有人会说是本地ip,曾有人说,用127.0.0.1比localhost好,可以减少一次解析. 看来这个入门问题还有人不清楚,其实这两者是有区别的. localhos ... 
随机推荐
- rand.Read() 和 io.ReadFull(rand.Reader) 的区别?
			golang的随机包 rand.go 中我们可以看到 rand.Read 其实是调用的io.Reader.Read() 1: // Package rand implements a cryptogr ... 
- Jmeter介绍
			一.添加线程组 一个性能测试请求负载是基于一个线程组完成的.一个测试计划必须有一个线程组.测试计划添加线程组非常简单.在测试计划右键弹出下拉菜单(添加-->Threads(Users)---&g ... 
- linux笔记_20150825_linux下的软件工具唠叨下
			这些都是书上看到的,有些工具我也没有完全用过.先记下来再说.闲着也是闲着. 1.linux下常见的语言及编程环境:c/c++/java/perl/fortan等. 2.图形环境:gnome/kde/g ... 
- bzoj1036 树的统计Count
			第一次写链剖,于是挑了个简单的裸题写. 以下几点要注意: 1.链剖中的height是从根到该店经过的轻边个数 2.分清num与sum..... #include<cstdio> #incl ... 
- 【LeetCode】190 & 191 - Reverse Bits & Number of 1 Bits
			190 - Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ... 
- 传统企业,"哀兵必胜"的想法要不得
			[文/ 任英杰]同事在内网上发了一篇文章『哀兵必胜』,思量数日,作文应对.文中表达的积极精神让人敬佩,但背后似乎隐含着一股莫名的“情绪”.对行业大格局的基本看法会影响公司转型的成败,觉得还是有必要讨论 ... 
- MVC&&MVP
			Classic MVC Classic MVC 大概上世纪七十年代,Xerox PARC的Trygve提出了MVC的概念. 并应用在Smalltalk系统中,为了和其它类型的MVC加以区分,历史上习惯 ... 
- Application Cache
			轉發處:http://www.cnblogs.com/blackbird/archive/2012/06/12/2546751.html HTML5提供了一系列的特性来支持离线应用: applicat ... 
- 僵尸进程&孤儿进程
			http://www.cnblogs.com/Anker/p/3271773.html 
- 《Java数据结构与算法》笔记-CH4-6栈结构实现中缀转后缀
			/** * 中缀表达式转换成后缀表达式: 从输入(中缀表达式)中读取的字符,规则: 操作数: 写至输出 左括号: 推其入栈 右括号: 栈非空时重复以下步骤--> * 若项不为(,则写至输出: 若 ... 
