1、模拟搭建openv-p-n
模拟环境说明:
环境条件:centos7 + Vmware平台,局域网模拟(172.30.0.0./24),外网网段(10.0.0.0/24),v-p-n分配网段:10.8.0.0/24。
客户端:windows宿主机
openv-p-n服务端:网卡eth0(NAT模式,地址10.0.0.171),网卡eth1(局域网lan模式,地址172.30.0.1),兼模拟防火墙,仅开放ping,ssh,openvpn 1194端口
服务器1/2:模拟内网服务器(172.30.0.100/200)
软件安装
[root@centos2 ~]#yum -y install openvpn
[root@centos2 ~]#yum -y install easy-rsa
准备相关配置文件
#服务器配置文件
cp -a /usr/share/doc/openvpn-2.4.11/sample/sample-config-files/server.conf /etc/openvpn/
#证书签发文件
cp -a /usr/share/easy-rsa/ /etc/openvpn/easy-rsa-server
#证书签发相关变量的配置文件,可修改证书有效期等参数
cp -a /usr/share/doc/easy-rsa-3.0.8/vars.example /etc/openvpn/easy-rsa-server/3/vars
准备证书相关文件
-- 详细输出过程省略
[root@centos2 3]# cd /etc/openvpn/easy-rsa-server/3/
[root@centos2 3]# file easyrsa
easyrsa: POSIX shell script, ASCII text executable
[root@centos2 3]# ./easyrsa init-pki #初始化pki工作目录
[root@centos2 3]# ./easyrsa build-ca nopass #创建CA证书
服务器端证书申请文件创建
[root@centos2 3]# ./easyrsa gen-req server nopass
req: /etc/openvpn/easy-rsa-server/3/pki/reqs/server.req
key: /etc/openvpn/easy-rsa-server/3/pki/private/server.key
#生成req请求文件和key私钥文件
签发服务器端证书
[root@centos2 3]# ./easyrsa sign-req server server
Certificate created at: /etc/openvpn/easy-rsa-server/3/pki/issued/server.crt
[root@centos2 3]#
创建 Diffie-Hellman 密钥
--用于通信加密
[root@centos2 3]# ./easyrsa gen-dh
DH parameters of size 2048 created at /etc/openvpn/easy-rsa-server/3/pki/dh.pem
初始化客户端证书目录
[root@centos2 ~]#cp -r /usr/share/easy-rsa/ /etc/openvpn/easy-rsa-client
[root@centos2 ~]#cd /etc/openvpn/easy-rsa-client/3
#生成证书申请所需目录pki和文件
[root@centos2 3]# ./easyrsa init-pki
客户端证书申请文件创建
[root@centos2 3]# ./easyrsa gen-req thomas nopass
Keypair and certificate request completed. Your files are:
req: /etc/openvpn/easy-rsa-client/3/pki/reqs/thomas.req #证书请求文件
key: /etc/openvpn/easy-rsa-client/3/pki/private/thomas.key #私钥文件
签发client证书
[root@centos2 3]# ./easyrsa import-req /etc/openvpn/easy-rsa-client/3/pki/reqs/thomas.req thomas
#导入证书
[root@centos2 3]# ./easyrsa sign-req client thomas
#签发客户端证书
创建专有服务端通信证书目录
[root@centos2 3]# mkdir /etc/openvpn/certs
#将之前创建服务端证书复制到该目录下
[root@centos2 3]# ll /etc/openvpn/certs/
total 20
-rw-------. 1 root root 1172 Feb 12 13:30 ca.crt
-rw-------. 1 root root 424 Feb 12 13:31 dh.pem
-rw-------. 1 root root 4545 Feb 12 13:31 server.crt
-rw-------. 1 root root 1704 Feb 12 13:31 server.key
[root@centos2 3]#
创建保存客户端通信证书目录
[root@centos2 3]# mkdir /etc/openvpn/client/thomas
[root@centos2 3]# ls /etc/openvpn/client/thomas/
ca.crt thomas.crt thomas.key
准备openv-p-n服务配置文件 /etc/openvpn/server.conf
[root@centos2 openvpn]# vim server.conf
[root@centos2 openvpn]# pwd
/etc/openvpn
[root@centos2 openvpn]# grep '^[a-z].*' server.conf
port 1194
proto tcp
dev tun
ca /etc/openvpn/certs/ca.crt
cert /etc/openvpn/certs/server.crt
key /etc/openvpn/certs/server.key
dh /etc/openvpn/certs/dh.pem
server 10.8.0.0 255.255.255.0
push "route 172.30.0.0 255.255.255.0"
keepalive 10 120
cipher AES-256-CBC
compress lz4-v2
push "compress lz4-v2"
max-clients 2048
user openvpn
group openvpn
status /var/log/openvpn/openvpn-status.log
log-append /var/log/openvpn/openvpn.log
verb 3
mute 20
[root@centos2 openvpn]#
#准备日志相关目录
[root@centos2 openvpn]# getent passwd openvpn
openvpn:x:997:994:OpenVPN:/etc/openvpn:/sbin/nologin
[root@centos2 openvpn]# mkdir /var/log/
anaconda/ audit/ chrony/ rhsm/ tuned/
[root@centos2 openvpn]# mkdir /var/log/openvpn
[root@centos2 openvpn]# chown openvpn:openvpn /var/log/openvpn
[root@centos2 openvpn]# ll -d /var/log/openvpn/
drwxr-xr-x. 2 openvpn openvpn 6 Feb 12 13:59 /var/log/openvpn/
[root@centos2 openvpn]#
开启内核ip_forward转发参数和防火墙SNAT规则添加
[root@centos2 openvpn]#echo net.ipv4.ip_forward = 1 >> /etc/sysctl.conf
[root@centos2 openvpn]# sysctl -a |grep ipv4.ip_forward
net.ipv4.ip_forward = 1
#添加SNAT规则
[root@centos2 openvpn]# iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j MASQUERADE
启动server
[root@centos2 openvpn]# systemctl enable --now openvpn@server
[root@centos2 openvpn]# ss -antp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 32 *:1194 *:*
users:(("openvpn",pid=30935,fd=5))
客户端配置文件
[root@centos2 openvpn]# vim /etc/openvpn/client/thomas/client.ovpn
client
dev tun
proto tcp
remote 10.0.0.171 1194 #生产中为OpenVPN公网IP
resolv-retry infinite
nobind
#persist-key
#persist-tun
ca ca.crt
cert wangxiaochun.crt
key wangxiaochun.key
remote-cert-tls server
#tls-auth ta.key 1
cipher AES-256-CBC
verb 3 #此值不能随意指定,否则无法通信
compress lz4-v2 #此项在OpenVPN2.4.X版本使用,需要和服务器端保持一致,如不指定,默认使用comp-lz压缩
#auth-nocache 建议加上可避免客户端缓存密码
客户端安装
#下载client安装至宿主机Windows,进入安装路径的config目录下,打包如下证书复制到该目录下面
[root@centos2 thomas]# tar -cvf thomas.tar ./*
./ca.crt
./client.ovpn
./thomas.crt
./thomas.key
客户端启动和验证
PS C:\Users\Administrator\Desktop> ping 172.30.0.100
Pinging 172.30.0.100 with 32 bytes of data:
Reply from 172.30.0.100: bytes=32 time=1ms TTL=63
Reply from 172.30.0.100: bytes=32 time=2ms TTL=63
Reply from 172.30.0.100: bytes=32 time=2ms TTL=63
Reply from 172.30.0.100: bytes=32 time=2ms TTL=63
Ping statistics for 172.30.0.100:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 1ms, Maximum = 2ms, Average = 1ms
PS C:\Users\Administrator\Desktop>
PS C:\Users\Administrator\Desktop> curl 172.30.0.100
StatusCode : 200
StatusDescription : OK
Content : hello world
RawContent : HTTP/1.1 200 OK
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Accept-Ranges: bytes
Content-Length: 12
Content-Type: text/html; charset=UTF-8
Date: Sat, 12 Feb 2022 06:45:05 GMT
ETag: "c...
Forms : {}
Headers : {[Keep-Alive, timeout=5, max=100], [Connection, Keep-Alive], [Accept-Ranges, bytes], [Content-Lengt
h, 12]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : mshtml.HTMLDocumentClass
RawContentLength : 12
2、通过编译、二进制安装MySQL5.7
二进制安装MySQL5.7版本
1 安装依赖包
yum -y install libaio numactl-libs
2 创建用户和组 mysql
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
3 准备安装程序
官网下载地址:https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.36-linux-glibc2.12-i686.tar.gz
tar -zxvf /data/mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
cd /usr/local/
ln -sv mysql-5.7.36-linux-glibc2.12-x86_64 mysql
chown -R root.root /usr/local/mysql
4 配置环境变量
echo 'PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql-env.sh
. /etc/profile.d/mysql-env.sh
5 准备数据库配置文件 /etc/my.cnf
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
expire_logs_days=7
character-set-server=utf8
#recommend character-set-server=utf8mb4 in mysql80
lower_case_table_names=1
[mysql]
prompt=[\\u@\\h][\\d]>\\_
[client]
socket=/data/mysql/mysql.sock
6 初始化数据库并记录临时密码
mysqld --initialize --user=mysql --datadir=/data/mysql
[root@cent70 mysql]# grep password /data/mysql/mysql.log
2022-02-14T13:58:43.689885Z 1 [Note] A temporary password is generated for root@localhost: sQJrOYI7i/is
7 准备服务启动脚本
文件目录
cd /etc/systemd/system/
#or /usr/lib/systemd/system/
vim mysqld.service
文件内容
[Unit]
Description=MySQL Community Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
Type=forking
User=mysql
Group=mysql
#PIDFile=/var/run/mysqld/mysqld.pid
PIDFile=/data/mysql/mysql.pid
TimeoutSec=0
PermissionsStartOnly=true
#ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/data/mysql/mysql.pid $MYSQLD_OPTS
# Use this to switch malloc implementation
#EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
启动服务
systemctl daemon-reload
systemctl enable --now mysqld
登录mysql
[root@cent70 mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.36 Source distribution
Copyright (c) 2000, 2021, 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.
[root@localhost][(none)]>
[root@localhost][(none)]>
[root@localhost][(none)]>
[root@localhost][(none)]>
基于centos7 源码编译安装mysql5.7
1 安装依赖包
yum -y install gcc gcc-c++ cmake bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel perl-Data-Dumper libtirpc-devel
2 创建用户和组
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
3 准备数据目录
mkdir /data/mysql
chown mysql.mysql /data/mysql
4 上传下载的包并解压缩
tar -zxvf /data/mysql-5.7.36.tar.gz -C /usr/local/src
5 编译安装MySQL
cd /usr/local/src/mysql-5.7.36/
cmake . \
-DCMAKE_INSTALL_PREFIX=/apps/mysql \
-DMYSQL_DATADIR=/data/mysql/ \
-DSYSCONFDIR=/etc/ \
-DMYSQL_USER=mysql \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_BOOST=/usr/local/boost
make && make install
cmake过程出现的报错及处理
错误信息:
-- Mysql currently requires boost_1_59_0
CMake Error at cmake/boost.cmake:88 (MESSAGE):
处理建议:
1 下载解压
https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
2 解压缩至指定路径
mkdir -p /usr/local/boost
tar -zxvf boost_1_59_0.tar.gz -C /usr/local/boost
3 添加cmake参数设置
-DWITH_BOOST=/usr/local/boost
6 环境变量添加
echo 'PATH=$PATH:/apps/mysql/bin' > /etc/profile.d/mysql-env.sh
. /etc/profile.d/mysql-env.sh
7 编辑数据库配置文件/etc/my.cnf并初始化
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
expire_logs_days=7
character-set-server=utf8
#character-set-server=utf8mb4 in mysql80
lower_case_table_names=1
character_set_server=utf8
[client]
socket=/data/mysql/mysql.sock
default-character-set = utf8
初始化,同二进制安装方式一致
mysqld --initialize --user=mysql --datadir=/data/mysql
8 准备启动脚本
cp /apps/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
3、二进制安装mariadb10.4
1 安装依赖包
yum -y install libaio numactl-libs
2 创建用户和组 mysql
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
3 准备程序文件
tar -zxvf mariadb-10.4.22-linux-systemd-x86_64.tar.gz -C /usr/local/
cd /usr/local/
ln -sv mariadb-10.4.22-linux-systemd-x86_64 mysql
chown -R root.root /usr/local/mysql
4 准备环境变量
echo 'PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql-env.sh
. /etc/profile.d/mysql-env.sh
5 准备配置文件/etc/my.cnf 并初始化数据库
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
expire_logs_days=7
character-set-server=utf8mb4
lower_case_table_names=1
[client]
socket=/data/mysql/mysql.sock
初始化数据库
cd /usr/local/mysql
./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
6 准备服务启动脚本,开启服务
cp support-files/systemd/mariadb.service /usr/lib/systemd/system/
#启动服务
systemctl daemon-reload
systemctl enable --now mariadb
7 修改口令
#默认空密码登录
ALTER USER root@localhost IDENTIFIED VIA mysql_native_password USING PASSWORD("admin");
#centos里面通过上面的修改后就限制root无法空密码登录,然后执行 mysql_secure_installation 来初始化安全设置,如果未设置root密码先执行mysql_secure_installation脚本,centos里面仍可以空密码登录
mysql_secure_installation