Rootop 服务器运维与web架构

2023-12-20
发表者 Venus
centos7下升级openssh到openssh-9.5p1版本已关闭评论

centos7下升级openssh到openssh-9.5p1版本

官网下载:https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/

[root@localhost ~]# yum install -y wget net-tools
[root@localhost ~]# wget -c https://www.openssl.org/source/openssl-3.1.4.tar.gz --no-check-certificate
[root@localhost ~]# wget -c https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-9.5p1.tar.gz

# 安装cmd.pm模块,不然编译的时候会引发【Can‘t locate IPC/Cmd.pm in @INC】错误。
[root@localhost ~]# yum install -y perl-CPAN perl-IPC-Cmd gcc gcc-c++

[root@localhost ~]# tar zxvf openssl-3.1.4.tar.gz
[root@localhost ~]# tar zxvf openssh-9.5p1.tar.gz

# 安装openssl 3.1.4版本
[root@localhost ~]# cd openssl-3.1.4
[root@localhost openssl-3.1.4]# ./config --prefix=/usr/local/openssl
[root@localhost openssl-3.1.4]# make
[root@localhost openssl-3.1.4]# make install

[root@localhost openssl-3.1.4]# /usr/local/openssl/bin/openssl version
/usr/local/openssl/bin/openssl: error while loading shared libraries: libssl.so.3: cannot open shared object file: No such file or directory
[root@localhost openssl-3.1.4]# ldd /usr/local/openssl/bin/openssl
	linux-vdso.so.1 =>  (0x00007fff9b5ad000)
	libssl.so.3 => not found
	libcrypto.so.3 => not found
	libdl.so.2 => /lib64/libdl.so.2 (0x00007f0cfe5c4000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f0cfe3a8000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f0cfdfda000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f0cfe7c8000)

# 可以看到2个库文件未找到
	libssl.so.3 => not found
	libcrypto.so.3 => not found
	
# 创建软连接,可以find搜索下然后创建软连接
[root@localhost openssl-3.1.4]# ln -s /usr/local/openssl/lib64/libssl.so.3 /usr/lib64/libssl.so.3
[root@localhost openssl-3.1.4]# ln -s /usr/local/openssl/lib64/libcrypto.so.3 /usr/lib64/libcrypto.so.3

[root@localhost openssl-3.1.4]# /usr/local/openssl/bin/openssl version
OpenSSL 3.1.4 24 Oct 2023 (Library: OpenSSL 3.1.4 24 Oct 2023)


# 安装openssh9.5
[root@localhost openssl-3.1.4]# yum install pam-devel libselinux-devel zlib-devel -y 
[root@localhost openssl-3.1.4]# rpm -e --nodeps $(rpm -qa | grep openssh)
[root@localhost openssl-3.1.4]# rm -f /etc/ssh/*
[root@localhost openssl-3.1.4]# cd ../openssh-9.5p1
[root@localhost openssh-9.5p1]# ./configure --prefix=/usr --sysconfdir=/etc/ssh --with-md5-passwords --with-pam --with-zlib --with-tcp-wrappers --with-ssl-dir=/usr/local/openssl --without-hardening
[root@localhost openssh-9.5p1]# make && make install
[root@localhost openssh-9.5p1]# cp contrib/redhat/sshd.init /etc/init.d/sshd
[root@localhost openssh-9.5p1]# cp contrib/redhat/sshd.pam /etc/pam.d/sshd

[root@localhost openssh-9.5p1]# echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config
[root@localhost openssh-9.5p1]# echo "PermitRootLogin yes" >> /etc/ssh/sshd_config

# 修改日志到/var/log/secure
[root@localhost openssh-9.5p1]# sed -i 's/#SyslogFacility AUTH/SyslogFacility AUTHPRIV/g' /etc/ssh/sshd_config

# 服务管理
[root@localhost openssh-9.5p1]# cat /usr/lib/systemd/system/sshd.service 
[Unit]
Description=OpenSSH server daemon
After=network.target

[Service]
Type=simple
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

[root@localhost openssh-9.5p1]# systemctl daemon-reload
[root@localhost openssh-9.5p1]# systemctl start sshd
[root@localhost openssh-9.5p1]# systemctl enable sshd


# 升级openssh后调不到/usr/local/bin 下的命令
[root@localhost ~]# vi .bash_profile 
PATH=$PATH:$HOME/bin:/usr/local/bin
[root@localhost ~]# source .bash_profile 

2023-12-11
发表者 Venus
mysql从库报错HA_ERR_FOUND_DUPP_KEY已关闭评论

mysql从库报错HA_ERR_FOUND_DUPP_KEY

Worker 1 failed executing transaction 'ANONYMOUS' at source log mysql-bin.000973, end_log_pos 17372877; 
Could not execute Write_rows event on table dkmirel_gamemotors.casinoaccounting; 
Duplicate entry 'GeneGNMI-2023-12-07 00:00:00-USD-Game24_95_v1' for key 'casinoaccounting.PRIMARY', Error_code: 1062; 
handler error HA_ERR_FOUND_DUPP_KEY; 
the event's source log mysql-bin.000973, end_log_pos 17372877

可以分析出是在mysql-bin.000973二进制日志文件中的17372877位置的sql在从库中执行时,出现了主键重复(表中有多个主键)。

去主库查看binlog日志,关键词上下100行

[root@db01 bin_logs]# mysqlbinlog --no-defaults -v --base64-output=decode-rows mysql-bin.000973 | grep -A 100 -B 100 17372877
略···
### INSERT INTO `dkmirel_gamemotors`.`casinoaccounting`
### SET
###   @1='GeneGNMI'
###   @2='2023-12-07 00:00:00'
###   @3='USD'
###   @4='Game24_95_v1'
###   @5=0.50000000
###   @6=0.00000000
###   @7=1
###   @8=0.50000000
###   @9=0.00000000
###   @10=0.50000000
###   @11=0.00500000
###   @12=0.00000000
###   @13=0.00000000
###   @14=0.00000000
###   @15='0'
###   @16='2023-12-07 07:00:00'
# at 17372877
略···

