JiaHe's Blog

读万卷书,行万里路

Linux安装rpcapd

可以访问https://www.tcpdump.org/index.html#latest-releases,下载最新libpcap

# yum安装
yum install -y gcc gcc-c++ glibc-static flex
# dnf安装
dnf install -y gcc gcc-c++ flex
dnf --enablerepo=crb install glibc-static

wget https://www.tcpdump.org/release/libpcap-1.10.4.tar.gz

# 自v1.9.0版本开始,自带rpcapd,无需另外安装
tar -zxf libpcap-1.10.4.tar.gz
cd libpcap-1.10.4
./configurate --enable-remote && make

# 启动rpcapd
./rpcapd/rpcapd -4 -n

Windows安装wireshark

  1. 从官网下载安装https://www.wireshark.org/download.html
  2. 使用scoop安装,scoop install extras/wireshark

配置wireshark

顶部菜单栏 -> 捕获 -> 选项 -> 下方的Manage Interfaces -> 管理接口窗口

16958648541221695864853244.png

16958649011271695864900426.png

16958649351181695864934627.png

点击远程接口 -> 点击左下角加号➕

16958649701211695864969318.png

主机:服务器IP

端口:默认为2002,如果指定-p参数则自行修改

16958652601221695865260092.png

通过tcp port 端口,可以指定监听具体端口的请求数据。

最终效果大致如下
16958653171261695865316230.png

环境说明:

1、企业内网需求:企业内部许多服务器是不能连接互联网,但每台centos主机安装软件包时都配置本地yum源很麻烦,也缺少第三方yum源,因此在内网搭建一台yum源服务器满足这一需求,同时网络层面策略只允许此yum源服务器每周日晚限时联网同步阿里云的yum源。

2、准备两台测试主机,一台用作yum源服务器(能连互联网),一台用作客户端。

3、系统版本:CentOS 7.9

阅读全文 »

tee /etc/yum.repos.d/mysql-community.repo<<EOF
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/\$basearch/
enabled=1
gpgcheck=0

