用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
# redis-工具-安全-python连接 [TOC] ## 一、 PHP加载redis redis插件网址:http://pecl.php.net/package/redis 1. 操作步骤: 1)下载 ```sh cd /usr/local/src wget http://pecl.php.net/get/redis-2.2.7.tgz ``` 2)解压并进入目录 ``` /usr/local/php/bin/pipize .configure --with-php-config=/usr/local/bin/php-config make && make install 最后编辑php.ini,在最后加入如下语句 extension = redis.so ``` 3)重启PHP并检查redis是否加载成功 检查方法1:phpinfo ```sh <?php phpinfo(); ?> ``` 检查方法2:写一个插入并读取的脚本redis.php ```sh <?php $redis = newRedis(); $redis->connect('192.168.1.10',6379); $redis->set('keyname','value'); echo $redis->get('keyname'); ?> ``` ## 二、 redis工具 redis推荐使用的工具有两个,分别是PHPRedisAdmin管理工具和rebtools分析工具,还有一个redis在windows下的管理工具xxx ### 1. PHPRedisAdmin: 网址:https://github.com/erikdubbelboer/phpRedisAdmin 方法: ```sh wget https://github.com/erikdubbelboer/phpRedisAdmin/archive/master.zip unzip master.zip mv phpRedisAdmin-master/ /var/www/html/redisadmin cd /var/www/html/ chown -R apache:apache redisadmin/ /etc/init.d/httpd start ``` 2. rdbtools 网址:https://github.com/sripathikrishnan/redis-rdb-tools 方法: ``` yum install python-pip -y pip install rdbtools cd /var/lib/redis/ cp dump.rdb /tmp/ rdb -c memory /tmp/dump.rdb >redis.csv ``` ## 三、redis安全 redis漏洞入侵模拟:http://unixhot.com/article/23 1)设置密码 找到配置文件中的如下位置,然后将井号去掉并写上自己的密码,重启即可 ```sh grep requirepass /etc/redis.conf # If the master is password protected (using the "requirepass" configuration # requirepass foobared ``` 以后连接上redis数据库以后,任何操作都会提示没有权限,需要进行认证 ``` 11.10.0.0.41:6379> auth oldboy ``` 2)给危险命令进行重命名 ```sh grep rename /etc/redis.conf # security of read only slaves using 'rename-command' to shadow all the # environment. For instance the CONFIG command may be renamed into something # rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52 # rename-command CONFIG "" ``` ## 四、 redis的api开发 Redis提供了各类开发语言的API,方便开发语言连接使用Redis。 https://redis.io/clients官方网站提供了不同开发语言的API程序 官网中,给我们提供了很多种Python连接redis的API,我们通常选择有“笑脸”并且带有“星号”的使用,这里我们推荐使用redis-py. ### 1. pythoy连接redis 1)python安装 ```sh tar xf Python-3.5.2.tar.xz cd Python-3.5.2 ./configure make && make install ``` 2)安装redis-py客户端 下载地址 https://redis.io/clients,下载redis-py-master.zip ```sh unzip redis-py-master.zip cd redis-py-master python3 setup.py install ``` 3)安装redis-cluser的客户端程序 ```sh cd redis-py-cluster-unstable python3 setup.py install ### 2. 对redis的单实例进行连接操作 python3 >>>import redis >>>r = redis.StrictRedis(host='localhost', port=6379, db=0,password='root') >>>r.set('lufei', 'guojialei') True >>>r.get('lufei') 'bar' ``` ### 3. sentinel集群连接并操作 ```sh [root@db01 ~]# redis-server /data/redis_m/6380/redis.conf [root@db01 ~]# redis-server /data/redis_m/6381/redis.conf [root@db01 ~]# redis-server /data/redis_m/6382/redis.conf [root@db01 ~]# redis-sentinel /data/26380/sentinel.conf & 1) 导入redis sentinel包 >>> from redis.sentinel import Sentinel 2) 指定sentinel的地址和端口号 >>> sentinel = Sentinel([('localhost', 26380)], socket_timeout=0.1) 3) 测试,获取以下主库和从库的信息 >>> sentinel.discover_master('mymaster') >>> sentinel.discover_slaves('mymaster') ``` ### 4. 配置读写分离 ```sh 1) 写节点 >>> master = sentinel.master_for('mymaster', socket_timeout=0.1) 2) 读节点 >>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1) 3) 读写分离测试 key >>> master.set('oldboy', '123') >>> slave.get('oldboy') '123' ``` ### 5. redis cluster的连接并操作 python2.7.2以上版本才支持redis cluster,我们选择的是3.5,下载地址:https://github.com/Grokzen/redis-py-cluster 1)python连接rediscluster集群测试 安装 ```sh $ pip install redis-py-cluster or from source $ python setup.py install ``` 使用 ```sh >>> from rediscluster import StrictRedisCluster >>> startup_nodes = [{"host": "127.0.0.1", "port": "7000"}] >>> # Note: decode_responses must be set to True when used with python3 >>> rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True) >>> rc.set("foo", "bar") True >>> print(rc.get("foo")) 'bar' ```