Linux查看文件指定行数内容

1、tail date.log 输出文件末尾的内容,默认10行

tail -20  date.log        输出最后20行的内容

tail -n -20  date.log    输出倒数第20行到文件末尾的内容

tail -n +20  date.log   输出第20行到文件末尾的内容

tail -f date.log            实时监控文件内容增加,默认10行。

2、head date.log 输出文件开头的内容,默认10行

head -15  date.log     输出开头15行的内容

head -n +15 date.log 输出开头到第15行的内容

head -n -15 date.log  输出开头到倒数第15行的内容

3、sed -n "开始行,结束行p" 文件名

sed -n '70,75p' date.log             输出第70行到第75行的内容

sed -n '6p;260,400p; ' 文件名    输出第6行 和 260到400行

sed -n 5p 文件名                       输出第5行

tail 和 head 加上 -n参数后 都代表输出到指定行数,tail 是指定行数到结尾,head是开头到指定行数

+数字 代表整数第几行, -数字代表倒数第几行

原文链接 https://www.cnblogs.com/zeke-python-road/p/9455048.html

最近重新安装了MySQL,遇到一些问题,发现几篇文章,觉得有用就转过来了。

MySQL5.7开启远程访问及Ubuntu18.04防火墙3306端口

在虚拟机中安装了Ubuntu18.04,MySQL5.7。系统默认的root等只能在本地访问,host被限制为localhost,为了进行Java程序测试,本地eclipse访问虚拟机的数据库,避免用户管理混乱,特意新建一数据库和用户。

新建数据库ttmsg,用户ub64,开启ub64用户远程访问的过程。

--新建数据库ttmsg
 
mysql> CREATE DATABASE ttmsg;
Query OK, 1 row affected (0.00 sec)
 
--新建用户ub64,和登陆密码,设置访问限制为%,允许远程访问。
mysql> CREATE USER 'ub64'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.01 sec)
 
--给新建的用户ub64访问数据库ttmsg的所有权限
mysql> grant all privileges on ttmsg.* to 'ub64'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
--刷新权限列表,这样就可以用ub64用户登陆了
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
--新建数据表,开始测试。
mysql> CREATE TABLE Student(
    ->    ID   INT NOT NULL AUTO_INCREMENT,
    ->    NAME VARCHAR(20) NOT NULL,
    ->    AGE  INT NOT NULL,
    ->    PRIMARY KEY (ID)
    -> );
Query OK, 0 rows affected (0.01 sec)
 
mysql> show tables;
+-----------------+
| Tables_in_ttmsg |
+-----------------+
| Student         |
+-----------------+
1 row in set (0.00 sec)

在eclipse中调试java程序,尝试往Student表中插入数据,报错了。

------Records Creation--------
Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
 
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:81)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:612)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:862)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:917)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:927)
    at com.tutorialspoint.StudentJDBCTemplate.create(StudentJDBCTemplate.java:14)
    at com.tutorialspoint.MainApp.main(MainApp.java:13)
Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

好吧,检查报错说远程JDBC创建失败,没连上。检查一下MySQL是不是没有开通对外的3306端口过滤,导致外部地址无法访问呢,通过netstat命令,检查3306端口,果然只有一个127.0.0.1:3306的监听端口。

ub64@ub64-1804-1:~$ netstat -ntpl
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:23              0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      -
tcp6       0      0 :::22                   :::*                    LISTEN      -
tcp6       0      0 ::1:6010                :::*                    LISTEN      -

怀疑是否配置mysqld.cnf文件,检查bind-address的设置值问题。

vim /etc/mysql/mysql.conf.d/mysqld.cnf

#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address            = 127.0.0.1

好吧,把bind-address修改成0.0.0.0,无限制,重启mysql服务。重新检查netstat,3306端口的访问已经有所有来源地址的监听了。

bind-address            = 0.0.0.0
 
ub64@ub64-1804-1:~$ service mysql restart
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'mysql.service'.
Authenticating as: ub64-1804-1 (ub64)
Password:
==== AUTHENTICATION COMPLETE ===
ub64@ub64-1804-1:~$ netstat -ntpl
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:23              0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      -
tcp6       0      0 :::22                   :::*                    LISTEN      -
tcp6       0      0 ::1:6010                :::*                    LISTEN      -

继续程序代码测试,重新跑数据库访问程序。额(⊙﹏⊙),又出错了,还是一样的报错信息,还是没有建立连接。

------Records Creation--------
Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
 
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:81)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:612)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:862)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:917)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:927)
    at com.tutorialspoint.StudentJDBCTemplate.create(StudentJDBCTemplate.java:14)
    at com.tutorialspoint.MainApp.main(MainApp.java:13)
Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

好吧,那应该就是系统防火墙没有开了,我也没有设置过。检查防火墙通过规则,果然grep一下没有见到3306的端口记录。

