月度归档:2017年03月

postgres — PostgreSQL database server 安装和简单配置教程(centos7)

安装:

yum install postgresql-server

配置开机启动:

systemctl enable postgresql

初始化数据库:

postgresql-setup  initdb

创建和删除数据库:

[root@web4 ~]# su - postgres
Last login: Mon Mar 20 01:14:46 CST 2017 on pts/0
-bash-4.2$ 
-bash-4.2$ createdb xxxxx
-bash-4.2$ createdb mydb
-bash-4.2$ dropdb mydb

访问数据库:

psql xxxxx

web.py注册为服务(centos7)

命令行脚本:

/usr/bin/python /mnt/www/xxx/scripts/mq.py

创建服务文件(服务名xxx)

vim /usr/lib/systemd/system/xxx.service

xxx.service 文件内容

[Unit]
Description=The doufu python message queue server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=simple
#PIDFile=
#ExecStartPre=
#ExecStartPre=
ExecStart=/usr/bin/python /mnt/www/xxx/scripts/mq.py
ExecReload=/usr/bin/python /mnt/www/xxx/scripts/mq.py
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
# 启用服务(开机自动启动)
systemctl enable xxx.service
# 查看服务状态
systemctl status xxx.service
# 开始服务
systemctl start xxx.service
# 查看服务状态
systemctl status xxx.service

pkg_resources.DistributionNotFound: Flask==0.10.1

现象:
装完新版mrq运行示例又报错:

[root@web4 test-mrq]# mrq-run tasks.Fetch url http://www.google.com
Traceback (most recent call last):
  File "/bin/mrq-run", line 5, in 
    from pkg_resources import load_entry_point
  File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 3011, in 
    parse_requirements(__requires__), Environment()
  File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 626, in resolve
    raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: Flask==0.10.1

解决方法:
yum install python-flask

继续报错:

[root@web4 test-mrq]# mrq-run tasks.Fetch url http://www.google.com
Traceback (most recent call last):
  File "/bin/mrq-run", line 5, in 
    from pkg_resources import load_entry_point
  File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 3011, in 
    parse_requirements(__requires__), Environment()
  File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 626, in resolve
    raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: subprocess32==3.2.5

ImportError: cannot import name _NONE

故障现象:
运行pip示例报错

[root@i test-mrq]# mrq-run tasks.Fetch url http://www.google.com
2017-03-18 23:11:18.162884 [DEBUG] mongodb_jobs: Connecting to MongoDB at [('127.0.0.1', 27017)]/mrq...
Traceback (most recent call last):
  File "/usr/bin/mrq-run", line 9, in 
    load_entry_point('mrq==0.1.18', 'console_scripts', 'mrq-run')()
  File "/usr/lib/python2.7/site-packages/mrq/bin/mrq_run.py", line 52, in main
    job = worker_class.job_class(None)
  File "/usr/lib/python2.7/site-packages/mrq/job.py", line 52, in __init__
    self.collection = context.connections.mongodb_jobs.mrq_jobs
  File "/usr/lib/python2.7/site-packages/mrq/utils.py", line 160, in __getattr__
    value = factory(attr)
  File "/usr/lib/python2.7/site-packages/mrq/context.py", line 159, in _connections_factory
    db = MongoClient(config_obj, **kwargs)[mongo_name]
  File "/usr/lib64/python2.7/site-packages/pymongo/mongo_client.py", line 461, in __init__
    self._topology.open()
  File "/usr/lib64/python2.7/site-packages/pymongo/topology.py", line 151, in open
    self._ensure_opened()
  File "/usr/lib64/python2.7/site-packages/pymongo/topology.py", line 378, in _ensure_opened
    self._update_servers()
  File "/usr/lib64/python2.7/site-packages/pymongo/topology.py", line 441, in _update_servers
    server.open()
  File "/usr/lib64/python2.7/site-packages/pymongo/server.py", line 46, in open
    self._monitor.open()
  File "/usr/lib64/python2.7/site-packages/pymongo/monitor.py", line 78, in open
    self._executor.open()
  File "/usr/lib64/python2.7/site-packages/pymongo/periodic_executor.py", line 83, in open
    thread.start()
  File "/usr/lib64/python2.7/threading.py", line 746, in start
    _start_new_thread(self.__bootstrap, ())
  File "/usr/lib64/python2.7/site-packages/gevent/thread.py", line 40, in start_new_thread
    greenlet = Greenlet.spawn(function, *args, **kwargs)
  File "/usr/lib64/python2.7/site-packages/gevent/greenlet.py", line 201, in spawn
    g = cls(*args, **kwargs)
  File "/usr/lib64/python2.7/site-packages/gevent/greenlet.py", line 75, in __init__
    hub = get_hub()
  File "/usr/lib64/python2.7/site-packages/gevent/hub.py", line 169, in get_hub
    hub = _threadlocal.hub = hubtype(*args, **kwargs)
  File "/usr/lib64/python2.7/site-packages/gevent/hub.py", line 268, in __init__
    loop_class = _import(self.loop_class)
  File "/usr/lib64/python2.7/site-packages/gevent/hub.py", line 198, in _import
    return _import(path[-1])
  File "/usr/lib64/python2.7/site-packages/gevent/hub.py", line 210, in _import
    x = __import__(module)
  File "/usr/lib/python2.7/site-packages/mrq/monkey.py", line 150, in mrq_safe_import
    result = orig_import(*args, **kwargs)
  File "core.pyx", line 10, in init gevent.core (gevent/gevent.core.c:41156)
  File "/usr/lib/python2.7/site-packages/mrq/monkey.py", line 150, in mrq_safe_import
    result = orig_import(*args, **kwargs)
  File "/usr/lib64/python2.7/site-packages/gevent/signal.py", line 19, in 
    from gevent._util import _NONE as _INITIAL
