• 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来连接。

     

  • 相关阅读:
    手写web框架之加载配置项目
    JAVA中注解的实现原理
    使用Mock 测试 controller层
    如何写resultful接口
    RSA加密、解密、签名、验签的原理及方法
    AES256位加密
    聊聊分布式事务,再说说解决方案
    分布式锁简单入门以及三种实现方式介绍
    redis总结(面试中容易遇到的)
    字符串匹配的KMP算法
  • 原文地址:https://www.cnblogs.com/xiaoit/p/3992917.html
Copyright © 2020-2023  润新知