postgresql备份问题:aborting because of server version mismatch

备份时提示错误信息:

1
2
pg_dump: server version: 9.4.5; pg_dump version: 9.3.13
pg_dump: aborting because of server version mismatch

原因是pg_dump版本问题。

解决办法:
1、根据官网提示下载,安装指定版本的postgresql(这里是9.4版本)

2、找出安装的pg_dump的版本

1
2
3
4
5
6
7
8
9

> find / -name pg_dump -type f 2>/dev/null

# 我的输出:

/usr/lib/postgresql/9.4/bin/pg_dump

/usr/lib/postgresql/9.3/bin/pg_dump

3、进行更新

1
> sudo ln -s /usr/lib/postgresql/9.4/bin/pg_dump /usr/lib/postgresql/9.3/bin/pg_dump --force

4、再次备份成功

Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,
Given input array A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Solution {
public int removeDuplicates(int[] A) {
if(A.length==0){
return 0;
}
int i=1,j=1;
for(;j<A.length;){
if(A[i-1]!=A[j]){
A[i]=A[j];
i++;
}
j++;
}

return i;
}
}

Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree and sum = 22,

          5
         / \
        4   8
       /   / \
      11  13  4
     /  \      \
    7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
//一次提交就过、、
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if(root==null){
return false;
}
sum=sum-root.val;
if(root.left==null&&root.right==null&&sum==0){
return true;
}

return (hasPathSum(root.left,sum)||hasPathSum(root.right,sum));
}
}

Merge Two Sorted List

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1==null&&l2==null){
return null;
}
ListNode resultList = new ListNode(0);
ListNode curNode = resultList;
while(l1!=null||l2!=null){
if(l1!=null&&l2!=null){
if(l1.val<l2.val){
curNode.next=l1;
l1 = l1.next;
}else{
curNode.next=l2;
l2 = l2.next;
}
curNode =curNode.next;
}else if(l1!=null){
curNode.next = l1;
break;
}else{
curNode.next=l2;
break;
}
}
return resultList.next;
}
}

Spring静态注入

需求:在静态方法中注入并使用Spring管理下的bean。

解决方案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import javax.annotation.PostConstruct;

public class DemoUtil {
private static DemoUtil demoUtil;
@Autowire
public DemoServ demoServ;//需注入的对象

/**
* 构造方法执行后调用 init()
*/
@PostConstruct
public void init() {
demoUtil = this;
demoUtil.demoServ = this.demoServ;
}

public static void utilMethod() {
demoUtil.demoServ.method1();
}
}

Length of Last Word

Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘, return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

For example,
Given s = “Hello World”,
return 5.

1
2
3
4
5
6
7
8
9
10
//开始length(),trim忘加
public class Solution {
public int lengthOfLastWord(String s) {
if(s==null||"".equals(s)||s.trim().equals("")){
return 0;
}
String[] r=s.split(" ");
return r[r.length-1].length();
}
}

监听文件变化

使用场景

对于某些文件的变化进行监听操作(如IDE及各编辑器监听文件变化来进行同步,应用服务器监听如jsp、jar等文件的变化来重新部署)。
个人学习使用该API的目的是想监听功能开关配置文件,达到修改文件后,程序重新加载配置到内存的目的。

例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;

public class PathsDemo {
public static void main(String[] args){
try {
//The first step is to create a new WatchService by using the newWatchService method in the FileSystem class.
WatchService watchService=FileSystems.getDefault().newWatchService();

//register one or more objects with the watch service.
//When registering an object with the watch service, you specify the types of events that you want to monitor.
Paths.get(propertiesPath).register(watchService,
StandardWatchEventKinds.ENTRY_CREATE, //A directory entry is created
StandardWatchEventKinds.ENTRY_DELETE, //A directory entry is deleted
StandardWatchEventKinds.ENTRY_MODIFY);//A directory entry is modified.

while(true)
{
//Returns a queued key. If no queued key is available, this method waits.
WatchKey key=watchService.take();
for(WatchEvent<?> event:key.pollEvents())
{
System.out.println(event.context()+" happens "+event.kind()+" event.");
//TODO business operations...
}
if(!key.reset())
{
break;
}
}
} catch (Exception e) {
System.err.println(e);
}
}
}
1
2
//控制台输出
config.properties happen ENTRY_MODIFY event.
注意

Java7 才支持NIO Paths。
该API 设计不是用于索引硬盘。

Jenkins系列(一): 安装

参考官网进行如下操作。

This is the Debian package repository of Jenkins to automate installation and upgrade. To use this repository, first add the key to your system:

1
wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -

Then add the following entry in your /etc/apt/sources.list:

1
deb http://pkg.jenkins-ci.org/debian binary/

Update your local package index, then finally install Jenkins:

1
2
sudo apt-get update
sudo apt-get install jenkins

完成后,浏览器端输入 [主机ip 或 主机域名]:8080,即可看到如下界面,说明安装成功。

安装成功

MySQL重置root密码(Ubuntu)

1、停止mysql(也可使用其它有效命令结束)

1
sudo stop mysql

2、以不检查权限的方式启动MySQL

1
2
mysqld --skip-grant-tables &
mysql -u root#用root空密码登录即可进入MySQL命令行

3、修改root用户密码

1
2
3
mysql> update mysql.user set password=PASSWORD('newpassword') where User='root'; 
mysql> flush privileges;
mysql> quit

4、结束MySQL进程(不检查权限的进程),并正常启动MySQL即可

1
ps -ef #查看进程

MySQL进程

1
2
kill -9 [不检查权限的MySQL进程]#kill -9 4793 结束该进程
sudo start mysql

SSH登录配置(Mac登录Ubuntu)

Ubuntu服务器

1、生成公私钥,按提示步骤操作

1
ssh-keygen -t rsa

2、将生成的公钥放入下面文件

1
cat id_rsa.pub >> /root/.ssh/authorized_keys

3、将私钥移动到本机,删除服务器的私钥

Mac电脑

1、打开ssh配置

1
sudo vi /etc/ssh_config

2、配置中添加如下内容

1
2
3
4
Host [别名]
HostName [主机IP]
User [登录名]
IdentityFile [私钥位置]

3、此时,ssh [别名] 可登录服务器。