1. 在用户目录下新建一个.ssh目录,并将其目录权限改为700(仅用户自身有读写操作权限):

mkdir .ssh
chmod 700 .ssh

2. 进入.ssh目录,使用ssh-keygen命令生成rsa密钥对

# 一路回车即可
ssh-keygen -t rsa -b 4096 -C "[email protected]"

3. 这时生成了两个文件:id_rsaid_rsa.pub,其中前一个为私钥,后一个为公钥,公钥须保留在服务器上,私钥拷贝到客户端机器上

4. 在.ssh目录中新建一个文件名为:authorized_keys,将公钥内容拷贝到这个文件中,并将文件权限改为600(仅用户自身有读写权限):

touch authorized_keys
cat id_rsa.pub >> authorized_keys
chmod 600 authorzied_keys

5. 修改sshd_config配置如下:

vi /etc/ssh/sshd_config
 
#禁用root账户登录,如果是用root用户登录请开启
PermitRootLogin yes
 
# 是否让 sshd 去检查用户家目录或相关档案的权限数据,
# 这是为了担心使用者将某些重要档案的权限设错,可能会导致一些问题所致。
# 例如使用者的 ~.ssh/ 权限设错时,某些特殊情况下会不许用户登入
StrictModes no
 
# 是否允许用户自行使用成对的密钥系统进行登入行为,仅针对 version 2。
# 至于自制的公钥数据就放置于用户家目录下的 .ssh/authorized_keys 内
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys
 
# 有了证书登录了,就禁用密码登录吧,安全要紧
PasswordAuthentication no

阅读全文

1. 下载测速工具speedtest:

wget https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py

2. 给予可执行权限:

chmod +x speedtest.py

3. 测试使用:

4. 如果想方便的使用speedtest-cli:

mv speedtest.py /usr/local/sbin/speedtest-cli

阅读全文

1. 添加Centos7的epel及remi源:

yum install epel-release
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

2.使用yum list命令查看可安装的包:

yum list --enablerepo=remi --enablerepo=remi-php56 | grep php

3. 使用yum install 命令安装php5.6:

 yum install --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-pecl-apcu php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof php-pdo php-pear php-fpm php-cli php-xml php-bcmath php-process php-gd php-common

4. 安装PHP-fpm并设置为开机自启动:

yum install --enablerepo=remi --enablerepo=remi-php56 php-fpm 
systemctl enable php-fpm.service

5. 查看PHP版本:

php -v

阅读全文

Promise in js

回调函数真正的问题在于他剥夺了我们使用 return 和 throw 这些关键字的能力。而 Promise 很好地解决了这一切。

2015 年 6 月,ECMAScript 6 的正式版 终于发布了。

ECMAScript 是 JavaScript 语言的国际标准,JavaScript 是 ECMAScript 的实现。ES6 的目标,是使得 JavaScript 语言可以用来编写大型的复杂的应用程序,成为企业级开发语言。

概念

ES6 原生提供了 Promise 对象。

所谓 Promise,就是一个对象,用来传递异步操作的消息。它代表了某个未来才会知道结果的事件(通常是一个异步操作),并且这个事件提供统一的 API,可供进一步处理。

Promise 对象有以下两个特点。

(1)对象的状态不受外界影响。Promise 对象代表一个异步操作,有三种状态:Pending(进行中)、Resolved(已完成,又称 Fulfilled)和 Rejected(已失败)。只有异步操作的结果,可以决定当前是哪一种状态,任何其他操作都无法改变这个状态。这也是 Promise 这个名字的由来,它的英语意思就是「承诺」,表示其他手段无法改变。

(2)一旦状态改变,就不会再变,任何时候都可以得到这个结果。Promise 对象的状态改变,只有两种可能:从 Pending 变为 Resolved 和从 Pending 变为 Rejected。只要这两种情况发生,状态就凝固了,不会再变了,会一直保持这个结果。就算改变已经发生了,你再对 Promise 对象添加回调函数,也会立即得到这个结果。这与事件(Event)完全不同,事件的特点是,如果你错过了它,再去监听,是得不到结果的。

有了 Promise 对象,就可以将异步操作以同步操作的流程表达出来,避免了层层嵌套的回调函数。此外,Promise 对象提供统一的接口,使得控制异步操作更加容易。

