【10.22】php-fpm配置,mariadb密码、慢查询日志

【10.22】php-fpm配置,mariadb密码、慢查询日志

  • 4.52-55 php-fpm 配置
  • 4.56 MariaDB 密码重置
  • 4.57 MariaDB 慢查询日志

4.52-55 php-fpm 配置

  • 如果在虚拟主机配置文件中,fastcgi_pass 的 ip 和端口配置错误,那么访问网站时会出现 502 Bad Gateway 错误

  • /usr/local/php-fpm/sbin/php-fpm -t 检查 php-fpm 配置文件语法

    /etc/init.d/php-fpm reload 重新载入 php-fpm 配置

    /etc/init.d/php-fpm start | restart | stop 启动、重启、停止 php-fpm 服务

  • php-fpm 配置文件

1、主配置文件

[root@alexis-01 ~]# ll /usr/local/php-fpm/etc/php-fpm.conf
-rw-r--r-- 1 root root 5398 10月 22 20:41 /usr/local/php-fpm/etc/php-fpm.conf

2、子配置文件
主配置文件中,定义了真正的配置文件在 include=/usr/local/php-fpm/etc/php-fpm.d/*.conf

[root@alexis-01 ~]# cd /usr/local/php-fpm/etc/php-fpm.d/
[root@alexis-01 php-fpm.d]# ll
总用量 40
-rw-r--r--. 1 root root 19244 10月 22 20:51 www.conf

www.conf 就是其中子配置文件


  • www.conf 配置讲解
    pool 名字: [www] 可以自定义,启动后,ps aux |grep php-fpm 看最右侧,就是 pool 的名字

    user = php-fpm,group = php-fpm 指 [www] 的池子,监听的服务,使用的用户和组是什么,用户名在 ps aux |grep php-fpm 最左侧

    listen 指定监听的 ip:port 或者 socket 地址
    这个地址需要和 nginx 配置文件里面的那个 fastcgi_pass 所制定的地址一致,否则就会 502

    · 监听 socket 文件格式:listen = /tmp/www.socket,那么nginx 虚拟主机文件配置中,fastcgi_pass unix:/tmp/www.socket;
    · 如果监听的是 socket 文件,那么要保证 nginx 服务用户(nginx)对该 socket 文件有读写权限,否则 502,修改 php-fpm 配置文件的 listen.mode = 0666,改完必须重启 php-fpm,重启完权限才会更改

    listen.mode 指定 socket 文件的权限

    listen.allowed_clients = 127.0.0.1 谁允许访问 php-fpm 服务

    pm = dynamic 动态模式,可以动态调整进程个数

    pm.max_children = 5 最大进程数

    pm.start_servers = 2 启动几个子进程

    pm.min_spare_servers = 1 空闲时,最少不能少于几个子进程

    pm.max_spare_servers = 3 空闲时,最多不能多于几个子进程

    pm.max_requests = 500 单个子进程最多处理多少个请求

    php_flag[display_errors] = off php 文件错误是否会显示在浏览器里,一般不打开,不仅用户体验不好,而且会暴露文件路径

    php_admin_value[error_log] = /var/log/fpm-php.www.log 如果php_flag 不想为 on,那么配置个错误日志

    php_admin_flag[log_errors] = on 配置错误日志的话这里要 on

    php_admin_value[error_reporting] = E_ALL 错误日志的级别


  • 查看php.ini路径:
    1) /usr/local/php-fpm/bin/php -i |head
[root@alexis-01 ~]# /usr/local/php-fpm/bin/php -i|head
phpinfo()
PHP Version => 7.3.0

System => Linux alexis-01 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64
Build Date => Oct 16 2020 06:22:10
Configure Command =>  './configure'  '--prefix=/usr/local/php-fpm' '--with-config-file-path=/usr/local/php-fpm/etc' '--enable-fpm' '--with-fpm-user=php-fpm' '--with-fpm-group=php-fpm' '--with-mysql=/usr/local/mysql5.6' '--with-mysqli=/usr/local/mysql5.6/bin/mysql_config' '--with-pdo-mysql=/usr/local/mysql5.6' '--with-mysql-sock=/tmp/mysql.sock' '--with-libxml-dir' '--with-gd' '--with-jpeg-dir' '--with-png-dir' '--with-freetype-dir' '--with-iconv-dir' '--with-zlib-dir' '--with-mcrypt' '--enable-soap' '--enable-gd-native-ttf' '--enable-ftp' '--enable-mbstring' '--enable-exif' '--with-pear' '--with-curl' '--with-openssl'
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /usr/local/php-fpm/etc
Loaded Configuration File => /usr/local/php-fpm/etc/php.ini

2)用phpinfo

[root@alexis-01 ~]# vim phpinfo.php
<?php
phpinfo();
?>

网站访问 xxxx/phpinfo.php,就能显示
【10.22】php-fpm配置,mariadb密码、慢查询日志_第1张图片
不过第二种最好不要用,如果被人利用很容易出事
在 /usr/local/php-fpm/etc/php.ini 中,修改 disable_functions = phpinfo,页面是白页
错误信息会显示在日志中

[root@alexis-01 ~]# tail /var/log/fpm-php.www.log 
[22-Oct-2019 14:54:58 UTC] PHP Warning:  phpinfo() has been disabled for security reasons in /data/wwwroot/bbs.ars4life.com/phpinfo.php on line 2

curl -k -H “host:bbs.ars4lifen.com” https://127.0.0.1/phpinfo.php

  • 配置 slow log
    slowlog = /tmp/php.slow 文件位置
    request_slowlog_timeout = 1 超时时间
[root@alexis-01 ~]# cat /tmp/php.slow 

[22-Oct-2019 23:09:50]  [pool www] pid 8594
script_filename = /data/wwwroot/bbs.ars4life.com/phpinfo.php
[0x00007faa9f6200a0] sleep() /data/wwwroot/bbs.ars4life.com/phpinfo.php:3
  • open_basedir
    用来定义 php-fpm 服务,允许在某个路径下活动

    配置 open_basedir:
    · 可以在 /usr/local/php-fpm/etc/php.ini 里定义 open_basedir,可以定义多个网站的目录,但是不安全
    · 建议在 /usr/local/php-fpm/etc/php-fpm.d/www.conf 中定义以下参数:
    php_admin_value[open_basedir] = /data/wwwroot/bbs.ars4life.com:/tmp
    /tmp 下是网站会生成一些临时文件的目录

如果要两个网站相互不影响,需要再定义一个 pool

  • 定义多个 pool
    定义多个配置文件,在配置文件中指定不同的 listen 地址,不同的 [pool_name]

1、先摘取 [www] pool 的参数作为参考

[root@alexis-01 ~]# grep -v '^;' /usr/local/php-fpm/etc/php-fpm.d/www.conf |grep -v '^$'
[www]
user = php-fpm
group = php-fpm
listen = /tmp/www.socket
listen.mode = 0666
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
slowlog = /tmp/php.slow
request_slowlog_timeout = 1
php_flag[display_errors] = off
php_admin_value[error_log] = /var/log/fpm-php.www.log
php_admin_flag[log_errors] = on
php_admin_value[error_reporting] = E_ALL
php_admin_value[open_basedir] = /data/wwwroot/bbs.ars4life.com:/tmp

2、新建新的 [bbs] pool

[root@alexis-01 php-fpm.d]# vim blog.conf
[blog]
user = php-fpm
group = php-fpm
listen = /tmp/blog.socket
listen.mode = 0666
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
slowlog = /tmp/php.slow
request_slowlog_timeout = 1
php_flag[display_errors] = off
php_admin_value[error_log] = /var/log/fpm-php.www.log
php_admin_flag[log_errors] = on
php_admin_value[error_reporting] = E_ALL
php_admin_value[open_basedir] = /data/wwwroot/blog.ars4life.com:/tmp
[root@alexis-01 php-fpm.d]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@alexis-01 php-fpm.d]# ll /tmp/ |grep blog
srw-rw-rw-  1 root  root    0 10月 23 12:17 blog.socket
[root@alexis-01 php-fpm.d]# ps aux|grep php-fpm
root       7378  0.0  0.6 230592  6360 ?        Ss   12:17   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm    7379  0.0  0.6 230584  6352 ?        S    12:17   0:00 php-fpm: pool blog
php-fpm    7380  0.0  0.6 230584  6352 ?        S    12:17   0:00 php-fpm: pool blog
php-fpm    7381  0.0  0.6 230584  6352 ?        S    12:17   0:00 php-fpm: pool www
php-fpm    7382  0.0  0.6 230584  6352 ?        S    12:17   0:00 php-fpm: pool www
root       7393  0.0  0.0 112728   976 pts/0    R+   12:21   0:00 grep --color=auto php-fpm

★★★ php.ini 中定义的参数,都可以在 /usr/local/php-fpm/etc/php-fpm.d/ 中的 .conf 文件中定义
php_admin_value [ 参数 ] = 值
如果是 on 或 off 一类的,需要时 php_admin_flag [ 参数 ] = on/off
★★ php_admin_flag 和 php_flag 都可以
如果即在 php.ini 中,又在 php-fpm 中定义,那么以 php-fpm 为准,php-fpm 优先级更高

4.56 MariaDB 密码重置

1、如果记得root的密码:
mysqladmin -uroot -paminglinux password “aming-linux”

2、如果不记得root密码:
1)编辑 /etc/my.cnf
增加:skip-grant
重启 mysqld 服务

[root@alexis-01 ~]# vim /etc/my.cnf
skip-grant
[root@alexis-01 ~]# /etc/init.d/mysqld restart
Restarting mysqld (via systemctl):                         [  确定  ]

2)登录进MariaDB,执行
use mysql 切换到mysql库
desc user 查看user表的所有字段
update user set authentication_string=password(“123456”) where user=‘root’;

[root@alexis-01 ~]# mysql -uroot
MariaDB [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> desc user;
+------------------------+-----------------------------------+------+-----+----------+-------+
| Field                  | Type                              | Null | Key | Default  | Extra |
+------------------------+-----------------------------------+------+-----+----------+-------+
| Host                   | char(60)                          | NO   | PRI |          |       |
| User                   | char(80)                          | NO   | PRI |          |       |
| Password               | char(41)                          | NO   |     |          |       |
| Select_priv            | enum('N','Y')                     | NO   |     | N        |       |
| Insert_priv            | enum('N','Y')                     | NO   |     | N        |       |
| Update_priv            | enum('N','Y')                     | NO   |     | N        |       |
| Delete_priv            | enum('N','Y')                     | NO   |     | N        |       |
| Create_priv            | enum('N','Y')                     | NO   |     | N        |       |
| Drop_priv              | enum('N','Y')                     | NO   |     | N        |       |
| Reload_priv            | enum('N','Y')                     | NO   |     | N        |       |
| Shutdown_priv          | enum('N','Y')                     | NO   |     | N        |       |
| Process_priv           | enum('N','Y')                     | NO   |     | N        |       |
| File_priv              | enum('N','Y')                     | NO   |     | N        |       |
| Grant_priv             | enum('N','Y')                     | NO   |     | N        |       |
| References_priv        | enum('N','Y')                     | NO   |     | N        |       |
| Index_priv             | enum('N','Y')                     | NO   |     | N        |       |
| Alter_priv             | enum('N','Y')                     | NO   |     | N        |       |
| Show_db_priv           | enum('N','Y')                     | NO   |     | N        |       |
| Super_priv             | enum('N','Y')                     | NO   |     | N        |       |
| Create_tmp_table_priv  | enum('N','Y')                     | NO   |     | N        |       |
| Lock_tables_priv       | enum('N','Y')                     | NO   |     | N        |       |
| Execute_priv           | enum('N','Y')                     | NO   |     | N        |       |
| Repl_slave_priv        | enum('N','Y')                     | NO   |     | N        |       |
| Repl_client_priv       | enum('N','Y')                     | NO   |     | N        |       |
| Create_view_priv       | enum('N','Y')                     | NO   |     | N        |       |
| Show_view_priv         | enum('N','Y')                     | NO   |     | N        |       |
| Create_routine_priv    | enum('N','Y')                     | NO   |     | N        |       |
| Alter_routine_priv     | enum('N','Y')                     | NO   |     | N        |       |
| Create_user_priv       | enum('N','Y')                     | NO   |     | N        |       |
| Event_priv             | enum('N','Y')                     | NO   |     | N        |       |
| Trigger_priv           | enum('N','Y')                     | NO   |     | N        |       |
| Create_tablespace_priv | enum('N','Y')                     | NO   |     | N        |       |
| Delete_history_priv    | enum('N','Y')                     | NO   |     | N        |       |
| ssl_type               | enum('','ANY','X509','SPECIFIED') | NO   |     |          |       |
| ssl_cipher             | blob                              | NO   |     | NULL     |       |
| x509_issuer            | blob                              | NO   |     | NULL     |       |
| x509_subject           | blob                              | NO   |     | NULL     |       |
| max_questions          | int(11) unsigned                  | NO   |     | 0        |       |
| max_updates            | int(11) unsigned                  | NO   |     | 0        |       |
| max_connections        | int(11) unsigned                  | NO   |     | 0        |       |
| max_user_connections   | int(11)                           | NO   |     | 0        |       |
| plugin                 | char(64)                          | NO   |     |          |       |
| authentication_string  | text                              | NO   |     | NULL     |       |
| password_expired       | enum('N','Y')                     | NO   |     | N        |       |
| is_role                | enum('N','Y')                     | NO   |     | N        |       |
| default_role           | char(80)                          | NO   |     |          |       |
| max_statement_time     | decimal(12,6)                     | NO   |     | 0.000000 |       |
+------------------------+-----------------------------------+------+-----+----------+-------+
47 rows in set (0.003 sec)

MariaDB [mysql]> update user set authentication_string=password('123456') where user='root';
Query OK, 4 rows affected (0.002 sec)
Rows matched: 4  Changed: 4  Warnings: 0

3)退出MariaDB,删除 /etc/my.cnf 里面的 skip-grant, 重启服务

4)用新密码登录即可

  • 常识:
    mysql 在 5.7.36 版本之后把密码字段存到了authentication_string 字段里,在之前版本存在 password 字段里。
    update user set password=password(“aming-linux”) where user=‘root’;

4.57 MariaDB 慢查询日志

  • 为什么要配置慢查询日志?
    目的是为了帮助我们分析 MariaDB 的瓶颈点。

  • 如何配置?
    1)进入 MariaDB 里面执行:
    show variables like ‘slow%’;
    show variables like ‘datadir’;
    show variables like ‘long%’;

    2)打开配置文件 /etc/my.cnf,编辑,增加:
    slow_query_log = ON
    slow_query_log_file = /data/mysql/alexis-01-slow.log
    long_query_time = 2

    3)重启服务 /etc/init.d/mysqld restart

    4)模拟慢查询
    select sleep(5);

    5)查看慢查询日志:cat /data/mysql/alexis-01-slow.log

[root@alexis-01 ~]# cat /data/mysql/alexis-01-slow.log 
/usr/local/mysql/bin/mysqld, Version: 10.3.11-MariaDB-log (MariaDB Server). started with:
Tcp port: 0  Unix socket: /tmp/mysql.sock
Time		    Id Command	Argument
# Time: 191023 12:58:19
# User@Host: root[root] @ localhost []
# Thread_id: 9  Schema:   QC_hit: No
# Query_time: 5.001154  Lock_time: 0.000000  Rows_sent: 1  Rows_examined: 0
# Rows_affected: 0  Bytes_sent: 63
SET timestamp=1571806699;
select sleep(5);
  • 总结:
    show variables like ’ 参数 ’ 查看某个参数
MariaDB [mysql]> show variables like 'slow%';
+---------------------+--------------------+
| Variable_name       | Value              |
+---------------------+--------------------+
| slow_launch_time    | 2                  |
| slow_query_log      | OFF                |
| slow_query_log_file | alexis-01-slow.log |
+---------------------+--------------------+
3 rows in set (0.001 sec)

MariaDB [mysql]> show variables like 'datadir';
+---------------+--------------+
| Variable_name | Value        |
+---------------+--------------+
| datadir       | /data/mysql/ |
+---------------+--------------+
1 row in set (0.001 sec)

MariaDB [mysql]> show variables like 'long%';
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.001 sec)

- 扩展:

  • show processlist;
  • show full processlist;
MariaDB [(none)]> show processlist;
+----+-------------+-----------+------+---------+------+--------------------------+------------------+----------+
| Id | User        | Host      | db   | Command | Time | State                    | Info             | Progress |
+----+-------------+-----------+------+---------+------+--------------------------+------------------+----------+
|  1 | system user |           | NULL | Daemon  | NULL | InnoDB purge worker      | NULL             |    0.000 |
|  2 | system user |           | NULL | Daemon  | NULL | InnoDB purge coordinator | NULL             |    0.000 |
|  3 | system user |           | NULL | Daemon  | NULL | InnoDB purge worker      | NULL             |    0.000 |
|  4 | system user |           | NULL | Daemon  | NULL | InnoDB purge worker      | NULL             |    0.000 |
|  5 | system user |           | NULL | Daemon  | NULL | InnoDB shutdown handler  | NULL             |    0.000 |
| 10 | root        | localhost | NULL | Query   |    0 | Init                     | show processlist |    0.000 |
+----+-------------+-----------+------+---------+------+--------------------------+------------------+----------+
6 rows in set (0.000 sec)

MariaDB [(none)]> show full processlist;
+----+-------------+-----------+------+---------+------+--------------------------+-----------------------+----------+
| Id | User        | Host      | db   | Command | Time | State                    | Info                  | Progress |
+----+-------------+-----------+------+---------+------+--------------------------+-----------------------+----------+
|  1 | system user |           | NULL | Daemon  | NULL | InnoDB purge worker      | NULL                  |    0.000 |
|  2 | system user |           | NULL | Daemon  | NULL | InnoDB purge coordinator | NULL                  |    0.000 |
|  3 | system user |           | NULL | Daemon  | NULL | InnoDB purge worker      | NULL                  |    0.000 |
|  4 | system user |           | NULL | Daemon  | NULL | InnoDB purge worker      | NULL                  |    0.000 |
|  5 | system user |           | NULL | Daemon  | NULL | InnoDB shutdown handler  | NULL                  |    0.000 |
| 10 | root        | localhost | NULL | Query   |    0 | Init                     | show full processlist |    0.000 |
+----+-------------+-----------+------+---------+------+--------------------------+-----------------------+----------+
6 rows in set (0.000 sec)

如果队列超过 100,那就非常忙了
如果语句特别长,那么 show full processlist 就能完整展示

  • mysql -uroot -pxxxx -e “show processlist” 不用到 mysql 中就可以执行命令

你可能感兴趣的:(学习笔记)