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 ...
随机推荐
- WebApp开发经验
1.自适应屏幕 <meta name="viewport" id="viewport" content="width = device-widt ...
- R工作空间
工作空间,指的是你现有的R语言工作环境,它包括了任何一个用户定义的对象,比如:向量,矩阵,数据结构,列表,方法等.在一个R会话结束的时候,你可以保存现有的工作空间的映像,在下一次R启动的时候,该工作空 ...
- kali 重置 mysql 密码
You can recover MySQL database server password with following five easy steps. Step # 1: Stop the My ...
- linux安装lua相关编译报错
1.报之类的错误 /usr/lib/libreadline.so: undefined reference to `PC' /usr/lib/libreadline.so: undefined ref ...
- Classic Source Code Collected
收藏一些经典的源码,持续更新!!! 1.深度学习框架(Deep Learning Framework). A:Caffe (Convolutional Architecture for Fast Fe ...
- 构造数组的MaxTree
题目 一个数组的MaxTree定义: 数组必须没有重复元素 MaxTree是一棵二叉树,数组的每一个值对应一个二叉树节点 包括MaxTree树在内且在其中的每一棵子树上,值最大的节点都是树的头 给定一 ...
- Ajax禁止缓存的几个解决方案
最常用的方法是 方法1:服务器端代码加入 代码如下 复制代码 response.setHeader("Cache-Control", "no-cache, must-r ...
- JSF 2 panelGrid example
In JSF , "h:panelGrid" tag is used to generate HTML table tags to place JSF components in ...
- 对pymysql的简单封装
#coding=utf-8 #!/usr/bin/python import pymysql class MYSQL: """ 对pymysql的简单封装 "& ...
- 题目连接:http://acm.zznu.edu.cn/problem.php?id=1329
题目大意: 定理:把一个至少两位的正整数的个位数字去掉,再从余下的数中减去个位数的5倍.当且仅当差是17的倍数时,原数也是17的倍数 . 例如,34是17的倍数,因为3-20=-17是17的倍数:20 ...