Promise 也有一些缺点。首先,无法取消 Promise,一旦新建它就会立即执行,无法中途取消。其次,如果不设置回调函数,Promise 内部抛出的错误,不会反应到外部。第三,当处于 Pending 状态时,无法得知目前进展到哪一个阶段(刚刚开始还是即将完成)。

var promise = new Promise(function(resolve, reject) {
 if (/* 异步操作成功 */){
 resolve(value);
 } else {
 reject(error);
 }
});

promise.then(function(value) {
 // success
}, function(value) {
 // failure
});

Promise 构造函数接受一个函数作为参数,该函数的两个参数分别是 resolve 方法和 reject 方法。

如果异步操作成功,则用 resolve 方法将 Promise 对象的状态,从「未完成」变为「成功」(即从 pending 变为 resolved);

如果异步操作失败,则用 reject 方法将 Promise 对象的状态,从「未完成」变为「失败」(即从 pending 变为 rejected)。

基本的 api

  1. Promise.resolve()
  2. Promise.reject()
  3. Promise.prototype.then()
  4. Promise.prototype.catch()
  5. Promise.all() // 所有的完成

     var p = Promise.all([p1,p2,p3]);
  6. Promise.race() // 竞速,完成一个即可

进阶

promises 的奇妙在于给予我们以前的 return 与 throw,每个 Promise 都会提供一个 then() 函数,和一个 catch(),实际上是 then(null, ...) 函数,

    somePromise().then(functoin(){
        // do something
    });

我们可以做三件事,

1. return 另一个 promise
2. return 一个同步的值 (或者 undefined)
3. throw 一个同步异常 ` throw new Eror('');`

1. 封装同步与异步代码

```
new Promise(function (resolve, reject) {
 resolve(someValue);
 });
```
写成

```
Promise.resolve(someValue);
```

2. 捕获同步异常

 new Promise(function (resolve, reject) {
 throw new Error('悲剧了,又出 bug 了');
 }).catch(function(err){
 console.log(err);
 });

如果是同步代码,可以写成

    Promise.reject(new Error("什么鬼"));

3. 多个异常捕获,更加精准的捕获

somePromise.then(function() {
 return a.b.c.d();
}).catch(TypeError, function(e) {
 //If a is defined, will end up here because
 //it is a type error to reference property of undefined
}).catch(ReferenceError, function(e) {
 //Will end up here if a wasn't defined at all
}).catch(function(e) {
 //Generic catch-the rest, error wasn't TypeError nor
 //ReferenceError
});

4. 获取两个 Promise 的返回值

1. .then 方式顺序调用
2. 设定更高层的作用域
3. spread

5. finally

任何情况下都会执行的,一般写在 catch 之后

6. bind

somethingAsync().bind({})
.spread(function (aValue, bValue) {
 this.aValue = aValue;
 this.bValue = bValue;
 return somethingElseAsync(aValue, bValue);
})
.then(function (cValue) {
     return this.aValue + this.bValue + cValue;
});

或者 你也可以这样

var scope = {};
somethingAsync()
.spread(function (aValue, bValue) {
 scope.aValue = aValue;
 scope.bValue = bValue;
 return somethingElseAsync(aValue, bValue);
})
.then(function (cValue) {
 return scope.aValue + scope.bValue + cValue;
});

然而,这有非常多的区别,

  1. 你必须先声明,有浪费资源和内存泄露的风险
  2. 不能用于放在一个表达式的上下文中
  3. 效率更低

7. all。非常用于于处理一个动态大小均匀的 Promise 列表

8. join。非常适用于处理多个分离的 Promise

```
var join = Promise.join;
join(getPictures(), getComments(), getTweets(),
 function(pictures, comments, tweets) {
 console.log("in total: " + pictures.length + comments.length + tweets.length);
});
```

9. props。处理一个 promise 的 map 集合。只有有一个失败,所有的执行都结束

```
Promise.props({
 pictures: getPictures(),
 comments: getComments(),
 tweets: getTweets()
}).then(function(result) {
 console.log(result.tweets, result.pictures, result.comments);
});
```

10. any 、some、race

