Mysql配置文件读取的次序

来源:互联网 发布:物理地址是mac地址吗 编辑:程序博客网 时间:2024/06/18 10:54


配置文件在unix主机上面,有两种配置文件,按照两种方式进行搜索。

首先就是放在/etc/my.cnf下面,这是常见配置的位置。这个文件是全局配置文件,绝大多数配置是关系到服务器进程这一端的。但是mysql还有客户端的工具,所以也可以在MYSQL_HOME下面也有一个my.cnf,这个是影响单个实例的,如果自己定义一个如何去连接人家的那么就是MySQL客户端了。

 

在自己的目录夹下面有一个隐藏文件,.my.cnf,里面会放置一些特定的段,但是这个影响的是非服务器端,影响的是客户端怎么去连接服务端。

 

首先一个服务器启动的环节,会读mysqld这个环节,[mysqld]是运行程序的程序名。

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

 

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

可以看到这里有两段程序,一段程序是mysqld如何运行的,另外一个程序是mysqld_safe如何运行的。绿色标记的是常见的选项,能保证正常运行的选项。

 

Mysqld里面有哪些选项呢,可以在mysqld帮助查看并且在my.cnf里面进行开启。

[root@localhost test]# mysqld --help --verbose |grep log-bin

2017-10-23 16:10:49 0 [Note] mysqld (mysqld 5.6.38) starting as process 11435 ...

2017-10-23 16:10:49 11435 [Note] Plugin 'FEDERATED' is disabled.

  --log-bin[=name]    Log update queries in binary format. Optional (but

  --log-bin-index=name

  --log-bin-trust-function-creators

                      If set to FALSE (the default), then when --log-bin is

  --log-bin-use-v1-row-events

2017-10-23 16:10:49 11435 [Note] Binlog end

2017-10-23 16:10:49 11435 [Note] Shutting down plugin 'MyISAM'

2017-10-23 16:10:49 11435 [Note] Shutting down plugin 'CSV'

log-bin                                                    (No default value)

log-bin-index                                              (No default value)

log-bin-trust-function-creators                            FALSE

log-bin-use-v1-row-events                                  FALSE

可以看到log-bin是未被打开的,此时将log-bin写入到my.cnf里面。

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

log-bin

此时查看数据目录下面的文件

[root@localhost test]# service mysqld stop

Redirecting to /bin/systemctl stop  mysqld.service

[root@localhost test]# ls /var/lib/mysql

auto.cnf  ibdata1  ib_logfile0  ib_logfile1  mysql  performance_schema

 

重启之后再查看数据目录下面的文件

[root@localhost test]# service mysqld start

Redirecting to /bin/systemctl start  mysqld.service

[root@localhost test]# ls /var/lib/mysql

auto.cnf  ib_logfile0  mysql              mysqld-bin.index  performance_schema

ibdata1   ib_logfile1  mysqld-bin.000001  mysql.sock

可以看到多了两个文件,一个是存放日志的索引,一个是日志的序列号。

 

 

如果将服务再次重启可以看见又多了一个日志文件,mysql的每次重启都会多一个二进制的日志文件。

[root@localhost test]# service mysqld restart

Redirecting to /bin/systemctl restart  mysqld.service

[root@localhost test]# ls /var/lib/mysql

auto.cnf  ib_logfile0  mysql              mysqld-bin.000002 mysql.sock

ibdata1   ib_logfile1  mysqld-bin.000001  mysqld-bin.index   performance_schema

 

 

 

查看mysql默认使用的引擎

[root@localhost test]# mysqld --help --verbose |grep storage-engine

  --default-storage-engine=name

  --default-tmp-storage-engine=name

default-storage-engine                                     InnoDB

default-tmp-storage-engine                                 InnoDB

 

 

my.cnf里面添加

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

default-storage-engine=myisam  --指定默认的存储引擎为myisam

之后切换到mysql

mysql> use test;

Database changed

mysql> create table t(id int);

Query OK, 0 rows affected (0.01 sec)

 

mysql> show create table t;

+-------+---------------------------------------------------------------------------------------+

| Table | Create Table                                                                          |

+-------+---------------------------------------------------------------------------------------+

| t     | CREATE TABLE `t` (

  `id` int(11) DEFAULT NULL

) ENGINE=MyISAM DEFAULT CHARSET=latin1 |

可以看到创建表默认使用的存储引擎为myisam。服务器默认使用的引擎为innodb

 

 

 

查看客户端的配置。

[root@localhost test]# /usr/bin/mysql --help less

/usr/bin/mysql  Ver 14.14 Distrib 5.6.38, for Linux (x86_64) using  EditLine wrapper

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

 

Usage: /usr/bin/mysql [OPTIONS] [database]

  -?, --help          Display this help and exit.

  -I, --help          Synonym for -?

  --auto-rehash       Enable automatic rehashing. One doesn't need to use

                      'rehash' to get table and field completion, but startup

                      and reconnecting may take a longer time. Disable with

                      --disable-auto-rehash.

                      (Defaults to on; use --skip-auto-rehash to disable.)

  -A, --no-auto-rehash

                      No automatic rehashing. One has to use 'rehash' to get

                      table and field completion. This gives a quicker start of

                      mysql and disables rehashing on reconnect.

  --auto-vertical-output

                      Automatically switch to vertical output mode if the

                      result is wider than the terminal width.

  -B, --batch         Don't use history file. Disable interactive behavior.

                      (Enables --silent.)

  --bind-address=name IP address to bind to.

  -b, --binary-as-hex Print binary data as hex

  --character-sets-dir=name

                      Directory for character set files.

  --column-type-info  Display column type information.

  -c, --comments      Preserve comments. Send comments to the server. The

                      default is --skip-comments (discard comments), enable

                      with --comments.

 

 

 

/etc/my.cnf是对mysqld这一段进行配置的,但是也可以加上形形色色的字段,如[mysql]客户端工具,[mysqladmin],每一条指令都是单独的一段,如果在[mysql]这一段里面设置了用户名,密码。

编辑/etc/my.cnf加上下面这段,账户名密码都是正确的。

[mysql]

user=root

password=123456

在客户端登入的时候只需要[root@localhost test]# mysql就可以登入了,不需要使用-uroot -p123456选项登入。

现在将配置文件my.cnf修改,账户为错误的信息。

[mysql]

user=hh

password=123456

[root@localhost test]# mysql

ERROR 1045 (28000): Access denied for user 'hh'@'localhost' (using password: YES)

 

可以看到使用mysql命令登入不了了。

 

现在切换到test普通用户

[root@localhost test]# su test

[test@localhost ~]$ vim ~/.my.cnf     隐藏文件my.cnf为新创建的文件

在该文件里面写上

[mysql]

user=root

password=123456

再次使用mysql命令连接mysql发现也可以成功连接上。如果切换到其他用户但是不新建立自己的隐藏的my.cnf文件那么还是登入不上的,因为在他的主目录下面并没有所说的配置文件,在读取的时候还是读取的全局配置文件。

[test@localhost ~]$ rm ~/.my.cnf

[test@localhost ~]$ mysql

ERROR 1045 (28000): Access denied for user 'hh'@'localhost' (using password: YES)

在删除了test的隐藏的.my.cnf文件再次使用mysql命令登入可以看到读取的是全局配置文件。

 

在~/.my.cnf下面定义的[mysql]字段使用了错误的信息在登入的时候会失败,因为会覆盖全局配置文件即/ect/my.cnf