这里有一条insert操作导致的主键冲突,经过和开发分析原因是数据库中的事件产生的数据,从库也执行了这个事件(每隔一小时执行一次)

根本原因还是因为
从库的数据是从主库mysqldump备份过来的,备份时加了-E参数,也就是同时导出事件
导入到从库后,这条事件的状态为 ENABLED ,从库也会运行。

如果这个事件是从主库同步过来的话,从库会自动将其状态改为 SLAVESIDE_DISABLED,也就是从库侧关闭。

事件相当于一个任务计划,所以导致从库同时间也执行了,主库执行完同步到从库,从库再执行就报重复键的错误了。

这里为新做的从库,直接就reset slave; 主库备份,不导出事件,从库重新导入后配置从库。

虽然可以通过跳过或者跳过指定错误码方式恢复从库。

mysql> set global sql_slave_skip_counter=1;
mysql> start slave;

/etc/my.cnf
[mysqld]
slave-skip-errors = 1062,1032

跳过这条sql或指定的错误码,但是并不太推荐这么干,容易数据不一致。

出现过其他的错误提示:
Worker 1 failed executing transaction ‘ANONYMOUS’ at source log mysql-bin.000963, end_log_pos 28523026;
Could not execute Update_rows event on table wynnrel_secreds.loginhistory;
Can’t find record in ‘loginhistory’, Error_code: 1032;
handler error HA_ERR_KEY_NOT_FOUND;
the event’s source log mysql-bin.000963, end_log_pos 28523026

2023-12-11
发表者 Venus
mysql命令行提示mysql: [Warning] Using a password on the command line interface can be insecure问题已关闭评论

mysql命令行提示mysql: [Warning] Using a password on the command line interface can be insecure问题

在命令行下执行mysql命令,指定密码参数会提示不安全。

[root@db2 ~]# mysql -uroot -p"xxx" -e "show slave status\G;" |grep Running | grep -v State | grep Yes | wc -l
mysql: [Warning] Using a password on the command line interface can be insecure.
2

抑制此提示有2种方法;
方法1:加上 2>&1

[root@db2 ~]# mysql -uroot -p"xxx" -e "show slave status\G;" 2>&1 |grep Running | grep -v State | grep Yes | wc -l
2

方法2、使用加密登录。
使用mysql_config_editor命令生成认证文件

[root@db2 ~]# /usr/local/mysql/bin/mysql_config_editor set --login-path=local -h localhost --user=root --port=3306 --password
Enter password: 

# 指定认证信息登录

[root@db2 ~]# mysql --login-path=local
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 34
Server version: 8.0.33 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye

# 设置一个远程连接

[root@db2 ~]# /usr/local/mysql/bin/mysql_config_editor set --login-path=r247 --user=root --password --host=192.168.12.247

mysql_config_editor 会在用户家目录下生成一个.mylogin.cnf隐藏文件,并且是加密无法直接读取的。

# 打印指定登录路径

[root@db2 ~]# /usr/local/mysql/bin/mysql_config_editor print --login-path=local
[local]
user = "root"
password = *****
host = "localhost"
port = 3306

# 打印所有

[root@db2 ~]# /usr/local/mysql/bin/mysql_config_editor print --all

# mysqldump 备份使用登录路径信息。

[root@db2 ~]# mysqldump --login-path=local test > test.sql

2023-11-28
发表者 Venus
华为S5720S交换机配置策略路由已关闭评论

华为S5720S交换机配置策略路由

目的:
将内网的一台设备访问外网时通过指定的下一跳。

# 参考:
https://support.huawei.com/enterprise/zh/doc/EDOC1000069491/df424d7a

# 创建一个acl
acl 3900
rule permit ip source 192.168.12.243 0.0.0.0
quit

# 创建分类器
traffic classifier c0 operator or
if-match acl 3900
quit


# 创建行为
traffic behavior b0 
redirect ip-nexthop 192.168.12.244
quit

# 创建策略,将分类器和行为关联。
traffic policy p1
classifier c0 behavior b0
quit

# 应用到接口或者vlan
vlan 3
traffic-policy p1 inbound

此时192.168.12.243访问外网就会通过192.168.12.244这台网关出去。

2023-11-28
发表者 Venus
使用flock防止任务计划启动多次已关闭评论

使用flock防止任务计划启动多次

比如一个脚本,每隔1分钟执行一次,由于处理速度慢,导致执行时间超过1分钟,此时任务计划又启动了一个进程处理数据。
实际要求是必须单进程处理,也就是串行处理。
之前都是通过创建一个临时文件(程序运行完成后删除)判断程序是否在运行中,如果存在则不执行。

现在发现可以通过flock命令实现保证只存在一个进程。

测试脚本:

[root@localhost ~]# cat test.sh
while true
do
	date
	sleep 1
done

执行方法:

[root@localhost ~]# flock -xn /tmp/shell.lock -c "bash /root/test.sh"

-x 是排它锁
-n 是非阻塞,也就是立即返回执行失败,$? = 1,否则就会一直等待另一个执行完。
-c 是要执行的命令
/tmp/shell.lock 是锁的路径

另起一个终端测试

[root@localhost ~]# flock -xn /tmp/shell.lock -c "bash /root/test.sh"
[root@localhost ~]# echo $?
1

可以看到程序立即执行完成,返回值为1,也就是执行失败。