```
Promise.some([
 ping("ns1.example.com"),
 ping("ns2.example.com"),
 ping("ns3.example.com"),
 ping("ns4.example.com")
], 2).spread(function(first, second) {
 console.log(first, second);
}).catch(AggregateError, function(err) {

err.forEach(function(e) {
console.error(e.stack);
});
});;

```
有可能,失败的 promise 比较多,导致,Promsie 永远不会 fulfilled

11. .map(Function mapper [, Object options])

用于处理一个数组,或者 promise 数组,

Option: concurrency 并发现

    map(..., {concurrency: 1});

以下为不限制并发数量,读书文件信息

var Promise = require("bluebird");
var join = Promise.join;
var fs = Promise.promisifyAll(require("fs"));
var concurrency = parseFloat(process.argv[2] || "Infinity");

var fileNames = ["file1.json", "file2.json"];
Promise.map(fileNames, function(fileName) {
 return fs.readFileAsync(fileName)
 .then(JSON.parse)
 .catch(SyntaxError, function(e) {
 e.fileName = fileName;
 throw e;
 })
}, {concurrency: concurrency}).then(function(parsedJSONs) {
 console.log(parsedJSONs);
}).catch(SyntaxError, function(e) {
 console.log("Invalid JSON in file " + e.fileName + ": " + e.message);
});

结果

$ sync && echo 3 > /proc/sys/vm/drop_caches
$ node test.js 1
reading files 35ms
$ sync && echo 3 > /proc/sys/vm/drop_caches
$ node test.js Infinity
reading files: 9ms

11. .reduce(Function reducer [, dynamic initialValue]) -> Promise

Promise.reduce(["file1.txt", "file2.txt", "file3.txt"], function(total, fileName) {
 return fs.readFileAsync(fileName, "utf8").then(function(contents) {
 return total + parseInt(contents, 10);
 });
}, 0).then(function(total) {
 //Total is 30
});

12. Time

  1. .delay(int ms) -> Promise
  2. .timeout(int ms [, String message]) -> Promise

Promise 的实现

  1. q
  2. bluebird
  3. co
  4. when

ASYNC

async 函数与 Promise、Generator 函数一样,是用来取代回调函数、解决异步操作的一种方法。它本质上是 Generator 函数的语法糖。async 函数并不属于 ES6,而是被列入了 ES7。

参考文献(说是抄也可以的):

阮一峰的ES6 教程
关于promises,你理解了多少?
Bluebird 的官方文档

阅读全文

1. 下载yum的repo源并安装,执行安装后,在/etc/yum.repos.d/这个目录下多出mysql-community-source.repomysql-community.repo:

# Centos6.5:
wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
rpm -ivh mysql-community-release-el6-5.noarch.rpm
 
# Centos7:
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm

2. 用yum repolist mysql 查看是否已经有mysql可安装文件:

yum repolist all | grep mysql

3. 安装mysql服务:

yum install mysql-community-server

4. 安装成功后,启动mysql服务:

systemctl start mysqld

5. mysql安装后密码默认为空,需要修改密码:

mysql -u root
use mysql;
update user set password=PASSWORD("这里输入root用户密码") where User='root';
flush privileges; 

6. 设置mysql服务自启动:

systemctl enable mysqld

7. mysql安全问题:

mysql_secure_installation
 
Enter current password for root (enter for none): <- 初次运行直接回车
Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车
Remove anonymous users? [Y/n] <– 是否删除匿名用户,生产环境建议删除,所以直接回车
Disallow root login remotely? [Y/n] <–是否禁止root远程登录,根据自己的需求选择Y/n并回车
Remove test database and access to it? [Y/n] <– 是否删除test数据库,直接回车
Reload privilege tables now? [Y/n] <– 是否重新加载权限表,直接回车

8.本地远程连接可能会出现1130错误,需要设置root的'host'为'%':

# 以权限用户root登录
mysql -u root -p
 
# 选择mysql库
use mysql; 
 
# 查看mysql库中的user表的host值
select 'host' from user where user='root'; 
 
# 修改host值,如果这步出错'ERROR 1062 (23000): Duplicate entry `%-root` for key `PRIMARY`'说明该记录有了
update user set host = '%' where user ='root'; 
 
# 刷新mysql的系统权限相关表
flush privileges; 
 
# 再重新查看user表
select 'host' from user where user='root';

9. 重启mysql服务:

systemctl restart mysqld;

参考文章

  1. linux CentOS6.5 yum安装mysql 5.6

阅读全文