MySQL配置管理
1.配置mysql的方法
1.编译的时候
#程序存放位置
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.38 \
#数据存放位置
-DMYSQL_DATADIR=/application/mysql-5.6.38/data \
#socket文件存放位置
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.38/tmp/mysql.sock \
... ...
2.初始化
/mysql_install_db --defaults-file=/service/3309/my.cnf --user=mysql --basedir=/service/mysql --datadir=/service/3309/data
--datadir=/application/mysql/data
--basedir=/application/mysql
--defaults-file=/etc/my.cnf
--pid-file=/application/mysql/data/db01.pid
--socket=/application/mysql/data/mysql.sock
--user=mysql
--port=3306
--log-error=/application/mysql/data/db01.err
3.配置文件
/etc/my.cnf
/etc/mysql/my.cnf
$basedir/my.cnf
~/my.cnf
#defaults-extra-file (类似include)
2.数据库配置文件
1)配置文件都有哪些
/etc/my.cnf
/etc/mysql/my.cnf
$basedir/my.cnf
~/.my.cnf
2)配置文件的读取顺序
/etc/my.cnf
/etc/mysql/my.cnf
$basedir/my.cnf
~/.my.cnf
3)配置文件生效顺序
~/.my.cnf
$basedir/my.cnf
/etc/mysql/my.cnf
/etc/my.cnf
4)测试配置文件生效顺序
#1.配置/etc/my.cnf
[root@db02 ~]# vim /etc/my.cnf
[mysqld]
server_id=1
#2.配置/etc/mysql/my.cnf
[root@db02 ~]# vim /etc/mysql/my.cnf
[mysqld]
server_id=2
#3.配置$basedir/my.cnf
[root@db02 ~]# vim /service/mysql/my.cnf
[mysqld]
server_id=3
#4.配置~/my.cnf
[root@db02 ~]# vim ~/.my.cnf
[mysqld]
server_id=4
#5.重启数据库
[root@db02 ~]# systemctl stop mysqld
[root@db02 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
#6.查看server_id
[root@db02 ~]# mysql -uroot -p123456 -e "show variables like 'server_id'"
Warning: Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 4 |
+---------------+-------+
#7.删除生效的配置重启,再次查看id
[root@db02 ~]# rm -rf ~/.my.cnf
[root@db02 ~]# /etc/init.d/mysqld restart
[root@db02 ~]# mysql -uroot -p123456 -e "show variables like 'server_id'"
Warning: Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 3 |
+---------------+-------+
#8.删除生效的配置重启,再次查看id
[root@db02 ~]# rm -rf /service/mysql/my.cnf
[root@db02 ~]# /etc/init.d/mysqld restart
[root@db02 ~]# mysql -uroot -p123456 -e "show variables like 'server_id'"
Warning: Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 2 |
+---------------+-------+
#9.删除生效的配置重启,再次查看id
[root@db02 ~]# rm -rf /etc/mysql/my.cnf
[root@db02 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@db02 ~]# mysql -uroot -p123456 -e "show variables like 'server_id'"
Warning: Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
3.执行参数优先级
1)socket文件指定位置
#cmake:
socket=/application/mysql/tmp/mysql.sock
#命令行:
--socket=/tmp/mysql.sock
#配置文件:
/etc/my.cnf 中[mysqld]标签下:socket=/opt/mysql.sock
#default参数:
--defaults-file=/tmp/a.txt 配置文件中[mysqld]标签下:socket=/tmp/test.sock
2)测试优先级
#1.启动MySQL,查看sock文件位置
[root@db02 ~]# mysqld_safe --defaults-file=/tmp/a.txt --socket=/tmp/mysql.sock &
[root@db02 ~]# ll /tmp/
srwxrwxrwx 1 mysql mysql 0 Oct 22 18:58 mysql.sock
#2.启动MySQL,查看sock文件位置
[root@db02 ~]# mysqld_safe --defaults-file=/tmp/a.txt &
[root@db02 ~]# ll /tmp/
srwxrwxrwx 1 mysql mysql 0 Oct 22 19:01 test.sock
#如果设置--defaults-file,那么MySQL启动时只读取指定的配置文件
#3.启动MySQL,查看sock文件位置
[root@db02 ~]# mysqld_safe &
[root@db02 ~]# ll /opt/
srwxrwxrwx 1 mysql mysql 0 Oct 22 19:03 mysql.sock
4.优先级总结
#优先级排序
命令行 > --defaults-file参数指定配置 > ~/.my.cnf > $basedir/my.cnf > /etc/mysql/my.cnf > /etc/my.cnf > 初始化配置 > cmake
5.配置文件作用
1)作用
1.影响客户端的连接
2.影响服务端的启动
2)影响客户端的连接
[root@db02 ~]# vim /etc/my.cnf
#配置文件添加连接数据库信息的配置
[mysql]
user=root
password=123456
3)影响服务端的启动
#修改server_id
[root@db02 ~]# vim /etc/my.cnf
[mysqld]
server_id=2
#查看配置没有生效
[root@db02 ~]# mysql -e "show variables like 'server_id'"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
#重启数据库后,修改的服务端配置才会生效
[root@db02 ~]# systemctl restart mysqld
[root@db02 ~]# mysql -e "show variables like 'server_id'"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 2 |
+---------------+-------+
4)总结
1.客户端配置受配置文件影响
2.客户端配置修改后不需要重启服务 [mysql] 或者 [client]
3.服务端配置修改后需要重启服务后生效 [mysqld] 或者 [server]
4.常见配置
[root@db02 ~]# vim /etc/my.cnf
[mysqld]
server_id=2
socket=/tmp/mysql.sock
[mysql]
socket=/tmp/mysql.sock