ub64@ub64-1804-1:~$ sudo iptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination
ufw-before-logging-input  all  --  0.0.0.0/0            0.0.0.0/0
ufw-before-input  all  --  0.0.0.0/0            0.0.0.0/0
ufw-after-input  all  --  0.0.0.0/0            0.0.0.0/0
ufw-after-logging-input  all  --  0.0.0.0/0            0.0.0.0/0
......
 
ub64@ub64-1804-1:~$ sudo iptables -L -n | grep 3306
ub64@ub64-1804-1:~$ 

那就好办了,iptables新增一条3306的端口允许通过规则,在重新检查一下iptables,这下有了。

ub64@ub64-1804-1:~$ sudo iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
 
ub64@ub64-1804-1:~$ sudo iptables -L -n | grep 3306
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:3306
ub64@ub64-1804-1:~$ 

重新测试程序,程序访问成功。果然,开启mysql的访问,不仅要设置user表用户的访问控制权限,还要设置mysqld.cnf的bind-address,同时系统防火墙规则也要配置好3306的端口通过权限。这3个地方的控制缺一不可。

//程序日志
------Records Creation--------
Created Record Name = Zara Age = 11
Created Record Name = Nuha Age = 2
Created Record Name = Ayan Age = 15
 
mysql> select * from Student ;
+----+------+-----+
| ID | NAME | AGE |
+----+------+-----+
|  1 | Zara |  11 |
|  2 | Nuha |   2 |
|  3 | Ayan |  15 |
+----+------+-----+
3 rows in set (0.00 sec)

另外,在程序访问数据库进行操作的日志中出现了WARN告警如下:

Sun Oct 07 21:21:50 GMT+08:00 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

上网搜了一下,以下参考来自我是康小小的CSDN博客


原来是Mysql数据库的SSL连接问题,提示警告不建议使用没有带服务器身份验证的SSL连接,是在MYSQL5.5.45+, 5.6.26+
and 5.7.6+版本中才有的这个问题。解决办法在警告中已经说明了:

1.在数据库连接的url中添加useSSL=false;

jdbc:mysql://localhost:3306/test?useSSL=false
或者在使用Java进行JDBC连接的时候,可以在Properties对象中设置useSSL的值为false。 

2.url中添加useSSL=true,并且提供服务器的验证证书。

参考来源地址:https://blog.csdn.net/u010429286/article/details/77750177


作者:秋野001
来源:CSDN
原文:https://blog.csdn.net/ZENMELAOSHIYOUREN/article/details/82961610

在最近的项目中用到了websocket,遇到了一些问题,记下来。

案情重现

首先实例化一个连接(随便找的回声测试)

var ws = new WebSocket('ws://121.40.165.18:8800')

然后想当然的加上事件监听↓

ws.addEventListener('message', fun(event))

function fun(e) {
    console.log(e.data)
}

然后发现不起作用,而改用匿名函数就没啥问题。

ws.addEventListener('message', function (event) {
    console.log(event.data)
})

笔记

其实仔细想想就没发现了,第一次ws.addEventListener('message', fun(event))中传入的fun(event)不是一个函数,而是fun(event)的返回值undefind
正确的方法是

ws.addEventListener('message', fun)

举个例子

function main(f) {
    f('123')
}
function fun(str) {
    console.log(str)
    return 'qwe'
}
main(fun)//'123'
main(fun())// Uncaught TypeError: f is not a function

其中fun是作为一个变量传入的main函数,而如果传入fun(),则传入的实际是fun()的返回值'qwe'

正确的操作如下

var ws = new WebSocket('ws://121.40.165.18:8800')

ws.addEventListener('message', fun)

function fun(e) {
    console.log(e.data)
}

我可真是个小机灵鬼呢~

最近查资料忽然发现了一个很有意思的博客X.D笔记
我从百度搜索页跳转过去,他给我弹了个浮层

nobd.png

然后果断抄了代码,因为原始代码依赖jQuery,所以我自己修改了一下。
下面是修改后的代码↓

function removeElement(_element) {
    var _parentElement = _element.parentNode
    if (_parentElement) {
        _parentElement.removeChild(_element)
    }
}

document.ready = function (callback) {
    ///兼容FF,Google
    if (document.addEventListener) {
        document.addEventListener('DOMContentLoaded', function () {
            document.removeEventListener('DOMContentLoaded', arguments.callee, false)
            callback()
        }, false)
    }
    //兼容IE
    else if (document.attachEvent) {
        document.attachEvent('onreadytstatechange', function () {
            if (document.readyState == "complete") {
                document.detachEvent("onreadystatechange", arguments.callee)
                callback()
            }
        })
    } else if (document.lastChild == document.body) {
        callback()
    }
}

