Redis基础

  1. 安装
  2. 启动
    redis-server; (默认地址127.0.0.1, port:6379)
    redis-server –port 6380(修改启动端口)
    通过初始化脚本启动:utils/redis_init_script:包含redis-server的主要配置,可以修改,也可以从后面的通过参数修噶
  3. 停止
    强制终止会导致缓存数据丢失
    redis-cli SHUTDOWN:正常关闭,首先会断开所有客户端连接,然后持久化内存中的数据到磁盘
  4. redis-cli客户端连接
    (1)redis-cli -h <ip addr> -p <port number> — 远程连接redis server
    (2)不带参数执行redis-cli, 进入交互模式,可以发多条命令
  5. redis-cli命令返回值
    (1)返回状态:set值返回ok,ping返回pong等
    (2)输入错误命令:提示 unknown command “<错误命令串>”
    (3)返回整数值:incr(返回递增键值);dbsize;
    (4)字符串回复:get — 获取key的value为字符串的值
    (5)多行字符串回复:keys * — 按照一定规则返回符合规则的键值列表
  6. 配置
    (1)可以修改redis.conf或者redis.windows.conf文件,启动的时候作为参数传入
    (2)redis server已经启动了,可以即使修改属性,如config set loglevel warning
  7. 多数据库
    select <编号> –切换到某个编号的数据库,redis不存在数据库名,不支持一个实例里面不同数据库用不同的权限,多个数据库之间没有完全隔离,所以最好不用业务来分库,可以根据不同环境分库,如开发环境,测试环境等。

Redis启动错误

D:\redis-2.8.17>redis-server.exe redis.windows.conf
[4692] 27 Nov 12:14:05.995 #
The Windows version of Redis allocates a memory mapped heap for sharing with
the forked process used for persistence operations. In order to share this
memory, Windows allocates from the system paging file a portion equal to the
size of the Redis heap. At this time there is insufficient contiguous free
space available in the system paging file for this operation (Windows error
0x5AF). To work around this you may either increase the size of the system
paging file, or decrease the size of the Redis heap with the --maxheap flag.
Sometimes a reboot will defragment the system paging file sufficiently for
this operation to complete successfully.

Please see the documentation included with the binary distributions for more
details on the --maxheap flag.

Redis can not continue. Exiting.

解决方案:redis.windows.conf中增加 maxheap 1024000000

java操作mongodb示例2

public class SimpleCRUDTest {
	private Mongo mongo = null;
	private DB db = null;
	private DBCollection collection = null;
	public SimpleCRUDTest(){
		try {
			mongo = new Mongo();
			db = mongo.getDB("test");
			collection = db.getCollection("things");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
	
	public void queryAll(){
		DBCursor cursor = collection.find();
		while(cursor.hasNext()){
			System.out.println(cursor.next());
		}
	}
	
	public void simpleAdd(){
		DBObject object = new BasicDBObject();
		object.put("name", "long0416");
		object.put("age", 12);
		object.put("sex", "male");
		WriteResult result = collection.save(object);
		System.out.println("受影响条数: " + result.getN());
	}
	
	public void complexAdd(){
		List<DBObject> objectList = new ArrayList<DBObject>();
		DBObject object = new BasicDBObject();
		object.put("name", "longtest1");
		object.put("sex", "female");
		
		DBObject object2 = new BasicDBObject();
		object2.put("name", "longtest2");
		object2.put("age", 11);
		
		objectList.add(object);
		objectList.add(object2);
		
		collection.insert(objectList);
	}
	
	public void update(){
		BasicDBObject updateObject = new BasicDBObject();
		updateObject.put("x", 4.0);
		updateObject.put("j", "sss");
		WriteResult result = collection.update(new BasicDBObject("x", 4.0), updateObject);
		System.out.println("受影响条数:" + result);
	}
	
	public void findAndUpdate(){
		BasicDBObject updateObject = new BasicDBObject();
		updateObject.put("x", 4.0);
		updateObject.put("j", "tt");
		DBObject object = collection.findAndModify(new BasicDBObject("x", 4.0), updateObject);
		System.out.println(object);
	}
	
	public void remove(){
		System.out.println(collection.remove(new BasicDBObject("j", new BasicDBObject("$gte", 8))).getN());
	}
	
	public static void main(String[] args) {
		SimpleCRUDTest simpleTest = new SimpleCRUDTest();
		System.out.println("添加前");
		simpleTest.queryAll();
		System.out.println("添加开始");
		// simpleTest.simpleAdd();
		// simpleTest.complexAdd();
		// simpleTest.update();
		// simpleTest.remove();
		//simpleTest.findAndUpdate();
		//simpleTest.queryAll();
		System.out.println("添加结束");
	}

java操作mongodb简单示例

public class SimpleTest {
	public static void main(String[] args) {
		Mongo mongo;
		try {
			mongo = new Mongo();
			List<String> dbNames = mongo.getDatabaseNames();
			for(String dbName : dbNames){
				System.out.println(dbName);
			}
			DB db = mongo.getDB("test");
			System.out.println("======================================");
			Set<String> collectionSet = db.getCollectionNames();
			for(String collectionName : collectionSet){
				System.out.println(collectionName);
			}
			DBCollection collection = db.getCollection("things");
			DBCursor cursor = collection.find();
			while(cursor.hasNext()){
				DBObject object = cursor.next();
				System.out.println(object);
			}
			System.out.println("count:" + cursor.count());
			//System.out.println("length: " + cursor.length());
			System.out.println("size: " + cursor.size());
			System.out.println("cursorId: " + cursor.getCursorId());
			System.out.println("json: " + JSON.serialize(cursor));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
}

Mysql 重新安装遇到的问题

当安装到Mysql 最后一步execute的时候:
出现“could not start the service”的问题。
解决方案:停用Mysql的服务,然后在MySQL的bin目录下执行“mysqld-nt -remove”再重新execute(execute中第三步就可以通过)
出现“mysql Apply security setting Error Nr 1045”
解决方案:
1.到控制面板里面先把mysql删除.
2.到c盘C:/Program Files目录下把mysql目录删除.
3.如果在其他盘还有设置目录也要删除.空目录也要删除,最好全盘搜索下(c:用户/我的文档/Navicat/MySQL和MySQL安装目录首先需要删除)
4.到regedit把注册表
HKEY_LOCAL_MACHINE/SYSTEM/ControlSet001/Services/Eventlog/Applications/MySQL
HKEY_LOCAL_MACHINE/SYSTEM/ControlSet002/Services/Eventlog/Applications/MySQL
HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/Eventlog/Applications/MySQL
其他的最好也ctrl + f 然后全删除了
5.如果任务管理器里有关mysql内容也删除
6.把防火墙关了
7.重新装mysql(这里我没有重启计算机)