Mariadb/MySQL生产环境的my.cnf配置示例
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.针对MySQL主程序mysqld定义参数[mysqld]
1>.skip-networking=1
关闭网络连接,只侦听本地客户端, 所有和服务器的交互都通过一个socket实现,socket的配置存放在"/var/lib/mysql/mysql.sock",可在/etc/my.cnf修改
[root@node102.yinzhengjie.org.cn ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 50 *:3306 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 :::22 :::* [root@node102.yinzhengjie.org.cn ~]# [root@node102.yinzhengjie.org.cn ~]# grep -v ^# /etc/my.cnf #关闭网络,将"skip-networking"的Value设置为1即可。 [mysqld] skip-networking #关闭网络,此处我们只需要把该属性写在这里就表示启用了,除非你显示指定其值为0(表示不启用),如果我们这样写默认是启用了 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock symbolic-links=0 [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid !includedir /etc/my.cnf.d [root@node102.yinzhengjie.org.cn ~]# [root@node102.yinzhengjie.org.cn ~]# systemctl restart mariadb #重启服务,发现MySQL服务在正常运行但是并未对外暴漏端口。 [root@node102.yinzhengjie.org.cn ~]# [root@node102.yinzhengjie.org.cn ~]# ps aux | grep mysql | grep -v grep #MySQL服务处于正常运行状态 root 8556 0.0 0.1 234060 4184 pts/0 S+ 07:50 0:00 mysql mysql 10132 0.0 0.0 113308 1624 ? Ss 10:38 0:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr mysql 10306 0.0 2.0 968688 81204 ? Sl 10:38 0:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/li b64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sockroot 10342 0.0 0.1 234060 4180 pts/1 S+ 10:38 0:00 mysql -uroot -px xxxxxxxxx [root@node102.yinzhengjie.org.cn ~]# [root@node102.yinzhengjie.org.cn ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 128 :::22 :::* [root@node102.yinzhengjie.org.cn ~]# [root@node102.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie #本机基于socket文件依旧是可以正常访问的哟~ Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.64-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> status -------------- mysql Ver 15.1 Distrib 5.5.64-MariaDB, for Linux (x86_64) using readline 5.1 Connection id: 2 Current database: Current user: root@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server: MariaDB Server version: 5.5.64-MariaDB MariaDB Server Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: latin1 Db characterset: latin1 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /var/lib/mysql/mysql.sock Uptime: 19 sec Threads: 1 Questions: 4 Slow queries: 0 Opens: 0 Flush tables: 2 Open tables: 26 Queries per second avg: 0.210 -------------- MariaDB [(none)]>