Vultr 主机端口设置

如果 Vultr 自带的 Firewall 设置不起作用,可采用如下命令行方式:

apt-get install firewalld

#Web server
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent

#允许ping
firewall-cmd --permanent --add-icmp-block-inversion
firewall-cmd --permanent --add-icmp-block=echo-reply
firewall-cmd --permanent --add-icmp-block=echo-request
firewall-cmd --reload

最后注意删除 Vultr 的 Firewall group。

阅读:17

Android Studio编译错误:clang++: error: no such file or directory

Android Studio版本升级后编译原有工程,出现错误:clang++: error: no such file or directory: 'CMakeFiles/native.dir/src/main/cpp/native.cpp.o',尝试过网上建议的修改工程设置等各种方法均无效,最终解决办法:

$ rm -Rf .externalNativeBuild/

阅读:81

强制Android Studio使用本地文档

使用Android Studio时发现一个怪现象,即使下载了SDK对应的文档,每次按F1时也会出现Fetching Documentation...,等待很久才能看到文档。研究后在Stack Overflow找到了答案,经测试可行:

Delete all javadoc paths from jdk.table.xml. Path to this file on OS X: /Users/.../Library/Preferences/AndroidStudio.../options/jdk.table.xml
--> Delete all of these and all occurrences -->

<javadocPath>
<root type="composite">
<root type="simple" url="http://developer.android.com/reference/" />
</root>
</javadocPath>

继续阅读强制Android Studio使用本地文档

阅读:108

mysql数据库故障修复一例

某线上服务器的mysql突然崩溃后,重启一直失败。查看mysql的error.log发现如下错误信息:

InnoDB: Serious error! InnoDB is trying to free page 716
InnoDB: though it is already marked as free in the tablespace!
InnoDB: The tablespace free space info is corrupt.
InnoDB: You may need to dump your InnoDB tables & recreate the whole
InnoDB: database!

尝试了各种修复方法均告失败,看来只能重建数据库了。

首先解决无法启动的问题(参见http://dev.mysql.com/doc/refman/5.5/en/forcing-innodb-recovery.html的说明):

先添加如下设置到my.cnf:

# vi /etc/mysql/my.cnf

[mysqld]
innodb_force_recovery = 1

保存my.cnf退出vi后重启mysql:

# service mysql start

但重启依然失败。

按上述官方文档提示,尝试提高recovery级别到2:

[mysqld]
innodb_force_recovery = 2

这次重启成功了,并且可以正常查询,但此时mysql处于只读状态,无法做update、delete等写操作。下面开始尝试先备份再恢复全部数据库。

1、备份全部数据库

mysqldump -uroot -p -AER > /root/recovery_dump.sql

2、删除全部数据库

mysql> SET FOREIGN_KEY_CHECKS=0;
mysql> DROP DATABASE db1;
mysql> DROP DATABASE db2;
...

如果数据库太多,可使用如下命令批量删除。注意:<password>应该替换为真实密码。

# mysql -uroot -p<password>  -e "show databases" | grep -v Database | grep -v mysql| grep -v information_schema| grep -v test | grep -v OLD |gawk '{print "drop database " $1 ";select sleep(0.1);"}' | mysql -uroot -p<password>

3、停止mysql。注意应该禁用innodb_fast_shutdown,确保mysql完全停止。

# mysql -uroot -p -e "SET GLOBAL innodb_fast_shutdown = 0"
# service mysql stop

继续阅读mysql数据库故障修复一例

阅读:81

C#中的抽象基类(Abstract Base Class)和接口(Interface)

抽象基类和接口有很多类似的地方,那么我们在软件架构的过程中如何决定该用抽象基类还是接口呢?Stack Overfollow上有一个回答总结得比较好,虽然只有短短几十个字:

Abstract classes and interfaces are semantically different, although their usage can overlap.

An abstract class is generally used as a building basis for similar classes. Implementation that is common for the classes can be in the abstract class.

An interface is generally used to specify an ability for classes, where the classes doesn't have to be very similar.

可以这么理解:基类是爹,接口是能力。爹只能有一个,而且他决定了你是什么,但能力可以有多个。

应该考虑使用抽象基类的场景:

  • 逻辑上是同一类的东西(is a...);
  • 有需要实现的公共方法或属性;
  • 需要序列化的成员类型(因为接口无法序列化);

阅读:142

为Ubuntu Linux创建Swap分区

前不久在Amazon购买了一台EC2服务器运行WordPress,配置为双核CPU,内存1G。运行了一段时间后,发现mysql经常崩溃。检查mysql的error.log发现,崩溃前最后一条日志是:

160328 16:50:26 InnoDB: Completed initialization of buffer pool
160328 16:50:26 InnoDB: Fatal error: cannot allocate memory for the buffer pool
160328 16:50:26 [ERROR] Plugin 'InnoDB' init function returned error.
160328 16:50:26 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
160328 16:50:26 [ERROR] Unknown/unsupported storage engine: InnoDB
160328 16:50:26 [ERROR] Aborting

因为网站访问量比较大,推测可能是系统内存不足导致的。使用下面的命令查看了一下,发现apache已经使用了60%多的内存(总内存为1G)。

$ ps -ylC apache2 | awk '{x += $8;y += 1} END {print "Total (MB): "x/1024; print "Average (MB): "x/((y-1)*1024)}'

Total (MB): 622.996
Average (MB): 14.8332

apache、mysql都是吃内存的大户,1G屋里内存显然不够用了。使用swapon查看交换文件状态:

$ sudo swapon -s

Filename                Type        Size    Used    Priority

OMG,竟然没有。。。那就创建一个。

先创建一个4G的文件:

$ sudo dd if=/dev/zero of=/swapfile bs=1M count=4000
4000+0 records in
4000+0 records out
4194304000 bytes (4.2 GB) copied, 5.12 s, 1203 MB/s

然后把它转变为swap文件: 继续阅读为Ubuntu Linux创建Swap分区

阅读:142

Linux常用命令整理(不定期更新)

#查看系统版本
uname -a
cat /etc/issue

#修改系统时区
#先使用tzselect按提示一步步找到对应的时区,比如Asia/Shanghai,然后:
echo "Asia/Shanghai" | sudo tee /etc/timezone
sudo dpkg-reconfigure --frontend noninteractive tzdata

#查看进程侦听的端口
lsof -iTCP -sTCP:LISTEN -n -P

#查看目录大小(h:自动使用最佳可读单位,c:最后显示总大小)
du -hc /path
du -k /path | sort -nr

#查看磁盘使用情况
df -h

#创建空文件
touch filename

继续阅读Linux常用命令整理(不定期更新)

阅读:209