August 20, 2013
mysql中的表锁的优化
"\u003ch2 id=\"一获取锁等待情况\"\u003e一、获取锁等待情况\u003c/h2\u003e\n\u003cp\u003e可以通过检查table_locks_waited和table_locks_immediate状态变量来分析系统上的表锁定争夺:\nmysql\u0026gt; show status like ‘Table%’;\n+—————————-+———-+\n| Variable_name | Value |\n+—————————-+———-+\n| Table_locks_immediate | 105 |\n| Table_locks_waited | 3 |\n+—————————-+———-+\n2 rows in set (0.00 sec)\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003eTable_locks_immediate\u003c/strong\u003e 表示立即释放MySQL表锁数,\n\u003cstrong\u003eTable_locks_waited\u003c/strong\u003e 表示需要等待的MySQL表锁数\u003c/p\u003e\n\u003cp\u003e如果Table_locks_waited的值比较高,则说明存在着较严重的表级锁争用情况。这时,需要我们对应用做进一步的检查,来确定问题所在。\u003c/p\u003e\n\u003cp\u003e可以通过检查Innodb_row_lock状态变量来分析系统上的行锁的争夺情况:\nmysql\u0026gt; show status …\u003c/p\u003e"
August 12, 2013
golang中结构体的初始化方法的不同用法(new方法)
"\u003cp\u003e自定义一个结构体\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003etype Rect struct {\n x, y float64\n width, height float64\n}\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003e初始化方法:\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003erect1 := new(Rect)\nrect2 := \u0026amp;Rect{}\nrect3 := \u0026amp;Rect{0, 0, 100, 200}\nrect4 := \u0026amp;Rect{width:100, height:200}\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003e注意这几个变量全部为指向Rect结构的指针(\u003cstrong\u003e指针变量\u003c/strong\u003e),因为使用了new()函数和\u0026amp;操作符。\u003c/p\u003e\n\u003cp\u003e而如果使用方法\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003ea := Rect{}\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003e则表示这个是一个Rect{}\u003cstrong\u003e结构类型\u003c/strong\u003e.两者是不一样的.参考代码:\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003efunc main() {\na := Rect{}\na.x = 15\n\nrect1 := \u0026amp;Rect{0, 0, 100, 200}\nrect1.x = 10\n\nfmt.Printf(\u0026#34;%v\\n%T\\n\u0026#34;, a, a)\nfmt.Printf(\u0026#34;%v\\n%T\\n\u0026#34;, rect1, rect1)\n}\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003e运行结果为:\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003e{15 0 0 0}\n main.Rect …\u003c/code\u003e\u003c/pre\u003e"
August 11, 2013
golang中的文档管理
"\u003cp\u003efoo.go\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003e// CopyRight 2013 The Go Author. All Right reserved.\n// Use of this source code is governed by a BSD-style\n// license that can be found in the LICENSE fifle.\n\n/*\nPackage foo implements a set of simple mathematical functions. These comments are for\ndemonstration purpose only. Nothing more.\n\nIf you have any questions,please don’t hesitate to add yourself to\[email protected].\n\nyou can alse visit golang.org for full Go documentation.\n*/\n\npackage foo\n\nimport (\n\t\u0026#34;fmt\u0026#34;\n)\n\n// …\u003c/code\u003e\u003c/pre\u003e"
August 11, 2013
golang中的map数据类型操作实例
"\u003cpre tabindex=\"0\"\u003e\u003ccode\u003epackage main\n\nimport (\n\t\u0026#34;fmt\u0026#34;\n)\n\ntype stu struct {\n\tName string\n\tAge int\n}\n\nfunc main() {\n\n\t// 声明一个map变量student,键名为string,值为stu\n\tvar student map[string]stu\n\n\t// 给map变量创建值,同时指定最多可以存储5个stu值\n\tstudent = make(map[string]stu, 5)\n\n\t// map元素赋值\n\tstudent[\u0026#34;stu1\u0026#34;] = stu{\u0026#34;zhao\u0026#34;, 25}\n\tstudent[\u0026#34;stu2\u0026#34;] = stu{\u0026#34;zhang\u0026#34;, 28}\n\tstudent[\u0026#34;stu3\u0026#34;] = stu{\u0026#34;sun\u0026#34;, 32}\n\tstudent[\u0026#34;stu4\u0026#34;] = stu{\u0026#34;li\u0026#34;, 40}\n\tstudent[\u0026#34;stu5\u0026#34;] = stu{}\n\n\t//上面方式的简写方法\n\t/* …\u003c/code\u003e\u003c/pre\u003e"
August 9, 2013
go语言单元测试
"\u003cp\u003eGo本身提供了一套轻量级的测试框架.符合规则的测试代码会在运行测试时被自动识别并执行.单元测试源文件的命名规则如平衡点:在需要测试的包下面创建以”_test”结尾的go文件,开如[^.]*_test.go\u003c/p\u003e\n\u003cp\u003eGo单元测试函数分为两在类.\u003cstrong\u003e功能测试函数\u003c/strong\u003e和\u003cstrong\u003e性能测试函数\u003c/strong\u003e,分别以Test和Benchmark为函数名前缀并以*testing.T 和 *testing.B 为单一参数的函数。\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003efunc TestAdd1(t *testing.T)\nfunc BenchmarkAdd1(t *testing.T)\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003e测试工具会根据函数中的实际执行动作得到不同的测试结果。\u003c/p\u003e\n\u003cp\u003e功能测试函数会根据测试代码执行过程中是否发生错误来反馈结果;\n性能测试函数仅仅打印出来测试所花费时间,用来判断程序性能;\u003c/p\u003e\n\u003ch1 id=\"准备\"\u003e准备\u003c/h1\u003e\n\u003cp\u003e新建一个文件,命名为 go_test.go\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003epackage go_test\n\nimport \u0026#34;testing\u0026#34;\n\nfunc Add(a, b int) int {\n return a + b\n}\n\u003c/code\u003e\u003c/pre\u003e\u003ch1 id=\"功能测试\"\u003e功能测试\u003c/h1\u003e\n\u003cp\u003e在go_test.go文件里添加以下代码\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003efunc TestAdd1(t …\u003c/code\u003e\u003c/pre\u003e"
August 8, 2013
Redis配置文件参数说明
"\u003cp\u003e\u003cstrong\u003e配置文件参数说明\u003c/strong\u003e:\u003c/p\u003e\n\u003col\u003e\n\u003cli\u003eRedis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程\u003c/li\u003e\n\u003c/ol\u003e\n\u003cp\u003e\u003cstrong\u003edaemonize no\u003c/strong\u003e\u003c/p\u003e\n\u003col start=\"2\"\u003e\n\u003cli\u003e当Redis以守护进程方式运行时,Redis默认会把pid写入/var/run/redis.pid文件,可以通过pidfile指定\u003c/li\u003e\n\u003c/ol\u003e\n\u003cp\u003e\u003cstrong\u003epidfile /var/run/redis.pid\u003c/strong\u003e\u003c/p\u003e\n\u003col start=\"3\"\u003e\n\u003cli\u003e指定Redis监听端口,默认端口为6379,作者在自己的一篇博文中解释了为什么选用6379作为默认端口,因为6379在手机按键上MERZ对应的号码,而MERZ取自意大利歌女Alessia Merz的名字\u003c/li\u003e\n\u003c/ol\u003e\n\u003cp\u003e** port 6379**\u003c/p\u003e\n\u003col start=\"4\"\u003e\n\u003cli\u003e绑定的主机地址\u003c/li\u003e\n\u003c/ol\u003e\n\u003cp\u003e\u003cstrong\u003ebind 127.0.0.1\u003c/strong\u003e\u003c/p\u003e\n\u003cp\u003e5.当 客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003etimeout 300\u003c/strong\u003e\u003c/p\u003e\n\u003col start=\"6\"\u003e\n\u003cli\u003e指定日志记录级别,Redis总共支持四个级别:debug、verbose、notice、warning,默认为verbose\u003c/li\u003e\n\u003c/ol\u003e\n\u003cp\u003e\u003cstrong\u003eloglevel verbose\u003c/strong\u003e\u003c/p\u003e\n\u003col start=\"7\"\u003e\n\u003cli\u003e日志记录方式,默认为标准输出,如果配置Redis为守护进程方式运行,而这里又配置为日志记录方式为标准输出,则日志将会发送 …\u003c/li\u003e\u003c/ol\u003e"
August 7, 2013
安装hg命令
"\u003cp\u003e在使用golang开发的时候,有些时间github.com上面的包需要执行hg命令(hg命令简介).这个时候就需要安装一下才可以.\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003eLinux下安装Mercurial(hg):\u003c/strong\u003e\u003c/p\u003e\n\u003ch1 id=\"安装mercurial\"\u003e安装Mercurial\u003c/h1\u003e\n\u003cp\u003e在进行后面的操作之前需要安装Mercurial( \u003ca href=\"http://baike.baidu.com/view/1094619.htm\"\u003e百科\u003c/a\u003e)版本管理系统(可以输出hg名词检测是否安装)。输入以下命令安装:\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003esudo easy_install mercurial\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003e对于 Ubuntu/Debian 系统,easy_install 命令可以用\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003eapt-get install python-setuptools python-dev build-essential\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003e安装。 如果上述命令安装失败的话,还可以尝试去官方网站 \u003ca href=\"http://mercurial.selenic.com/wiki/Download\"\u003e下载\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003eWindows下安装\u003c/strong\u003e\u003c/p\u003e\n\u003cp\u003e国内国度网盘下载(32位): \u003ca href=\"http://pan.baidu.com/share/link?shareid=3528053518\u0026amp;uk=2365864479\"\u003ehttp://pan.baidu.com/share/link?shareid=3528053518\u0026amp;uk=2365864479\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003e(64位): \u003ca href=\"http://pan.baidu.com/share/link?shareid=3560201274\u0026amp;uk=2365864479\"\u003ehttp://pan.baidu.com/share/link?shareid=3560201274\u0026amp;uk=2365864479\u003c/a\u003e\u003c/p\u003e"
July 23, 2013
exec: “pkg-config”: executable file not found in %PATH% 的解决办法
"\u003cp\u003e在windows下要用 \u003ca href=\"http://blog.haohtml.com/tag/golang\"\u003egolang\u003c/a\u003e 实现操作 \u003ca href=\"http://blog.haohtml.com/tag/zeromq\"\u003ezeromq\u003c/a\u003e 消息队列,发现在sublime下进行\u003c/p\u003e\n\u003cp\u003ego get -tags zmq_3_x github.com/alecthomas/gozmq\u003c/p\u003e\n\u003cp\u003e操作的时候,提示\u003c/p\u003e\n\u003cblockquote\u003e\n\u003cp\u003e# pkg-config –cflags libzmq libzmq libzmq libzmq\nexec: “pkg-config”: executable file not found in %PATH%\nexit status 2\u003c/p\u003e\u003c/blockquote\u003e\n\u003cp\u003e原因是因为没有安装pkg-config.需要手动安装,并设置一下环境变量.pkg-config下载地址: \u003ca href=\"http://ftp.acc.umu.se/pub/gnome/binaries/win32/dependencies/pkg-config_0.23-3_win32.zip\"\u003ehttp://ftp.acc.umu.se/pub/gnome/binaries/win32/dependencies/pkg-config_0.23-3_win32.zip\u003c/a\u003e ( \u003ca href=\"http://ftp.acc.umu.se/pub/gnome/binaries/win64/dependencies/pkg-config_0.23-2_win64.zip\"\u003ehttp://ftp.acc.umu.se/pub/gnome/binaries/win64/dependencies/pkg-config_0.23-2_win64.zip\u003c/a\u003e)\u003c/p\u003e\n\u003cp\u003e如果无法下载,直接打开所在的目录,找到合适的软件包下载.然后将包里bin目录里 …\u003c/p\u003e"
July 23, 2013
用golang发送邮件
"\u003cp\u003e配置文件 \u003ccode\u003econf.json\u003c/code\u003e\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003e{\n\u0026#34;Username\u0026#34;: \u0026#34;[email protected]\u0026#34;,\n\u0026#34;Password\u0026#34;: \u0026#34;123456\u0026#34;,\n\u0026#34;Smtphost\u0026#34;:\u0026#34;smtp.163.com:25\u0026#34;\n}\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003e主程序 sendmail.go\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003epackage main\n\nimport (\n\t\u0026#34;encoding/json\u0026#34;\n\t\u0026#34;fmt\u0026#34;\n\t\u0026#34;io\u0026#34;\n\t\u0026#34;log\u0026#34;\n\t\u0026#34;net/smtp\u0026#34;\n\t\u0026#34;os\u0026#34;\n\t\u0026#34;strings\u0026#34;\n)\n\ntype cfgmail struct {\n\tUsername string\n\tPassword string\n\tSmtphost string\n}\n\ntype cfg struct {\n\tName, Text string\n}\n\nfunc main() {\n\n\t// 从json文件中读取发送邮件服务器配置信息\n\tcfgjson := getConf()\n\n\tvar …\u003c/code\u003e\u003c/pre\u003e"
July 14, 2013
nginx日志切割
"\u003cp\u003enginx的日志文件没有rotate功能。如果你不处理,日志文件将变得越来越大,还好我们可以写一个nginx日志切割脚本来自动切割日志文件。\u003c/p\u003e\n\u003cp\u003e第一步就是重命名日志文件,不用担心重命名后nginx找不到日志文件而丢失日志。在你未重新打开原名字的日志文件前,nginx还是会向你重命名的文件写日志,linux是靠文件描述符而不是文件名定位文件。\u003c/p\u003e\n\u003cp\u003e第二步向nginx主进程发送USR1信号。\u003c/p\u003e\n\u003cp\u003enginx主进程接到信号后会从配置文件中读取日志文件名称,重新打开日志文件(以配置文件中的日志名称命名),并以工作进程的用户作为日志文件的所有者。\u003c/p\u003e\n\u003cp\u003e重新打开日志文件后,nginx主进程会关闭重名的日志文件并通知工作进程使用新打开的日志文件。\u003c/p\u003e\n\u003cp\u003e工作进程立刻打开新的日志文件并关闭重名名的日志文件。\u003c/p\u003e\n\u003cp\u003e然后你就可以处理旧的日志文件了。\u003c/p\u003e\n\u003cp\u003enginx日志按日期自动切割脚本如下\n[shell]\n#nginx日志切割脚本\n#author: \u003ca href=\"http://www.nginx.cn\"\u003ehttp://www.nginx.cn\u003c/a\u003e\n#!/bin/bash\n#设置日志文件存放目录\nlogs_path=\u0026quot;/usr/local/nginx/logs/\u0026quot;\n#设 …\u003c/p\u003e"
July 10, 2013
mysql中innodb表的count优化
"\u003cp\u003e作/译者:叶金荣(imysql#imysql.com\u0026gt;),来源: \u003ca href=\"http://imysql.com/\"\u003ehttp://imysql.com\u003c/a\u003e,欢迎转载。\u003c/p\u003e\n\u003cp\u003e起因:在innodb表上做count(*)统计实在是太慢了,因此想办法看能不能再快点。\u003c/p\u003e\n\u003cp\u003e现象:先来看几个测试案例,如下\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003e一、 sbtest 表上的测试\u003c/strong\u003e\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003eshow create table sbtest\\G\n*************************** 1. row ***************************\nTable: sbtest\nCreate Table: CREATE TABLE `sbtest` (\n`aid` bigint(20) unsigned NOT NULL auto_increment,\n`id` int(10) unsigned NOT NULL default \u0026#39;0\u0026#39;,\n`k` int(10) unsigned NOT NULL default \u0026#39;0\u0026#39;,\n`c` char(120) NOT NULL default \u0026#39;\u0026#39;,\n`pad` char(60) NOT NULL …\u003c/code\u003e\u003c/pre\u003e"
July 8, 2013
Linux下安装php开发框架yaf
"\u003cp\u003e\u003ca href=\"https://github.com/laruence/php-yaf\"\u003ehttps://github.com/laruence/php-yaf\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003eyaf框架中文手册:\u003c/p\u003e\n\u003cp\u003eyaf手册:\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003e1.下载并安装yaf扩展\u003c/strong\u003e \u003ca href=\"http://pecl.php.net/package/yaf\"\u003ehttp://pecl.php.net/package/yaf\u003c/a\u003e\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003e#wget http://pecl.php.net/get/yaf-2.2.9.tgz\n#tar zxvf yaf-2.2.9.tgz\n#cd yaf-2.2.9\n\n[root@bogon yaf-2.2.9]# whereis phpize\nphpize: /usr/bin/phpize /usr/share/man/man1/phpize.1.gz\n/usr/bin/phpize\n\n[root@bogon yaf-2.2.9]# /usr/bin/phpize\nConfiguring for:\nPHP Api Version: 20090626\nZend Module Api No: 20090626\nZend Extension Api No: 220090626\n#whereis php-config\nphp-config: /usr/bin/php-config …\u003c/code\u003e\u003c/pre\u003e"
June 25, 2013
centos 6关闭ipv6
"\u003cp\u003e通过网卡属性查看\u003c/p\u003e\n\u003cp\u003e命令:ifconfig\u003c/p\u003e\n\u003cp\u003e注释:有 “inet6 addr:。。。。。。。“ 的表示开启了ipv6功能.使用下面的方法可以关闭ipv6\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003e1.修改/etc/modprobe.d/dist.conf\u003c/strong\u003e\u003c/p\u003e\n\u003cp\u003e[shell]#vi /etc/modprobe.d/dist.conf[/shell]\u003c/p\u003e\n\u003cp\u003e添加下面两行内容\u003c/p\u003e\n\u003cblockquote\u003e\n\u003cp\u003ealias net-pf-10 off\nalias ipv6 off\u003c/p\u003e\u003c/blockquote\u003e\n\u003cp\u003e保存退出,并且重新启动系统\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003e2.验证ipv6模块是否加载\u003c/strong\u003e\u003c/p\u003e\n\u003cp\u003e[shell]#lsmod|grep ipv6[/shell]\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003e4.开机不启动\u003c/strong\u003e\u003c/p\u003e\n\u003cp\u003e[shell]#chkconfig ip6tables off[/shell]\u003c/p\u003e\n\u003cp\u003e重启 用命令“lsmod | grep v6”查看,是否启动加载!\u003c/p\u003e\n\u003cp\u003e=======================================\u003c/p\u003e\n\u003cp\u003e1.修改/etc/sysconfig/network,追加:\u003c/p\u003e\n\u003cp\u003eNETWORKING_IPV6=no\u003c/p\u003e\n\u003cp\u003e2.修改/etc/hosts文件,把ipv6的那句本地主机名解析的也注释掉:\u003c/p\u003e\n\u003cp\u003e#::1 localhost localhost6 …\u003c/p\u003e"
June 18, 2013
测试golang中的多核多线程
"\u003cp\u003e“并发 (concurrency)” 和 “并行 ( parallelism)” 是不同的。在单个 CPU 核上,线程通过时间片或者让出控制权来实现任务切换,达到 “同时” 运行多个任务的⺫的,这就是所谓的并发。但实际上任何时刻都只有一个任务被执行,其他任务通过某种算法来排队。\u003c/p\u003e\n\u003cp\u003e多核 CPU 可以让同个进程内的 “多个线程” 做到真正意义上的同时运,它们之间不需要排队 (依然会发生排队,因为线程数量可能超出 CPU 核数量,还有其他的进程等等。这里说的是一个理想状况),这才是并行。除了多核,并行计算还可能是多台机器上部署运行。\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003epackage main\n\nimport (\n\u0026#34;fmt\u0026#34;\n\u0026#34;runtime\u0026#34;\n)\n\nfunc test(c chan bool, n int) {\n\nx := 0\nfor i := 0; i \u0026lt; 1000000000; i++ {\nx += i\n}\n\nprintln(n, x)\n\nif n == 9 {\nc \u0026lt;- true\n}\n}\n\nfunc main() {\nruntime.GOMAXPROCS(1) //设置cpu …\u003c/code\u003e\u003c/pre\u003e"
June 17, 2013
golang中的Array 、Slices 和 Maps
"\u003cp\u003e**注意\u003ccode\u003eslice\u003c/code\u003e和数组在声明时的区别:**声明数组时,方括号内写明了数组的长度或使用\u003ccode\u003e...\u003c/code\u003e自动计算长度,而声明\u003ccode\u003eslice\u003c/code\u003e时,方括号内没有任何字符。\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003earr1 := [10]int{1,2,3,4} //数组,长度为10,只有4个元素指定,其它的元素值默认为0\narr2 := [...]string{\u0026#34;a\u0026#34;,\u0026#34;b\u0026#34;,\u0026#34;c\u0026#34;} //数组,长度自适应,这里长度为3\ns1 := []int{1,2,3,4} //slice,目前长度为4,可能通过append来动态添加元素个数\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003e示例:\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003epackage main\n\nimport (\n \u0026#34;fmt\u0026#34;\n)\n\nfunc main() {\n\n//array example\n arr := [10]int{1, 2, 3} //array 指定前三个值,其它值使用默认类型值0\n fmt.Println(len(arr))\n fmt.Println(arr)\n //a1 := append(arr, 4, 5) //数组不支持append,只有slice才支持append …\u003c/code\u003e\u003c/pre\u003e"