NoSQL的总体介绍
memcached介绍
1.3 memcached安装
yum install -y epel-release
rpm -qa epel-release
yum install -y livevent memcached libmemcached
/etc/init.d/memcached start
netstat -lnp
ps aux |grep memcached
vi /etc/init.d/memcached
cat /etc/sysconfig/memcached
-l监听的服务器ip
memcached-tool 127.0.0.1:11211 stats
memstat --servers=127.0.0.1:11211
1.4 Memcached基本操作
telnet 127.0.0.1 11211
set key3 123 200 3
123
replace key3 123 210 4
1234
get key3
php连接memcached
cd /usr/local/src/
tar zxf memcache-2.2.3.tgz
php 下载 装了不需要装 没装重新从mysql 装起来http://cn2.php.net/get/php-5.4.45.tar.bz2/from/this/mirror
cd memcache-2.2.3
/usr/local/php/bin/phpize
运行/usr/local/webserver/php/bin/phpize时出现:
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
解决方法
# yum install m4
# yum install autoconf
./configure --with-php-config=/usr/local/php/bin/php-config make && make install
ls /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
vim /usr/local/php/etc/php.ini
cp /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so /usr/local/php/ext
ls !$
vim /usr/local/php/etc/php.ini
extension="memcache.so" 添加
/usr/local/php/bin/php -m
/usr/local/php/bin/php test.php
Memcached共享Session
1.7 Redis介绍和安装
cd /usr/local/src
mv 2.8.21 redis-2.8.21.tar.gz
tar zxvf redis-2.8.21.tar.gz
yum install -y epel-rele
yum install -y jemalloc-devel
make
make PREFIX=/usr/local/redis install
mkdir /usr/local/redis/etc
wget http://www.apelearn.com/study_v2/.redis_conf -O /usr/local/redis/etc/redis.conf 2>/dev/null
cat /usr/local/redis/etc/redis.conf
wget http://www.apelearn.com/study_v2/.redis_init -O /usr/local/redis/etc/redis.conf 2 >/dev/null
useradd -s /sbin/nologin redis
mkdir /usr/local/redis/var
chmod 777 /usr/local/redis/var/
vi /etc/init.d/redis
添加启动脚本
chmod 755 /etc/init.d/redis
chkconfig --add redisservice redis startRedis数据结构
/usr/local/redis/bin/redis-cli
alias redis-cli='/usr/local/redis/bin/redis-cli'
vi .bshre
cp /etc/skel/.bashrc .bashre 没有 就拷贝
#.bashre
#User specific allases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias redis-cli='/usr/local/redis/bin/redis-cli'
# Source global definitions
if [-f /etc/bashrc ]; then
. /etc/bashrc
fi
redis-cli
set key1 aminglinux
OK
127.0.0.1:6379> get key1
"aminglinux"
127.0.0.1:6379> mset key1 aminglinux key2 hello key3 love
OK
127.0.0.1:6379> mget key1 key2 key3
1) "aminglinux"
2) "hello"
3) "love"
redis-cli get key1
whereis redis 查看文件路径 安装地址
list是一个链表结构,主要功能是push、pop、获取一个范围的所有值等等。操作中key理解为链表的名字。
lrange list1 0 -1
1) "123 456"
2) "aaa"
127.0.0.1:6379> lpop list1
"123 456"
127.0.0.1:6379> rpush list1 aaa
(integer) 2
127.0.0.1:6379> lrange list1 0 -1
1) "aaa"
2) "aaa"
-set
set是集合,和我们数学中的集合概念相似,对集合的操作有添加删除元素,有对多个集合求交并差等操作
zadd myset2 12 "abc"
(integer) 1
127.0.0.1:6379> zadd myset2 12 "abc 123"
(integer) 1
127.0.0.1:6379> zadd myset2 12 "123-aaa"
(integer) 1
127.0.0.1:6379> zrange myset2 0 -1 正序
zrevrange myset2 0 -1 倒序
hset hash2 name aaa
(integer) 1
127.0.0.1:6379> hset hash2 age 10
(integer) 1
127.0.0.1:6379> set hash2 sex nan
(error) ERR syntax error
127.0.0.1:6379> hset hash2 sex nan
(integer) 1
127.0.0.1:6379> hgettall hash2
(error) ERR unknown command 'hgettall'
127.0.0.1:6379> hgetall hash2
1) "name"
2) "aaa"
3) "age"
4) "10"
5) "sex"
6) "nan"
hget hash2 age
1.9 Redis持久化
2.0 Redis配置讲解(上)
Redis配置讲解(下)
Redis主从配置
2.3 string类型常用命令
hash类型常用命令
source .bashrc
echo $PATH
vi /etc/profile
export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
2.5 list类型常用命令
set类型常用命令
zset类型常用命令
键值和服务器命令
2.9 php使用Redis存储
cd /usr/local/src
mv develop phpredis.zip
yum install -y unzip
unzip phpredis.zip
cd phpredis-develop/
/usr/local/php/bin/phpize
./configure
--with-php-config=/usr/local/php/bin/php-config
make
make install
vi /usr/local/php/etc/php.ini
最后添加一行
extension
= redis.so
vi /usr/local/apache2/conf/httpd.conf
去掉 servername 前的 #
lphp.ini中加入
session.save_handler = "redis"
session.save_path = "tcp://127.0.0.1:6379"
或者apache虚拟主机加入
php_valuesession.save_handler " redis"
php_valuesession.save_path " tcp://127.0.0.1:6379"
或者php-fpm.conf对应的pool中加入
php_value[session.save_handler] = redis
php_value[session.save_path] = " tcp://127.0.0.1:6379
3.0 MongoDB介绍
3.1 MongoDB安装
vim /etc/yum.repos.d/mongodb-org-3.0.repo加入:[mongodb-org-3.0]name=MongoDB Repositorybaseurl=http://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/gpgcheck=0enabled=1
yum install -y mongodb-org
ls /etc/init.d/mongod
ls /etc/mongod.conf
ls /var/log/mongodb/
ls /var/lib/mongo/
vim /etc/mongod.conf
/etc/init.d/mongod start
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
vim /etc/security/limits.con
mongod soft nofile 64000mongod hard nofile 64000mongod soft nproc 32000mongod hard nproc 32000
service mongod start
启动出现错误 第二个启动失败
mongod -f /etc/mongod.conf
ERROR: child process failed, exited with error number 100
rm /var/lib/mongo/mongod.lock 删除 重启 就好了
MongoDB连接和用户管理
mongo
#创建用户db.createUser({user:"admin",pwd:"123456",roles:[{role:'dbOwner',db:'userdb'}]})#查看用户db.system.users.find() //列出所有用户,需要切换到admin库下(use admin)show users //查看当前库下所有的用户#删除用户,需要切换到admin库db.dropUser('admin')
3.3 MongoDB库和集合管理
db.version()
MongoDB副本集搭建
MongoDB副本集测试
MongoDB备份和恢复
php连接MongoDB