标签 MySQL 下的文章

Centos上Apache重启,mysql重启,nginx重启方法

Centos上Apache重启,mysql重启, nginx 重启方法

  1.重启 apache

  service httpd restrat

  /etc/init.d/httpd stop

  /etc/init.d/httpd start

  2.重启 mysql

  service mysql restart

  /etc/init.d/mysqld stop

  /etc/init.d/mysqld start

  3.重启Nginx

  service nginx restart

  /etc/init.d/nginx stop

  /etc/init.d/nginx start

WordPress显示数据库查询次数和查询花费时间

WordPress提供了一些功能函数可以轻松地显示数据库查询的统计信息,这些信息可以公开地显示在网页中,或者隐藏在源代码中,更或者只有你自己可以看到。

WordPress主要提供了两个统计函数:

1.网页加载时查询数据库的次数:

 

2.服务器端完成这些查询所花费的时间:

 

可以使用3种方式在网页中显示:

1. 公开地显示查询的统计信息:

如果你觉的你的服务器或空间的处理速度好,或者想让你的访问者看到这些查询统计信息的话,你可以将这些统计信息公开显示在页面中:(效果查看博客的页面底部)

 

2. 将统计信息显示在源代码中,而不出现页面的内容中:

如果你不想将统计信息显示在页面上供访问者查看,但又想自己可以知道这些统计信息,那么你可以通过html的注释将结果只显示在源代码中(PS:大家也可以从源代码中查看的哦!):

 

3. 统计信息只有你自己登录了后可以看到:

该方法是第二种显示的优化方法,只需博客管理员登录后就可以查看查询统计信息,访问者是无法查看的:

 

如果想查看具体查询了数据库哪些内容,可以用如下解决方法:

1、首先在 wp-config.php 里添加如下代码:

 

然后在 footer.php 里添加如下代码:

 

分析:

1、if (is_user_logged_in()) 用于判断当前访客是否已登录,也可以用 if (current_user_can('level_10')) 来判断是否为管理员登录,目的是为了不让游客查看到这些数据,此代码可省;

2、global $wpdb; 定义全局变量$wpdb,这是Wordpress默认的数据库类;

3、<pre></pre>将结果嵌套在HTML标签<pre>内;

4、print_r($wpdb->queries); 输出各次数据库查询的信息。

刷新首页或日志页,可看到类似如下的输出结果:

 

下面的活儿就得自己分析了,看看哪些是可以删除,哪些是可以改进的,以减少数据库查询次数。

 

MyISAM InnoDB 区别

