• 嗨!不来看一下如何骚气十足的登陆MySQL嘛?


    前置知识

    我们想登陆到mysql中前提是肯定需要一个用户名和密码:比如

    mysql -uroot -proot
    

    在mysql中用户的信息会存放在 mysql数据库下的 user表中 可以像下面这样查看到所有用户信息

    1. mysql> use mysql
    2. Database changed
    3. mysql> select * from user\G
    4. *************************** 1. row ***************************
    5.                   Host: localhost
    6.                   User: root
    7.            Select_priv: Y
    8.            Insert_priv: Y
    9.            Update_priv: Y
    10.            Delete_priv: Y
    11.            Create_priv: Y
    12.              Drop_priv: Y
    13.            Reload_priv: Y
    14.          Shutdown_priv: Y
    15.           Process_priv: Y
    16.              File_priv: Y
    17.             Grant_priv: Y
    18.        References_priv: Y
    19.             Index_priv: Y
    20.             Alter_priv: Y
    21.           Show_db_priv: Y
    22.             Super_priv: Y
    23.  Create_tmp_table_priv: Y
    24.       Lock_tables_priv: Y
    25.           Execute_priv: Y
    26.        Repl_slave_priv: Y
    27.       Repl_client_priv: Y
    28.       Create_view_priv: Y
    29.         Show_view_priv: Y
    30.    Create_routine_priv: Y
    31.     Alter_routine_priv: Y
    32.       Create_user_priv: Y
    33.             Event_priv: Y
    34.           Trigger_priv: Y
    35. Create_tablespace_priv: Y
    36.               ssl_type:
    37.             ssl_cipher:
    38.            x509_issuer:
    39.           x509_subject:
    40.          max_questions: 0
    41.            max_updates: 0
    42.        max_connections: 0
    43.   max_user_connections: 0
    44.                 plugin: mysql_native_password
    45.  authentication_string: *C85A9826269E1AD748DFC3CEC32D040735B27207
    46.       password_expired: N
    47.  password_last_changed: 2019-11-07 14:39:30
    48.      password_lifetime: NULL
    49.         account_locked: N
    50. *************************** 2. row ***************************
    51.                   Host: localhost
    52.                   User: mysql.session
    53.            Select_priv: N

    其中有一列叫做HOST,HOST的不同值决定了用户拥有不同的登陆方式:比如:

    标识符含义
    %任意ip均等登陆
    localhost只允许本地登陆
    127.0.0.1只允许本地登陆
    sv1主机名为sv1的机器可登录,主机名可以在 /etc/hostname中查看
    ::1本机可登录

    所以在登陆前,请确定你的使用的登陆用户的HOST列中有相应的配置

    骚气的登陆

    在mac上登陆华为云的服务器

    1. MacBook-Pro% ssh 'root'@'139.9.92.123'
    2. root@139.9.92.123's password:
    3. Last failed login: Fri May 29 11:03:42 CST 2020 from 202.85.208.14 on ssh:notty
    4. There was 1 failed login attempt since the last successful login.
    5. Last login: Thu May 28 16:36:32 2020 from 202.85.208.7
    6.  Welcome to Huawei Cloud Service
    7. -bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
    8. [root@139 ~]#

    在mac上远程登陆服务器上的mysql

    1. MacBook-Pro% ./mysql -h139.9.92.123 -uroot -reqw123.. -P3306
    2. mysql: [Warning] Using a password on the command line interface can be insecure.
    3. Welcome to the MySQL monitor.  Commands end with ; or \g.
    4. Your MySQL connection id is 2174
    5. Server version: 5.7.29 MySQL Community Server (GPL)
    6. Copyright (c) 20002020, Oracle and/or its affiliates. All rights reserved.
    7. Oracle is a registered trademark of Oracle Corporation and/or its
    8. affiliates. Other names may be trademarks of their respective
    9. owners.
    10. Type 'help;' or '\h' for helpType '\c' to clear the current input statement.
    11. mysql> show databases;
    12. +--------------------+
    13. | Database           |
    14. +--------------------+
    15. | information_schema |

    mac登陆本地的mysql

    如果你有配置环境变量,或者你的mysql的可执行文件在/etc/bin中,那你可以在任何目录中使用mysql命令

    你可以直接像下面这样登陆:

    1. MacBook-Pro% mysql -uroot -p
    2. Enter password:
    3. Welcome to the MySQL monitor.  Commands end with ; or \g.
    4. Your MySQL connection id is 2
    5. Server version: 5.7.30 MySQL Community Server (GPL)
    6. Copyright (c) 20002020, Oracle and/or its affiliates. All rights reserved.
    7. Oracle is a registered trademark of Oracle Corporation and/or its
    8. affiliates. Other names may be trademarks of their respective
    9. owners.
    10. Type 'help;' or '\h' for helpType '\c' to clear the current input statement.
    11. mysql>

    如果你没有配置环境变量,系统就不能直接识别mysql命令,需要你进入到mysql安装目录下的bin文件下,找到mysql命令,然后执行登陆的动作

    1. MacBook-Pro% /usr/local/mysql/bin/mysql -uroot -p
    2. Enter password:
    3. Welcome to the MySQL monitor.  Commands end with ; or \g.
    4. Your MySQL connection id is 3
    5. Server version: 5.7.30 MySQL Community Server (GPL)
    6. Copyright (c) 20002020, Oracle and/or its affiliates. All rights reserved.
    7. Oracle is a registered trademark of Oracle Corporation and/or its
    8. affiliates. Other names may be trademarks of their respective
    9. owners.
    10. Type 'help;' or '\h' for helpType '\c' to clear the current input statement.
    11. mysql>

    也可以用远程登陆的方式登陆本地mysql

    1. MacBook-Pro% mysql -h127.0.0.1 -uroot -proot -P3306
    2. mysql: [Warning] Using a password on the command line interface can be insecure.
    3. Welcome to the MySQL monitor.  Commands end with ; or \g.
    4. Your MySQL connection id is 4
    5. Server version: 5.7.30 MySQL Community Server (GPL)
    6. Copyright (c) 20002020, Oracle and/or its affiliates. All rights reserved.
    7. Oracle is a registered trademark of Oracle Corporation and/or its
    8. affiliates. Other names may be trademarks of their respective
    9. owners.
    10. Type 'help;' or '\h' for helpType '\c' to clear the current input statement.
    11. mysql> show databases;
    12. +--------------------+
    13. | Database           |
    14. +--------------------+
    15. | information_schema |
    16. | assignment         |
    17. | cal                |

    本地登陆

    我们可以借助mysql.sock实现本地登陆。

    那这个mysql.sock是什么?

    看起来我们需要了解一下mysql.sock的作用,因为通过它我们可以实现mysql的本地登陆。

    mysql.sock应该是mysql的主机和客户机在同一host(物理服务器)上的时候,使用unix domain socket做为通讯协议的载体,它比tcp快。

    通过命令可以查看到mysql.sock的位置。

    1. MacBook-Pro% netstat -ln | grep mysql
    2. 64e3f4c55eb824d7 stream  0   0  64e3f4c5614859a7   0   0   0 /tmp/mysql.sock

    记下这个 mysql.sock的地址。接下来我们会创建一个配置文件,你找个看着比较顺眼的目录放置这个配置文件。

    比如就像下面这样:

    1. MacBook-Pro% sudo mkdir etc
    2. MacBook-Pro% ls -l
    3. total 552
    4. -rw-r--r--   1 root    wheel   275235 Mar 24 01:35 LICENSE
    5. -rw-r--r--   1 root    wheel      587 Mar 24 01:35 README
    6. drwxr-xr-x  40 root    wheel     1280 Mar 24 02:45 bin
    7. drwxr-x---  27 _mysql  _mysql     864 May 28 20:44 data
    8. drwxr-xr-x   5 root    wheel      160 Mar 24 02:44 docs
    9. drwxr-xr-x   2 root    wheel       64 May 29 11:39 etc
    10. drwxr-xr-x  53 root    wheel     1696 Mar 24 02:44 include
    11. drwxr-x---   3 _mysql  _mysql      96 May 28 20:44 keyring
    12. drwxr-xr-x  11 root    wheel      352 May 13 09:16 lib
    13. drwxr-xr-x   4 root    wheel      128 Mar 24 02:44 man
    14. drwxr-xr-x  39 root    wheel     1248 Mar 24 02:44 share
    15. drwxr-xr-x   6 root    wheel      192 May 28 19:20 support-files
    16. MacBook-Pro% cd etc
    17. MacBook-Pro% sudo touch user.root.cnf
    18. MacBook-Pro% sudo vim user.root.cnf

    然后在 user.root.cnf 中添加如下的配置:

    1. [client]
    2. user=root
    3. password=root
    4. socket=/tmp/mysql.sock

    好了,现在可以这样实现本地登陆

    1. MacBook-Pro% ../bin/mysql --defaults-extra-file=./user.root.cnf
    2. Welcome to the MySQL monitor.  Commands end with ; or \g.
    3. Your MySQL connection id is 6
    4. Server version: 5.7.30 MySQL Community Server (GPL)
    5. Copyright (c) 20002020, Oracle and/or its affiliates. All rights reserved.
    6. Oracle is a registered trademark of Oracle Corporation and/or its
    7. affiliates. Other names may be trademarks of their respective
    8. owners.
    9. Type 'help;' or '\h' for helpType '\c' to clear the current input statement.
    10. mysql>

    花里胡哨的本地登陆

    有时候,你可能会看到其他大佬登陆mysql时直接使用命令:mysql.local 就骚气十足的本地登陆mysql

    他是怎么做到的呢?其实很简单、借助alias+mysql.sock实现:

    为我们的登陆mysql的命令添加别名,像下面这样:

    1. MacBook-Pro% alias mysql.local='/usr/local/mysql/bin/mysql --defaults-extra-file=/usr/local/mysql/etc/user.root.cnf'
    2. MacBook-Pro% mysql.local
    3. Welcome to the MySQL monitor.  Commands end with ; or \g.
    4. Your MySQL connection id is 7
    5. Server version: 5.7.30 MySQL Community Server (GPL)
    6. Copyright (c) 20002020, Oracle and/or its affiliates. All rights reserved.
    7. Oracle is a registered trademark of Oracle Corporation and/or its
    8. affiliates. Other names may be trademarks of their respective
    9. owners.
    10. Type 'help;' or '\h' for helpType '\c' to clear the current input statement.
    11. mysql>

    从此,你也可以骚气登陆mysql

  • 相关阅读:
    MediaRecorder媒体录音机
    前端(二十七)——封装指南:Axios接口、常用功能、Vue和React中的封装技术
    瑞吉外卖之移动端菜品数据的展示
    商用清洁机器人即将爆发?INDEMIND为机器人落地“保驾护航”
    linux开机自动执行脚本、运行程序
    南大通用GBase 8a MPP Cluster管理工具简介
    Fetch与Axios数据请求
    消除springboot+thymeleaf时页面的红色波浪线告警
    苹果电脑好用的剪切板管理工具 Paste激活中文版最新
    软件测试/测试开发丨结对编程助手 GitHubCopilot
  • 原文地址:https://blog.csdn.net/weixin_72753070/article/details/126116962