[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/\$basearch/
enabled=1
gpgcheck=0

[mysql-tools-community]
name=MySQL Tools Community
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/\$basearch/
enabled=1
gpgcheck=0
EOF

dnf config-manager --disable mysql80-community
dnf config-manager --enable mysql57-community

dnf install -y libncurses* ncurses-devel
dnf --enablerepo=devel install -y ncurses-compat-libs
dnf install -y mysql-community-server
grep 'A temporary password' /var/log/mysqld.log |tail -1
# 2023-06-28T08:33:42.179329Z 1 [Note] A temporary password is generated for root@localhost: gkNH%eAMe6a?

# 修改密码,方式1
mysqladmin -uroot -pgkNH%eAMe6a? password 新密码

# 连接之后第一件事是修改密码,方式2
mysql -uroot -pgkNH%eAMe6a?
alter user 'root'@'localhost' identified by '新密码';

# 设置弱口令,修改完后只验证密码长度
set global validate_password_policy=0;
# 修改密码长度策略
set global validate_password_length=1;

# 更改用户的访问权限(%代表任何地址都可以访问)
use mysql;
update user set host = '%' where user = 'root';
flush privileges;

  • default_storage_engine:设置默认使用的存储引擎

    • 默认值为 INNODB
  • max_allowed_packet:客户端和服务器之间发送的数据包的最大大小限制

    • 默认值为 4M
    • 推荐值为 64M - 1G,具体取决于服务器的可用内存和需要处理的数据大小。
  • join_buffer_size:连接操作缓存大小

    • 默认值为 262144(即 256K)
    • 推荐值为 128M - 512M,具体取决于系统的总内存量和查询表的大小。
  • tmp_table_size:临时表大小

    • 默认值为 16777216(即 16M)
    • 推荐值为 1G 或大于最大单个表所需的空间。
  • interactive_timeout:交互式会话超时时间,单位为秒

    • 默认值为 28800(秒),推荐值根据应用程序的需求而定。
  • wait_timeout:非交互式会话超时时间,单位为秒

    • 默认值为 28800(秒),推荐值根据应用程序的需求而定。
  • read_buffer_size:读取数据时缓存的大小

    • 默认值为 131072(即 128K)
    • 推荐值为 128K - 2M,具体取决于系统内存大小和 I/O 负载。
  • read_rnd_buffer_size:随机读取数据时缓存的大小

    • 默认值为 262144(即 256K)
    • 推荐值为 256K - 2M,具体取决于系统内存大小和 I/O 负载。
  • sort_buffer_size:排序操作缓存大小

    • 默认值为 262144(即 256K)
    • 推荐值为 1M - 256M,具体取决于系统内存大小和排序操作的复杂程度。
  • key_buffer_size:索引缓存大小

    • 默认值为 8388608(即 8M)
    • 推荐值为 64M - 1G,具体取决于系统内存和索引大小。
  • back_log:等待处理连接请求的队列大小

    • 默认值为 -1(表示自动调整大小)
    • 推荐值为 100 - 1000,具体取决于并发连接数和所需的响应时间。
  • flush_time:将更新写入磁盘的间隔时间,单位为秒

    • 默认值为 0,推荐值根据应用程序的需求而定。
  • open_files_limit:同时打开的文件描述符数量

    • 默认值为 5000,建议将其设置为操作系统允许的最大值。
  • table_definition_cache:表定义缓存大小

    • 默认值为 -1(表示自动调整大小)
    • 推荐值为 2000 - 4000,具体取决于表的数量和大小。
  • max_connections:最大连接数

    • 默认值为 151
    • 推荐值为 500 - 5000,具体取决于系统硬件和应用程序的分布式程度。
  • max_connect_errors:客户端连续错误次数达到此值后,会被禁止进一步连接

    • 默认值为 100,更具安全要求设置次数。
  • thread_cache_size:线程缓存大小

    • 默认值为 -1(表示自动调整大小)
    • 推荐值为 100 - 1000,具体取决于系统中的并发连接数。
  • innodb_flush_log_at_trx_commit:InnoDB存储引擎日志写入磁盘的时机

    • 默认值为 1
    • 推荐值为 2 或 0,如果需要更好的性能,则可以将其设置为 2,而不是 1。
  • innodb_buffer_pool_size:InnoDB存储引擎缓冲池大小

    • 默认值为 128M
    • 推荐值为系统内存的 50% - 80%。
  • innodb_log_file_size:InnoDB存储引擎日志文件大小

    • 默认值为 48M
    • 推荐值为 256M - 2G,具体取决于系统 I/O 负载和事务吞吐量。
  • innodb_log_buffer_size:InnoDB存储引擎日志缓存大小

    • 默认值为 16777216(即 16M)
    • 建议将其设置为 innodb_log_file_size 的 25%。
  • innodb_thread_concurrency:InnoDB存储引擎线程并发度

    • 默认值为 0
    • 推荐值为 8 - 64,具体取决于应用程序的并发性质。
  • innodb_autoextend_increment:InnoDB存储引擎自动扩展增量

    • 默认值为 64M
    • 建议将其设置为 128M - 1G。
  • innodb_buffer_pool_instances:InnoDB存储引擎缓冲池实例数

    • 默认值为 1
    • 推荐值为 4 - 12,具体取决于系统内存的大小和 CPU 核心数量。
  • innodb_concurrency_tickets:InnoDB存储引擎并发票数

    • 默认值为 5000
    • 推荐值为 1000 - 10000,具体取决于应用程序的并发程度。
  • innodb_old_blocks_time:InnoDB存储引擎旧数据块保留时间,单位为毫秒

    • 默认值为 1000,推荐值根据应用程序的需求而定。
  • innodb_open_files:InnoDB存储引擎打开文件数

    • 默认值为 -1(表示自动调整大小)
    • 推荐值取决于您的系统负载和可用内存大小。一般来说,建议将innodb_open_files设置为足够大的值,以便InnoDB存储引擎能够同时处理多个表和索引文件。
  • innodb_stats_on_metadata:InnoDB存储引擎在获取表元数据时是否收集统计信息

    • 默认值为1(开启)
    • 对于大型数据库,建议将该参数设置为0(关闭),以减少在执行DDL操作时的开销。
  • innodb_file_per_table:是否为每个InnoDB表创建单独的数据文件

    • 推荐值为1
  • innodb_checksum_algorithm:InnoDB存储引擎数据校验算法

    • 默认值crc32
  • innodb_lock_wait_timeout:当一个事务等待锁定资源的时间超过此值时,将回滚事务。

    • 默认值为50秒
  • query_cache_size:查询缓存大小

    • 默认值为 0
    • 建议设置为0,MySQL缓存命中率不高,启用反而浪费时间
  • slow_query_log:是否记录慢查询日志

    • 默认值为 0
    • 建议将其设置为 1,以帮助诊断缓慢查询。
  • long_query_time:指定慢查询的时间阈值,单位为秒

    • 默认值为 10(秒),推荐值根据应用程序的需求而定。
  • lower_case_table_names:处理表名和数据库名时是否区分大小写

    • 默认值为 0
    • 推荐值为 1,以避免表名大小写不敏感导致的冲突问题。
  • table_open_cache:可以同时打开多少个表

    • 默认值为 2000
    • 推荐值为 4000 - 8000,具体取决于表的数量和大小。
  • autocommit:是否自动提交事务

    • 默认值为 1,推荐值根据应用程序的需求而定。
  • skip_name_resolve:是否对连接的客户端进行DNS解析

    • 默认值为 0
    • 推荐值为 1,以避免 DNS 查询的性能影响。
  • transaction_isolation:事务的隔离级别

    • 默认值为 REPEATABLE-READ
    • 推荐值为 READ-COMMITTED,以提高并发性能。
  • explicit_defaults_for_timestamp:如何处理TIMESTAMP列的默认值

    • 默认值为 0
    • 建议将其设置为 1,以防止时间戳列的插入错误。
  • symbolic-links:是否允许使用符号链接

    • 默认值为 1
    • 建议将其设置为 0,以提高安全性。

参考

/**
* Java实现DFA算法进行敏感词过滤
*/
public class SensitiveWordUtil {
private final TrieNode root;

public SensitiveWordUtil() {
root = new TrieNode();
}

/**
* 添加敏感词
*
* @param word 词
*/
public void addWord(String word) {
TrieNode node = root;
for (char ch : word.toCharArray()) {
node = node.getChildren().computeIfAbsent(ch, c -> new TrieNode());
}
node.setEndOfWord(true);
}

/**
* 是否在敏感词库中
*
* @param word 词
*/
public boolean containsWord(String word) {
TrieNode node = root;
for (char ch : word.toCharArray()) {
node = node.getChildren().get(ch);
if (node == null) {
return false;
}
}
return node.isEndOfWord();
}

/**
* 是否存在敏感词
*
* @param text 文本
*/
public boolean containsSensitiveWord(String text) {
for (int i = 0; i < text.length(); i++) {
if (containsWordStartingFromIndex(text, i)) {
return true;
}
}
return false;
}

private boolean containsWordStartingFromIndex(String text, int startIndex) {
TrieNode node = root;
for (int i = startIndex; i < text.length(); i++) {
char ch = text.charAt(i);
node = node.getChildren().get(ch);
if (node == null) {
return false;
}
if (node.isEndOfWord()) {
return true;
}
}
return false;
}

/**
* 将敏感词替换为*
*
* @param text 文本
*/
public String filterWords(String text) {
StringBuilder filteredText = new StringBuilder();
StringBuilder word = new StringBuilder();
TrieNode node = root;

for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
node = node.getChildren().get(ch);

if (node != null) {
word.append(ch);
if (node.isEndOfWord()) {
char[] chars = new char[word.length()];
Arrays.fill(chars, '*');
filteredText.append(new String(chars));
word.setLength(0);
node = root;
}
} else {
filteredText.append(text.charAt(i));
node = root;
word.setLength(0);
}
}

filteredText.append(word);
return filteredText.toString();
}

public List<String> findSensitiveWords(String text) {
List<String> sensitiveWords = new ArrayList<>();
for (int i = 0; i < text.length(); i++) {
int wordEndIndex = findWordEndIndex(text, i);
if (wordEndIndex != -1) {
String word = text.substring(i, wordEndIndex + 1);
if (containsWord(word)) {
sensitiveWords.add(word);
}
i = wordEndIndex;
}
}
return sensitiveWords;
}

private int findWordEndIndex(String text, int startIndex) {
TrieNode node = root;
int endIndex = -1;
for (int i = startIndex; i < text.length(); i++) {
char ch = text.charAt(i);
node = node.getChildren().get(ch);
if (node == null) {
break;
}
if (node.isEndOfWord()) {
endIndex = i;
}
}
return endIndex;
}

private static class TrieNode {
private final Map<Character, TrieNode> children;
private boolean endOfWord;

public TrieNode() {
children = new HashMap<>();
}

public Map<Character, TrieNode> getChildren() {
return children;
}

public boolean isEndOfWord() {
return endOfWord;
}

public void setEndOfWord(boolean endOfWord) {
this.endOfWord = endOfWord;
}
}

public static void main(String[] args) throws IOException {
SensitiveWordUtil filter = new SensitiveWordUtil();
filter.addWord("敏感词1");
filter.addWord("敏感词2");
filter.addWord("敏感词3");

String text = "这是一个包含敏感词1的测试文本,敏感词2也在其中。";

String filteredText = filter.filterWords(text);
System.out.println("修正后的内容:" + filteredText);

List<String> words = filter.findSensitiveWords(text);
System.out.println("包含以下敏感词:" + words);

double asDouble = Arrays.stream(new int[10000]).mapToLong(o -> {
long dfaStartTime = System.nanoTime();
filter.containsSensitiveWord(text);
return (System.nanoTime() - dfaStartTime);
}).average().getAsDouble();
System.out.println("DFA算法耗时:" + asDouble + "纳秒");

// 使用正则表达式查找敏感词
double asDouble1 = Arrays.stream(new int[10000]).mapToLong(o -> {
long regexStartTime = System.nanoTime();
boolean regexResult = text.matches(".*(敏感词1|敏感词2|敏感词3).*");
return (System.nanoTime() - regexStartTime);
}).average().getAsDouble();
System.out.println("正则表达式耗时:" + asDouble1 + "纳秒");

System.out.println(asDouble1 / asDouble);

}
}