function closeBD() {
    document.body.style["overflow-y"] = 'auto'
    removeElement(removeElement(document.getElementById('nobdDiv')))
}

function noBD() {
    var html = document.createElement("div")
    html.innerHTML = '<div id="nobdDiv" style="top:0px;display: block;position: fixed;width: 100%;height: 100%;z-index:999;background-color: rgba(0,0,0,0.88);left: 0;"><div style="margin: 70px auto;color:#fff;text-align: center;font-family:" Microsoft YaHei "><h1>拒绝百度,现在开始</h1><p>做为一个技术人员,还在使用百度这种<strong style="color: #D6007F;">垃圾搜索引擎</strong>?</p><p>1. 搜索质量实在<span style="color: #D6007F;">低能</span>,你可能找不到你想要的,找到时可能已经白白浪费几小时!</p><p>2. 百度毫无羞耻,获取出售用户隐私比垃圾软件还猖獗、毫无羞耻心</p><p>3. 唯利是图、没有半社会责任感,社会渣滓给钱就能在首页放广告,欺诈弱势人群。</p><p  style="margin-top: 50px;">请默念一句: “百度是垃圾”, <span style="color: #009BCA;cursor:pointer;" onclick="javascript:closeBD();">在此关闭</span></p></div></div>'
    document.body.appendChild(html)
    window.setTimeout(closeBD, 60000)
}

document.ready(function () {
    var url = document.referrer
    if (url && (url.search("http://") > -1 || url.search("https://") > -1)) {
        var refurl = url.match(/:\/\/(.[^/]+)/)[1]
        if (refurl.indexOf("baidu.com") > -1) {
            window.setTimeout(noBD, 3000)
        }
    }
})

下面是原始代码↓

function closeBD(){
    document.body.style["overflow-y"]='auto';
    $('#nobdDiv').remove();
}
function noBD(){
    var top = document.documentElement.scrollTop;
    document.body.style["overflow-y"]='hidden';
    var html='<div id="nobdDiv" style="top:'+top+'px;display: block;position: absolute;width: 100%;height: 100%;background-color: rgba(0,0,0,0.88);left: 0;"><div style="margin: 70px auto;color:#fff;text-align: center;font-family:" Microsoft YaHei ";"><h1>拒绝百度,现在开始</h1><p>做为一个技术人员,还在使用百度这种<strong style="color: #D6007F;">垃圾搜索引擎</strong>?</p><p>1. 搜索质量实在<span style="color: #D6007F;">低能</span>,你可能找不到你想要的,找到时可能已经白白浪费几小时!</p><p>2. 百度毫无羞耻,获取出售用户隐私比垃圾软件还猖獗、毫无羞耻心</p><p>3. 唯利是图、没有半社会责任感,社会渣滓给钱就能在首页放广告,欺诈弱势人群。</p><p  style="margin-top: 50px;">请默念一句: “百度是垃圾”, <span style="color: #009BCA;cursor:pointer;" onclick="javascript:closeBD();">再此关闭</span></p></div></div>';
    // var html='<div id="nobdDiv" style="top:'+top+'px;display: block;position: absolute;width: 100%;height: 100%;background-color: rgba(0,0,0,0.88);left: 0;"><div style="margin: 70px auto;color:#fff;text-align: center;font-family:"Microsoft YaHei";"><h1>拒绝百度,现在开始</h1><p>做为一个程序员,不应该使用百度这个垃圾引擎。</p><p>我不想干嘛,就是<span style="color: #D6007F;">黑屏一分钟</span>,<span style="color: #990000;">等不了就关网页</span>。</p><p>当然,如果你心里已经赞成了上一句话</p><p>默念一句:“百度是垃圾”,<span style="color: #009BCA;cursor:pointer;" onclick="javascript:closeBD();">就点我快速关闭吧</span></p><h2 style="margin-top: 50px;">为什么?</h2><p>1. 重点:做为一个程序员,你不觉得上百度搜不到东西浪费青春</p><p>2. 没有社会责任感、各种虚假广告,垃圾给钱就可以在前面骗人</p><p>3. 毫无羞耻的跟踪探取隐私行为,为了利益什么还有没基本廉耻</p><p>太多了,这个公司也配BAT,产品有几个给力的?不是早期垄断,Google退出,早灭了!</p></div></div>';
    $(document.body).append(html);
    window.setTimeout(closeBD,60000);
}
$(document).ready(function(){
   var url=document.referrer;
    if ( url && ( url.search("http://")>-1 || url.search("https://")>-1 ) ) {
        var refurl =  url.match(/:\/\/(.[^/]+)/)[1];
        if(refurl.indexOf("baidu.com")>-1){
            window.setTimeout(noBD,3000);
        }
    }
});