MyISAM 和 InnoDB 讲解

  InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型,这两个表类型各有优劣,视具体应用而定。基本的差别为:MyISAM类型不支持事务处理等高级处理,而InnoDB类型支持。MyISAM类型的表强调的是性能,其执行数度比InnoDB类型更快,但是不提供事务支持,而InnoDB提供事务支持已经外部键等高级数据库功能。

  以下是一些细节和具体实现的差别:

  ◆1.InnoDB不支持FULLTEXT类型的索引。

  ◆2.InnoDB 中不保存表的具体行数,也就是说,执行select count(*) from table时,InnoDB要扫描一遍整个表来计算有多少行,但是MyISAM只要简单的读出保存好的行数即可。注意的是,当count(*)语句包含 where条件时,两种表的操作是一样的。

  ◆3.对于AUTO_INCREMENT类型的字段,InnoDB中必须包含只有该字段的索引,但是在MyISAM表中,可以和其他字段一起建立联合索引。

  ◆4.DELETE FROM table时,InnoDB不会重新建立表,而是一行一行的删除。

  ◆5.LOAD TABLE FROM MASTER操作对InnoDB是不起作用的,解决方法是首先把InnoDB表改成MyISAM表,导入数据后再改成InnoDB表,但是对于使用的额外的InnoDB特性(例如外键)的表不适用。

  另外,InnoDB表的行锁也不是绝对的,假如在执行一个SQL语句时MySQL不能确定要扫描的范围,InnoDB表同样会锁全表,例如update table set num=1 where name like “%aaa%”

  两种类型最主要的差别就是Innodb 支持事务处理与外键和行级锁.而MyISAM不支持.所以MyISAM往往就容易被人认为只适合在小项目中使用。

  我作为使用MySQL的用户角度出发,Innodb和MyISAM都是比较喜欢的,但是从我目前运维的数据库平台要达到需求:99.9%的稳定性,方便的扩展性和高可用性来说的话,MyISAM绝对是我的首选。

  原因如下:

  1、首先我目前平台上承载的大部分项目是读多写少的项目,而MyISAM的读性能是比Innodb强不少的。

  2、MyISAM的索引和数据是分开的,并且索引是有压缩的,内存使用率就对应提高了不少。能加载更多索引,而Innodb是索引和数据是紧密捆绑的,没有使用压缩从而会造成Innodb比MyISAM体积庞大不小。

  3、从平台角度来说,经常隔1,2个月就会发生应用开发人员不小心update一个表where写的范围不对,导致这个表没法正常用了,这个时候MyISAM的优越性就体现出来了,随便从当天拷贝的压缩包取出对应表的文件,随便放到一个数据库目录下,然后dump成sql再导回到主库,并把对应的binlog补上。如果是Innodb,恐怕不可能有这么快速度,别和我说让Innodb定期用导出xxx.sql机制备份,因为我平台上最小的一个数据库实例的数据量基本都是几十G大小。

  4、从我接触的应用逻辑来说,select count(*) 和order by 是最频繁的,大概能占了整个sql总语句的60%以上的操作,而这种操作Innodb其实也是会锁表的,很多人以为Innodb是行级锁,那个只是where对它主键是有效,非主键的都会锁全表的。

  5、还有就是经常有很多应用部门需要我给他们定期某些表的数据,MyISAM的话很方便,只要发给他们对应那表的frm.MYD,MYI的文件,让他们自己在对应版本的数据库启动就行,而Innodb就需要导出xxx.sql了,因为光给别人文件,受字典数据文件的影响,对方是无法使用的。

  6、如果和MyISAM比insert写操作的话,Innodb还达不到MyISAM的写性能,如果是针对基于索引的update操作,虽然MyISAM可能会逊色Innodb,但是那么高并发的写,从库能否追的上也是一个问题,还不如通过多实例分库分表架构来解决。

  7、如果是用MyISAM的话,merge引擎可以大大加快应用部门的开发速度,他们只要对这个merge表做一些select count(*)操作,非常适合大项目总量约几亿的rows某一类型(如日志,调查统计)的业务表。

  当然Innodb也不是绝对不用,用事务的项目如模拟炒股项目,我就是用Innodb的,活跃用户20多万时候,也是很轻松应付了,因此我个人也是很喜欢Innodb的,只是如果从数据库平台应用出发,我还是会首选MyISAM。

  另外,可能有人会说你MyISAM无法抗太多写操作,但是我可以通过架构来弥补,说个我现有用的数据库平台容量:主从数据总量在几百T以上,每天十多亿 pv的动态页面,还有几个大项目是通过数据接口方式调用未算进pv总数,(其中包括一个大项目因为初期memcached没部署,导致单台数据库每天处理 9千万的查询)。而我的整体数据库服务器平均负载都在0.5-1左右。

在iPhone/iPad中搭建web服务器和PHP开发环境:Lighttpd + PHP(with GD) + MySQL

