暂无图片
暂无图片
3
暂无图片
暂无图片
暂无图片

MySQL配置文件client组参数同mysql组参数区别

原创 晨辉 2021-12-30
5217

在配置MySQL的参数文件时,有两个跟连接配置相关的组选项,像如下配置文件,既配置了[client] 也配置了[mysql],那么这两个参数组有啥区别呢
参考配置文件:

#cat /etc/my.cnf 
[client]
user = xhy
password = 123456
port = 3306
host = 127.0.0.1
[mysql]
user = root
password = 123456
port = 3306
prompt = [\u@\p][\d\]>
no_auto_rehash
# 以下其他参数省略

从官网上面找到这么一句话: [client] 被所有MySQL客户端读取,除了mysqld。
The [client] option group is read by all client programs provided in MySQL distributions (but not by mysqld)
那么也就是说 [client] 里面的内容可以被 mysql,mysqladmin,mysqldump 等客户端读取的,而 [mysql]里的内容应该就是只能给mysql客户端读取了,下面来做个简单实验验证一下:

1)配置文件如下时:

[client]
user = xhy
password = 123456
port = 3306
host = 127.0.0.1
[mysql]
user = root
password = 123456
port = 3306
prompt = [\u@\p][\d\]>
no_auto_rehash

使用mysql连接:

[root@ora11g1 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.26-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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.

[root@3306][(none)]>>
可以看到连接的用户为root,说明读取到的配置信息是[mysql]里的

2)将配置文件修改如下(调换了client和mysql的位置):

[mysql]
user = root
password = 123456
port = 3306
prompt = [\u@\p][\d\]>
no_auto_rehash
[client]
user = xhy
password = 123456
port = 3306
host = 127.0.0.1

使用mysql连接:

[root@ora11g1 ~]# mysql          
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.26-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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.

[xhy@3306][(none)]>>
可以看到连接的用户为xhy,说明读取到的用户配置信息是[client]里的
以上两点说明[mysql],[client]都是可以被mysql客户端读取的,两者都配置了一样的参数时,遵循覆盖原则,选取最下面的。

3)将配置文件修改如下(换回了client和mysql的位置,client 端口信息错误):

[client]
user = xhy
password = 123456
port = 3308  ### 端口错误
host = 127.0.0.1
[mysql]
user = root
password = 123456
port = 3306
prompt = [\u@\p][\d\]>
no_auto_rehash

使用mysql, mysqladmin,mysqldump 连接:

 mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.26-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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.

[root@3306][(none)]>>exit
Bye
[root@ora11g1 ~]# mysqladmin version 
mysqladmin: connect to server at '127.0.0.1' failed
error: 'Can't connect to MySQL server on '127.0.0.1' (111)'
Check that mysqld is running on 127.0.0.1 and that the port is 3308.
You can check this by doing 'telnet 127.0.0.1 3308'

[root@ora11g1 ~]# mysqldump -B xhy >xhy.sql
mysqldump: Got error: 2003: Can't connect to MySQL server on '127.0.0.1' (111) when trying to connect
可以看到mysql可正常连接,mysqladmin连接失败,提示检查 3308 端口,说明mysqladmin使用的是[client] 里的 port = 3308 信息,mysqldump 也是连接失败。

4)将配置文件修改如下([mysql] 端口信息错误):

[client]
user = xhy
password = 123456
port = 3306  
host = 127.0.0.1
[mysql]
user = root
password = 123456
port = 3308 ### 端口错误
prompt = [\u@\p][\d\]>
no_auto_rehash

使用mysql, mysqladmin,mysqldump 连接:

[root@ora11g1 ~]# mysql
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
[root@ora11g1 ~]# mysql -P3306
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.26-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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.

[root@3306][(none)]>>
[root@ora11g1 ~]# mysqladmin version       
mysqladmin  Ver 8.42 Distrib 5.7.26, for linux-glibc2.12 on x86_64
Copyright (c) 2000, 2019, 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.

Server version          5.7.26-log
Protocol version        10
Connection              127.0.0.1 via TCP/IP
TCP port                3306
Uptime:                 45 min 6 sec

Threads: 2  Questions: 285  Slow queries: 4  Opens: 132  Flush tables: 1  Open tables: 86  Queries per second avg: 0.105
[root@ora11g1 ~]# mysqldump -B xhy >xhy.sql
[root@ora11g1 ~]# head xhy.sql 
-- MySQL dump 10.13  Distrib 5.7.26, for linux-glibc2.12 (x86_64)
--
-- Host: 127.0.0.1    Database: xhy
-- ------------------------------------------------------
-- Server version       5.7.26-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
可以看到现在是mysql默认连接失败了,指定端口 3306时连接成功,mysqladmin 和 mysqldump 都是直接连接成功。

总结:1)当MySQL的配置文件既配置了[client] 也配置了[mysql]时,[client] 的作用范围是对MySQL的所有客户端,包括 mysql、mysqladmin、mysqldump等,而[mysql]只作用于mysql客户端

2)当有参数是重复配置的或者对同一客户端都生效时(比如mysql客户端),那么使用的是最下面那个(下面覆盖上面原则)。
3)MySQL的客户端都是可以在命令行指定连接参数的,如配置文件的参数错误,可直接在命令行输入该参数覆盖参数文件参数。

最后修改时间:2021-12-30 10:46:32
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论