标签 mysql 下的文章

yosemite

大家在win下和Linux系统下配置PHP运行环境已经有很多参考资料了,以老高的经验,win下最好用的是UPUPW,linux求方便是centos+kangle,更加复杂的方法可以翻翻老高的文章归档,里面有很多可以参考的内容。

今天由于工作需要,必须在OSX下配置PHP的开发运行环境,经过一番折腾,终于搞定了!主要参考了Install Nginx, PHP-FPM, MySQL and phpMyAdmin on OS X Mavericks or Yosemite这篇文章,推荐英文好的同学直接看原文。

下面记录一下!(请按照顺序配置)

阅读剩余部分

如果你选mysql数据库作为数据持久化的工具,那么就需要一个合理的日志配置,这样有助于排错和数据备份及恢复!

首先我们可以通过下面的MySQL的sql查询正在运行中的日志配置。

刚好我们熟悉一下SHOW VARIABLES LIKE的用法!这个命令是用来查询MySQL运行时配置的语句,LIKE后面的可以接通配符查找!

还有对应的设置语法叫SET GLOBAL,后面我们会用到。

mysql> SHOW VARIABLES LIKE '%log%';
+-----------------------------------------+---------------------------------+
| Variable_name                           | Value                           |
+-----------------------------------------+---------------------------------+
| back_log                                | 50                              |
| binlog_cache_size                       | 32768                           |
| binlog_direct_non_transactional_updates | OFF                             |
| binlog_format                           | STATEMENT                       |
| expire_logs_days                        | 0                               |
| general_log                             | OFF                             |
| general_log_file                        | /var/run/mysqld/mysqld.log      |
| innodb_flush_log_at_trx_commit          | 1                               |
| innodb_locks_unsafe_for_binlog          | OFF                             |
| innodb_log_buffer_size                  | 1048576                         |
| innodb_log_file_size                    | 5242880                         |
| innodb_log_files_in_group               | 2                               |
| innodb_log_group_home_dir               | ./                              |
| innodb_mirrored_log_groups              | 1                               |
| log                                     | OFF                             |
| log_bin                                 | OFF                             |
| log_bin_trust_function_creators         | OFF                             |
| log_bin_trust_routine_creators          | OFF                             |
| log_error                               | /var/log/mysqld.log             |
*| log_output                              | FILE                            |*
| log_queries_not_using_indexes           | OFF                             |
| log_slave_updates                       | OFF                             |
| log_slow_queries                        | OFF                             |
| log_warnings                            | 1                               |
| max_binlog_cache_size                   | 18446744073709547520            |
| max_binlog_size                         | 1073741824                      |
| max_relay_log_size                      | 0                               |
| relay_log                               |                                 |
| relay_log_index                         |                                 |
| relay_log_info_file                     | relay-log.info                  |
| relay_log_purge                         | ON                              |
| relay_log_space_limit                   | 0                               |
| slow_query_log                          | OFF                             |
| slow_query_log_file                     | /var/run/mysqld/mysqld-slow.log |
| sql_log_bin                             | ON                              |
| sql_log_off                             | OFF                             |
| sql_log_update                          | ON                              |
| sync_binlog                             | 0                               |
+-----------------------------------------+---------------------------------+
38 rows in set (0.00 sec)

请注意log_output一行,次配置决定将日志输出到文件还是table中。

我们可以通过SHOW VARIABLES LIKE 'log_output';查询此配置。

通过文件配置

mysql的配置文件位于 /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
# 错误日志
log-error=/var/log/mysql/error_mysql.log
# Slow Query
log-slow-queries=/var/log/mysql/slow_mysql.log
long_query_time=2
# 记录没有使用索引的查询
#log-queries-not-using-indexes

# 常规查询日志,老高主要用于调试PDO参数绑定的查询
general_log=1
general_log_file=/var/log/mysql/general_mysql.log
# < MySql5.1.12
#log = /var/log/mysql/general_mysql.log


[mysqld_safe]
# syslog指将日志记录至系统日志
#syslog
pid-file=/var/run/mysqld/mysqld.pid
log_error=/var/log/mysql/error_mysqld.log

接下来需要重启服务器即可是配置生效!

热配置

有时候可能想在不重启mysql的方式开启日志,怎么办?

当然我们可以通过MySQL的sql查询开启。

以下操作假设你已经以root方式登录进入MySQL交互界面

常规日志

# 查看常规日志配置
SHOW VARIABLES LIKE 'general_log%';

下面我们开启他

# 设置日志文件路径,请先保证路径存在
SET GLOBAL general_log_file='/var/log/mysql/general_mysql.log';
# 开启日志
SET GLOBAL general_log=ON;

# 关闭日志
SET GLOBAL general_log=OFF;

查看常规日志配置

错误日志

聪明的朋友,既然已经知道SET GLOBALSHOW VARIABLES LIKE这两个命令,我相信其他的日志配置就难不倒你了。

所以在此老高就不啰嗦了。

mysql的默认的root用户会有很多行,自习观察后你就会发现每行的用户名或密码可能相同,但是host一定不同,host是登陆用户的主机名,也就是说,'localhost','127.0.0.1','phpgao.local','%'都算不同的用户!

理解了这一点后,那么我的问题就附上水面了!

有些TX经常会遇到这个问题:

使用PHP连接mysql数据库,使用localhost作为主机名总是连接失败,但是使用'127.0.0.1'就可以顺利连接,这到底是为什么?

mysql中HOST为localhost和127.0.0.1到底有什么区别?

经过一番搜索,老高总结如下:

使用到的命令

mysql>status;
mysql>show grants;
  1. 类Unix系统下,如果不使用-h指定主机名或者使用了localhost,那么会使用unix domain socket与mysql服务器沟通,比TCP/IP快一些!所以你想使用TCP/IP协议,请将host指定为'127.0.0.1'。

  2. PHP连接mysql如果使用'localhost'发生问题,首先可以明确的是PHP会试着使用unix domain socket与服务器连接,所以请检查php.ini中mysql.default_socket = /var/mysql/mysql.sock是否配置正确。

  3. 如果想要明确连接方式,可以再配置文件中显式声明

protocol=tcp
  1. 在mysql的官方文档中解释道:如果mysql在win上跑,如果系统开启了--enable-named-pipe,然后访问服务器的时候没有指定hostname,那么mysql客户端会以pipe为优先连接,如果连接失败,那么再会去尝试使用TCP/IP去连接。你可以指定hostname为.在win下强制使用pipes。

If the MySQL server is running on Windows, you can connect using TCP/IP. If the server is started with the --enable-named-pipe option, you can also connect with named pipes if you run the client on the host where the server is running. The name of the named pipe is MySQL by default. If you do not give a host name when connecting to mysqld, a MySQL client first tries to connect to the named pipe. If that does not work, it connects to the TCP/IP port. You can force the use of named pipes on Windows by using . as the host name.

Reference:

http://stackoverflow.com/questions/19712307/mysql-localhost-127-0-0-1 http://stackoverflow.com/questions/3715925/localhost-vs-127-0-0-1 http://dev.mysql.com/doc/refman/5.5/en/can-not-connect-to-server.html http://madproject.com/general/connect-to-mysql-using-localhost-instead-of-127-0-0-1-on-a-mac/ http://stackoverflow.com/questions/9714899/php-mysql-difference-between-127-0-0-1-and-localhost http://superuser.com/questions/744972/connecting-to-mysql-from-127-0-0-1-instead-of-from-localhost http://blog.csdn.net/xifeijian/article/details/12879395 http://blog.csdn.net/topasstem8/article/details/18357789