1. Install the Package in Cydia Add the Cydia Source http://ios-webstack.tk/cydia, find and install the package ios-lighttpd-php-mysql. Your lighttpd web server is now running. You can test it in Safari. But you need to do some more work with php and mysql.
2. Mysql Configuration
Log in your IOS device and execute the following shell commands.
# bin/mysql_install_db –user=daemon
# /usr/local/bin/mysqladmin -u root password ‘new-password’
# /usr/local/bin/mysql_secure_installation
执行mysql_secure_installation
Change the root password? [Y/n]
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] n
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] n
# mysql -u root -p’yourpassword’
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection idis 8
Server version: 5.1.66 Source distribution
Copyright (c) 2000, 2012, 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.
Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the current input statement.
mysql>
mysql>
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
+——————–+
2 rows in set (0.01 sec)
mysql>
3. PHP Configuration
Create File: /etc/php.ini
;This is an ‘EXAMPLE’ configuration file for PHP.
;Be careful with the socket path
mysql.default_socket = /tmp/mysql.sock
pdo_mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock
;Limits
upload_max_filesize = 10M
post_max_size= 10M
;TimeZone
;You should simply replace ‘Europe/Berlin’ with the desired timezone.
;The full list of supported time zones is available here: http://www.php.net/manual/en/timezones.php
date.timezone = ‘UTC’
;I recommends to place following lines to /etc/php.d/security.ini (it will not be touched
;from a cydia packages update).
;========================================================================================
;Please have a look at http://www.cyberciti.biz/tips/php-security-best-practices-tutorial.html
;It is up to you to comment-out some directives.
;Make sure you log all php errors to a log file.
;Make sure that php and lighttpd have write permissions.
log_errors=Off
error_log=/var/log/lighttpd/php_scripts_error.log
;To restrict PHP information leakage disable expose_php.
;Have a check befor and after: curl -I http://your.iOS-Web.server/yourPHPsite.php
expose_php=Off
;Do not expose PHP error messages toall site visitors.
; display_errors=Off
;The allow_url_fopen option allows PHP’s file functions – such as file_get_contents() and the
;include and require statements – can retrieve data from remote locations using ftp or http
;protocols. Programmers frequently forget this and don’t do proper input filtering when passing
;user-provided data to these functions, opening them up to code injection vulnerabilities.
;A large number of code injection vulnerabilities reported in PHP-based web applications are
;caused by the combination of enabling allow_url_fopen and bad input filtering.
; allow_url_fopen=Off
;I also recommend to disable allow_url_include for security reasons:
; allow_url_include=Off
;If turned On, mysql_connect() and mysql_pconnect() ignore any arguments passed to them.
;Please note that you may have to make some changes to your code. Third party and open source
;application such as WordPress, and others may not work at all when sql.safe_mode enabled.
; sql.safe_mode=On
;It is also recommended that you turn off magic_quotes_gpc for all php 5.3.x installations
;as the filtering by it is ineffective and not very robust. mysql_escape_string() and
;custom filtering functions serve a better purpose (hat tip to Eric Hansen):
; magic_quotes_gpc=Off
;You can set maximum execution time of each php script, in seconds.
;Another recommend option is to set maximum amount of time each script
;may spend parsing request data, and maximum amount of memory a script may consume.
;GD picture converting needs a lot of memory. Be carefull with ‘memory_limit’
;Set in seconds:
; max_execution_time = 30
; max_input_time = 30
; memory_limit = 40M
The packages include configuration files for PHP in /etc/ and extension configuration files in /etc/php.d/
Create File: /etc/php.d/php.ini
;You can splite this file in separate INI files.
;If you do that, please remember the extension loading
;order of INI files (alphabetically).
extension=iconv.so
extension=mbstring.so
extension=curl.so
;The loading order is important
extension=sqlite3.so
extension=pdo_sqlite.so
;The loading order is important
extension=mysqlnd.so
extension=mysqli.so
extension=mysql.so
extension=pdo_mysql.so
;Must be loaded after mbstring.so
extension=exif.so
extension=gd.so
extension=zip.so
extension=bz2.so
extension=calendar.so
extension=mcrypt.so
extension=xsl.so
extension=openssl.so
Finally, input http://127.0.0.1/phpinfo.php in Safari, you’ll get a long PHP information page like this:
PHP Version 5.4.8 System
Darwin mengtaos-iPhone 10.4.0 Darwin Kernel Version 10.4.0: Wed Oct 20 20:08:31 PDT 2010; root:xnu-1504.58.28~3/RELEASE_ARM_S5L8920X iPhone2,1
Build Date
Nov 14 2012 17:33:08
Configure Command
‘./configure’
Server API
CGI/FastCGI
Virtual Directory Support
disabled
Configuration File (php.ini) Path
/etc
Loaded Configuration File
/etc/php.ini
Scan this dir for additional .ini files
/etc/php.d
Now, Enjoy this little web server!
2024年5月
« 2月    
 12345
6789101112
13141516171819
20212223242526
2728293031  

广告

分类目录

近期评论

标签

历史上的今天

文章归档