盘点Mysql的登陆方式

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

盘点Mysql的登陆方式

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

在mysql中用户的信息会存放在 mysql数据库下的 user表中

可以 use mysql 然后select * from userG;查看到系统上的所用的用户信息;

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

标识符 含义
% 任意ip均等登陆
localhost 只允许本地登陆
127.0.0.1 只允许本地登陆
sv1 主机名为sv1的机器可登录,主机名可以在 /etc/hostname中查看
::1 本机可登录
所以在登陆前,请确定你的使用的登陆用户的HOST列中有相应的配置

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

Copy
MacBook-Pro% ssh 'root'@'139.9.92.123'
root@139.9.92.123's password:
Last failed login: Fri May 29 11:03:42 CST 2020 from 202.85.208.14 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Thu May 28 16:36:32 2020 from 202.85.208.7

Welcome to Huawei Cloud Service

-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
[root@139 ~]#
在mac上远程登陆服务器上的mysql

Copy
MacBook-Pro% ./mysql -h139.9.92.123 -uroot -reqw123.. -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2174
Server version: 5.7.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

  1. Other names may be trademarks of their respective
    owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> show databases;
Database
information_schema

mac登陆本地的mysql

如果你有配置环境变量,在任何目录下系统都识别mysql命令

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

Copy
MacBook-Pro% mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

  1. 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命令,需要你进入到mysql安装目录下的bin文件下,找到mysql命令,然后执行登陆的动作

Copy
MacBook-Pro% /usr/local/mysql/bin/mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 3
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

  1. 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

Copy
MacBook-Pro% mysql -h127.0.0.1 -uroot -proot -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

  1. Other names may be trademarks of their respective
    owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> show databases;
Database
information_schema
assignment
cal

本地登陆

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

那这个mysql.sock是什么?

很直观的我们得知道这个mysql.sock的作用,通过它我们可以实现mysql的本地登陆。

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

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

Copy
MacBook-Pro% netstat -ln | grep mysql
64e3f4c55eb824d7 stream 0 0 64e3f4c5614859a7 0 0 0 /tmp/mysql.sock
记下这个 mysql.sock的地址。接下来我们会创建一个配置文件,你找个看着比较顺眼的目录放置这个配置文件。

我实在这样创建的:

Copy
MacBook-Pro% sudo mkdir etc
MacBook-Pro% ls -l
total 552
-rw-r--r-- 1 root wheel 275235 Mar 24 01:35 LICENSE
-rw-r--r-- 1 root wheel 587 Mar 24 01:35 README
drwxr-xr-x 40 root wheel 1280 Mar 24 02:45 bin
drwxr-x--- 27 _mysql _mysql 864 May 28 20:44 data
drwxr-xr-x 5 root wheel 160 Mar 24 02:44 docs
drwxr-xr-x 2 root wheel 64 May 29 11:39 etc
drwxr-xr-x 53 root wheel 1696 Mar 24 02:44 include
drwxr-x--- 3 _mysql _mysql 96 May 28 20:44 keyring
drwxr-xr-x 11 root wheel 352 May 13 09:16 lib
drwxr-xr-x 4 root wheel 128 Mar 24 02:44 man
drwxr-xr-x 39 root wheel 1248 Mar 24 02:44 share
drwxr-xr-x 6 root wheel 192 May 28 19:20 support-files
MacBook-Pro% cd etc
MacBook-Pro% sudo touch user.root.cnf
MacBook-Pro% sudo vim user.root.cnf
然后在 user.root.cnf 中添加如下的配置:

Copy
[client]
user=root
password=root
socket=/tmp/mysql.sock
好了,现在可以这样实现本地登陆

Copy
MacBook-Pro% ../bin/mysql --defaults-extra-file=./user.root.cnf
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 6
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

  1. 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时使用: mysql.local 骚气十足的本地登陆mysql

他是怎么做到的呢?可以借助alias+mysql.sock实现:

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

Copy
MacBook-Pro% alias mysql.local='/usr/local/mysql/bin/mysql --defaults-extra-file=/usr/local/mysql/etc/user.root.cnf'
MacBook-Pro% mysql.local
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 7
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

  1. 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

我是bloger 赐我白日梦 , 欢迎关注我,后续一定分享mysql相关知识点

作者: 赐我白日梦

出处:https://www.cnblogs.com/ZhuChangwu/p/12987168.html

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
SQL Kubernetes 监控
在 k8s 环境中使用 mysql 部署 dolphinscheduler (非 helm 的方式)
在 k8s 环境中使用 mysql 部署 dolphinscheduler (非 helm 的方式)
1584 0
|
关系型数据库 MySQL 中间件
5分钟,使用yum方式完成mysql安装
5分钟,使用yum方式完成mysql安装
1928 1
5分钟,使用yum方式完成mysql安装
|
11月前
|
安全 关系型数据库 MySQL
MySQL的登陆【数据库系统】
MySQL的登陆【数据库系统】
68 0
|
关系型数据库 MySQL 数据安全/隐私保护
【MySQL】解决MySQL登陆时的闪退问题
大家在打开MySQL时,可能会遇到在登陆界面输入密码之后就闪退的这个问题.平时我们写代码,虽然会报错,但是错误的原因是给我们了.我们可以按照错误的原因进行处理.但是MySQL是直接闪退,所以并不知道报错的信息.这就造成了大部分人遇到这种情况就无从下手了.在这里教大家如何查看MySQL的报错信息
|
关系型数据库 MySQL
PureMySQL 一个MySQL简单操作方式
PureMySQL: 一个MySQL简单操作方式
60 0
|
安全 关系型数据库 MySQL
因安全要求,需要修改mysql同步用户repl的密码。在主库修改完后,从库最佳修改同步用户密码的方式
因安全要求,需要修改mysql同步用户repl的密码。在主库修改完后,从库最佳修改同步用户密码的方式
489 0
|
安全 关系型数据库 MySQL
【数据库】centos 7系统,二进制方式安装mysql
【数据库】centos 7系统,二进制方式安装mysql
283 0
【数据库】centos 7系统,二进制方式安装mysql
|
关系型数据库 MySQL 数据库
【Mysql数据库】数据库添加索引方式
【Mysql数据库】数据库添加索引方式
116 0
|
关系型数据库 MySQL Linux
Linux 使用rpm方式安装最新mysql(5.7.22)步骤以及常见问题解决
Linux 使用rpm方式安装最新mysql(5.7.22)步骤以及常见问题解决
190 0
Linux 使用rpm方式安装最新mysql(5.7.22)步骤以及常见问题解决
|
存储 SQL 弹性计算
MySQL 备份方式|学习笔记
快速学习 MySQL 备份方式
172 0