ImportError: cannot import name _NONE

解决方法:
cd /usr/local/src
wget https://codeload.github.com/pricingassistant/mrq/zip/master
mv master mrq-master.zip
unzip mrq-master.zip
pip install mrq -e /usr/local/src/mrq-master

原因:
gevent.coros过时了,被gevent.lock替代了

mrq: ImportError: No module named coros

故障现象:

运行mrq示例(mrq-run tasks.Fetch url http://www.google.com),报错

[root@web4 test-mrq]# mrq-run tasks.Fetch url http://www.google.com
Traceback (most recent call last):
  File "/bin/mrq-run", line 9, in 
    load_entry_point('mrq==0.1.18', 'console_scripts', 'mrq-run')()
  File "/usr/lib/python2.7/site-packages/mrq/bin/mrq_run.py", line 33, in main
    set_current_config(cfg)
  File "/usr/lib/python2.7/site-packages/mrq/context.py", line 82, in set_current_config
    patch_import()
  File "/usr/lib/python2.7/site-packages/mrq/monkey.py", line 127, in patch_import
    import gevent.coros
  File "/usr/lib64/python2.7/site-packages/gevent/builtins.py", line 93, in __import__
    result = _import(*args, **kwargs)
ImportError: No module named coros

解决方法:

yum install python-gevent

用php监控从库同步延迟个数

<?php
$mysqli = new mysqli('slave_host', 'root', 'passwd', 'dbname');

/*
 * This is the "official" OO way to do it,
 * BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
 */
if ($mysqli->connect_error) {
 die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}

/*
 * Use this instead of $connect_error if you need to ensure
 * compatibility with PHP versions prior to 5.2.9 and 5.3.0.
 */
if (mysqli_connect_error()) {
 die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}

echo 'Success... ' . $mysqli->host_info . "\n";

/* Create table doesn't return a resultset */
/**
 * 
 * @var mysqli_result $result
 */
$result = $mysqli->query("show slave status");

if ($mysqli->errno) {
 printf("Error number: %s\n", $mysqli->errno);
 printf("Error message: %s\n", $mysqli->error);
}

if ($result !== false) {
 $row = $result->fetch_assoc();
 echo "master : {$row['Master_Log_File']}\n";
 echo "slave relay : {$row['Relay_Master_Log_File']}\n";
 echo " : " . ($row['Read_Master_Log_Pos'] - $row['Exec_Master_Log_Pos']);
}

$mysqli->close();

solr 突然卡死, 重启失败,kill没反应,kill -9才杀死,重启后一会儿才好了

# service solr restart
Java not found, or an error was encountered when running java.
A working Java 8 JRE is required to run Solr!
Please install latest version of Java 8 or set JAVA_HOME properly.
Command that we tried: ‘java -version’, with response:
Error occurred during initialization of VM
java.lang.OutOfMemoryError: unable to create new native threadDebug information:
JAVA_HOME: N/A
Active Path:

出现这种问题的原因时solr内存配置不足,参考:

solr内存配置,solr时区配置

阿里云LVM 真·原地扩容 加硬盘 换硬盘 释放硬盘

阿里云原地扩容方案:添加一个新的云盘替换旧的硬盘

第一步:扩容pv(物理卷)

创建物理卷(直接使用整个硬盘)

pvcreate /dev/vdc

将新物理卷(新硬盘)添加到卷组vg1

vgextend vg1 /dev/vdc

设置旧物理卷(旧硬盘)为不可分配

pvchange -x n /dev/vdb

将旧硬盘上的数据移走(热处理 无损移动到其他硬盘 确保其他硬盘空间充足)

pvmove /dev/vdb

可以将旧物理卷(旧硬盘)从vg1移除

vgreduce vg1 /dev/vdb

将旧物理卷(旧硬盘)从LVM移除

pvremove /dev/vdb

完成,可以拔除/dev/vdb了,此时可以使用pvs命令看到PFree列即为空闲空间

第二步:扩容lv(逻辑卷)

给lv +1GB容量

lvextend -L +1G /dev/vg1/lv1

使新加的容量生效(ext4)

resize2fs /dev/mapper/vg1-lv1

或者 使新加的容量生效(xfs)

xfs_growfs /dev/mapper/vg1-lv1