php下使用redis

php下使用redis

client的选择

先去官网看了下php的client支持,选择了:https://github.com/hiproz/php-redis-client
根据文档描述,只支持到redis4.0,所以我们需要安装redis4.0

安装redis4.0

下载 redis4.0

编译:

$ wget http://download.redis.io/releases/redis-5.0.0.tar.gz
$ tar xzf redis-5.0.0.tar.gz
$ cd redis-5.0.0
$ make

编译完成后,执行文件就在 src目录下,启动redis server

src/redis-server

安装好后,就可以用本地内置的client来测试:

$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

开机服务自启动

make install 后,reids在源码的util目录下提供了可以直接创建系统service的脚本install_server.sh。直接执行,就可以安装成系统服务:

[root@sz-svr1 utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli

设置成开机自启动:

[root@sz-svr1 /]# systemctl enable redis_6379
redis_6379.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig redis_6379 on

增加密码后的停止

在设置密码后,在我们用systemctl stop redis_xxxx时,redis不会停止,一直输出:

Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...

所以我们需要在结束指令中,增加密码参数。具体修改自行参看/etc/rc.d/init.d/reids_xxxx

测试

至此,我们的环境完成了,我们执行测试代码 examples/raw_commands.php,在浏览器输入路径后,页面显示:

result: bar result: bar result: or value with spaces

观察php日志,确认代码,我们知道页面的结果是正确的。

支持php环境下的redis支持就完成了,剩下的就是码业务逻辑了。

hanve fun!

指定版本的php安装

默认操作系统集成了对应版本的php版本,但是有时候我们因为要运行特定的程序,需要特定版本的php:

yum install phpXXw

XX的版本,可以参考php官网中的版本分支记录,寻找到合适的版本。

注意在安装新版本前,要卸载系统默认已经安装的所有php相关组件。

如何在php项目中安装vendor依赖

在测试开源代码时,提示vendor/autoload.php找不到。
vendor是composer安装后的系统依赖,默认项目里面是没有的。

安装composer

wget -nc http://getcomposer.org/composer.phar
php composer.phar
mv composer.phar /usr/local/bin/composer 
composer -V

有composer环境安装vendor

一般使用composer的工程,目录下都有现成的配置文件composer.json,主要描述了工程需要的各种依赖和需求:

{
  "name": "cheprasov/php-redis-client",
  "version": "1.8.0",
  "description": "Php client for Redis. It is a fast, fully-functional and user-friendly client for Redis, optimized for performance. RedisClient supports the latest versions of Redis starting from 2.6 to 4.0",
  "homepage": "http://github.com/cheprasov/php-redis-client",
  "minimum-stability": "stable",
  "license": "MIT",
  "authors": [
    {
      "name": "Alexander Cheprasov",
      "email": "acheprasov84@gmail.com"
    }
  ],
  "autoload": {
    "psr-0": {
      "RedisClient\\": "src"
    }
  },
  "require": {
    "php": ">=5.5"
  },
  "require-dev": {
    "phpunit/phpunit": "4.8.*",
    "cheprasov/php-extra-mocks": "^1.0.0"
  }
}

我们在项目的的composer.json同级目录下执行:

composer install

正常成功后:

Writing lock file
Generating autoload files

至此,verdor目录生成

无composer环境直接安装vendor

如果没有安装过composer,也可以直接通过php安装,在项目根目录下执行(有composer.json的目录):

php composer.phar require xxx

xxx是composer.json 中的name字段。

make install的卸载

系统中yum 和make install的路径是不同的。但是由于PATH路径的优先级问题,很多时候不同场景是不同的版本在生效,导致混乱。

如果源代码没有支持uninstall,怎么干净的卸载完所有的安装包呢,没有捷径,只有本笨办法:
1. 重新编译部署一次,使用--prefix=/xxx/xxx/ 指定make install的目录: ./configure --prefix=/tmp/packname/ && make && sudo make install
2. 根据安装完后的目录层次,依次手动在现有系统中删除对应的文件。
3. 重启相关的服务。

centos7下的mysql安装和开机自启动

资料整理,避免下次再浪费时间,待细化:


在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB。

1 下载并安装MySQL官方的 Yum Repository
[root@localhost ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
使用上面的命令就直接下载了安装用的Yum Repository,大概25KB的样子,然后就可以直接yum安装了。

[root@localhost ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm
之后就开始安装MySQL服务器。

[root@localhost ~]# yum -y install mysql-community-server
这步可能会花些时间,安装完成后就会覆盖掉之前的mariadb。

至此MySQL就安装完成了,然后是对MySQL的一些设置。

2 MySQL数据库设置
首先启动MySQL

[root@localhost ~]# systemctl start mysqld.service
查看MySQL运行状态,运行状态如图:

[root@localhost ~]# systemctl status mysqld.service

此时MySQL已经开始正常运行,不过要想进入MySQL还得先找出此时root用户的密码,通过如下命令可以在日志文件中找出密码:

[root@localhost ~]# grep "password" /var/log/mysqld.log

如下命令进入数据库:

[root@localhost ~]# mysql -uroot -p
输入初始密码,此时不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
这里有个问题,新密码设置的时候如果设置的过于简单会报错:

原因是因为MySQL有密码设置的规范,具体是与validate_password_policy的值有关:

MySQL完整的初始密码规则可以通过如下命令查看:

复制代码
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 4 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | LOW |
| validate_password_special_char_count | 1 |
+--------------------------------------+-------+
7 rows in set (0.01 sec)
复制代码
密码的长度是由validate_password_length决定的,而validate_password_length的计算公式是:

validate_password_length = validate_password_number_count + validate_password_special_char_count + (2 * validate_password_mixed_case_count)

我的是已经修改过的,初始情况下第一个的值是ON,validate_password_length是8。可以通过如下命令修改:

mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=1;
设置之后就是我上面查出来的那几个值了,此时密码就可以设置的很简单,例如1234之类的。到此数据库的密码设置就完成了。

但此时还有一个问题,就是因为安装了Yum Repository,以后每次yum操作都会自动更新,需要把这个卸载掉:

[root@localhost ~]# yum -y remove mysql57-community-release-el7-10.noarch
此时才算真的完成了。


关闭防火墙:
[root@localhost system]# systemctl stop firewalld.service
[root@localhost system]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.Fedoraproject.FirewallD1.service.

设置mysql的开机启动:
[root@localhost system]# systemctl enable mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[root@localhost system]# systemctl list-unit-files | grep mysqld
mysqld.service enabled

取消mysql的开机自启动:
[root@localhost system]# systemctl disable mysqld
Removed symlink /etc/systemd/system/multi-user.target.wants/mysqld.service.
[root@localhost system]# systemctl list-unit-files | grep mysqld
mysqld.service disabled

STM8 bootloader纪要

STM8 bootloader-UM0560

mcu:STM8L051F3P6,属于 STM8L Series low density

bootloder 支持

已经内置bootloader的mcu
bootloader group

没有内置bootloader的mcu

bootloder 执行流程图

bootloader执行流程图:

bootloder 传输设置

ota 同步消息SYNCHR=0x7F

支持传输的外设

从图上看,051F支持UART或者SPI
UART设置:1 start bit, 8 data bit, 1 bit 奇校验 ,1 stop bit
波特率:通过0x7F的传输,自动适配波特率,最大115200,最小4800.

传输回复
串口:1 start bit, 8 data bit, no parity bit, 1 stop bit,波特率自适应

SPI设置
• 8 data bit, MSB first
• Bit rate: Set by the host which acts as a master
• Peripheral set in slave mode with software management of NSS
• Data polarity: CPOL = 0 (SCK to 0 when idle), CPHA = 0 (the first clock transition is the
first data capture edge).

收到命令后,bl回复ACK 0x79

bootloder 外设选择

从资源的角度,我们倾向于使用spi做外设,因为uart可以预留来做用户调试用,但是目前官方提供了uart的串口下载PC客户端,方便测试。所以目前看使用uart作为bl外设可能更方便。这里也就要求选用单片机时,最好是多串口的设备。

bootloder 跳转地址表

关键点检测后,要跳转的地址表:

目前我们可以先考虑实现串口的版本,但是最后还是SPI的更实用。

完整文档:UM0560

通过acme的DNS方式实现letsencrypt通配证书泛证书的自动更新

详细完整参见:
https://github.com/Neilpang/acme.sh/wiki/说明

文章已经讲的很详细了,现就就DNS和通配相关的使用做简要摘录和关键步骤说明

想要实现通配证书,目前dns方式是唯一的方式。

  1. 安装

    curl  https://get.acme.sh | sh
  2. 重定向
    打开 .bashrc,添加 alias acme.sh=~/.acme.sh/acme.sh ,方便命令输入

  3. 生成证书。
    默认dns是需要手动添加域名记录验证归属合法性的,通过dnspod这样的第三方平台 API,我们就可以实现自动验证的功能。

    export DP_Id="1234"
    export DP_Key="sADDsdasdgdsf"
    acme.sh   --issue   --dns dns_dp   -d  *.aa.com

    dnspod的token申请路径:https://www.dnspod.cn/console/user/security

这期间可能提示你需要:

acme.sh --register-account -m aaa@bbb.com

注意,DNS的账号信息,只需要操作一次,系统会自动记录到配置文件,后面直接使用"--dns dns_dp" 就可以。

  1. 证书的安装
    证书默认是在~/.acme.sh/通配域名/目录下的,通过安装可以安装到我们指定的生产环境路径。

注意,下面的命令不会自动创建目录,如果目录不存在,需要手动提前创建。

rsa 密钥:

acme.sh --install-cert -d *.aa.com --key-file /path/domain/privkey.pem --fullchain-file /path/domian/fullchain.pem --reloadcmd "systemctl force-reload nginx.service"

说明:以上两个路径是安装的目的地址,也就是将第3步的证书内容,按照这个命令中的地址,进行安装。

ecc 密钥:

acme.sh --install-cert --ecc -d *.aa.com --key-file /path/domain/privkey.pem --fullchain-file /path/domian/fullchain.pem --reloadcmd "systemctl force-reload nginx.service"

以上相关的操作都会被acme脚本自动记录,并且在证书自动更新的时候会自动执行。

  1. 证书的更新
    完成以上后,acme就会自动帮你更新证书了。

  2. acme.sh本身的自动更新
    开启:acme.sh --upgrade --auto-upgrade
    关闭:acme.sh --upgrade --auto-upgrade 0

  3. acme的卸载
    acme.sh --uninstall

alios vsc 插件错误

点击项目和平台时,VSC提示错误:
ENOENT: no such file or directory, scandir 'C:\Users\xxx.aos\AliOS-Things\board'

很显然是alios-studio插件的系统路径和实际安装的不符。
在VSC中,点击设置,在右侧有"用户设置"和"工作区设置",点击工作区设置,指定对应的路径如下:

{
  "aliosStudio.sdkPath": "xxxxx\\AliOS-Things"
}