Hero Image
Python 安裝模組問題

無法安裝 python-ldap python-ldap https://www.python-ldap.org/en/latest/installing.html $ pip install python-ldap In file included from Modules/LDAPObject.c:9: Modules/errors.h:8: fatal error: lber.h: No such file or directory Debian/Ubuntu: sudo apt-get install libsasl2-dev python-dev libldap2-dev libssl-dev RedHat/CentOS: sudo yum install python-devel openldap-devel sudo yum groupinstall "Development tools" molecule $ pip install molecule error: command 'gcc' failed with exit status 1 ---------------------------------------- Command "/usr/bin/python2 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-I5DGC3/psutil/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-Fy7V4X/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-I5DGC3/psutil/ RedHat/CentOS: yum -y install gcc gcc-c++ kernel-devel yum -y install python-devel libxslt-devel libffi-devel openssl-devel ansible $ pip install ansible ImportError: No module named pkg_resources yum install -y python-setuptools import pandas ModuleNotFoundError: No module named '_bz2' apt-get install -y libbz2-dev yum install -y bzip2-devel from .cv2 import * ImportError: libSM.so.6: cannot open shared object file: No such file or directory ImportError: libXrender.so.1: cannot open shared object file: No such file or directory ImportError: libXext.so.6: cannot open shared object file: No such file or directory Ubuntu: apt-get install libsm6 apt-get install libxrender1 apt-get install libxext-dev CentOS: yum install libSM yum install libXrender-devel yum install libXext

Hero Image
Percona config

# Percona Server template configuration [mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M port=3306 datadir=/data/mysql socket=/data/mysql/mysql.sock pid_file=/data/mysql/mysqld.pid # 服务端编码 character_set_server=utf8mb4 # 服务端排序 collation_server=utf8mb4_general_ci # 强制使用 utf8mb4 编码集,忽略客户端设置 skip_character_set_client_handshake=1 # 日志输出到文件 log_output=FILE # 开启常规日志输出 general_log=1 # 常规日志输出文件位置 general_log_file=/var/log/mysql/mysqld.log # 错误日志位置 log_error=/var/log/mysql/mysqld-error.log # 记录慢查询 slow_query_log=1 # 慢查询时间(大于 1s 被视为慢查询) long_query_time=1 # 慢查询日志文件位置 slow_query_log_file=/var/log/mysql/mysqld-slow.log # 临时文件位置 tmpdir=/data/mysql_tmp # 线程池缓存(refs https://my.oschina.net/realfighter/blog/363853) thread_cache_size=30 # The number of open tables for all threads.(refs https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_table_open_cache) table_open_cache=16384 # 文件描述符(此处修改不生效,请修改 systemd service 配置) # refs https://www.percona.com/blog/2017/10/12/open_files_limit-mystery/ # refs https://www.cnblogs.com/wxxjianchi/p/10370419.html #open_files_limit=65535 # 表定义缓存(5.7 以后自动调整) # refs https://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_table_definition_cache # refs http://mysql.taobao.org/monthly/2015/08/10/ #table_definition_cache=16384 sort_buffer_size=1M join_buffer_size=1M # MyiSAM 引擎专用(内部临时磁盘表可能会用) read_buffer_size=1M read_rnd_buffer_size=1M # MyiSAM 引擎专用(内部临时磁盘表可能会用) key_buffer_size=32M # MyiSAM 引擎专用(内部临时磁盘表可能会用) bulk_insert_buffer_size=16M # myisam_sort_buffer_size 与 sort_buffer_size 区别请参考(https://stackoverflow.com/questions/7871027/myisam-sort-buffer-size-vs-sort-buffer-size) myisam_sort_buffer_size=64M # 内部内存临时表大小 tmp_table_size=32M # 用户创建的 MEMORY 表最大大小(tmp_table_size 受此值影响) max_heap_table_size=32M # 开启查询缓存 query_cache_type=1 # 查询缓存大小 query_cache_size=32M # sql mode sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' ########### Network ########### # 最大连接数(该参数受到最大文件描述符影响,如果不生效请检查最大文件描述符设置) # refs https://stackoverflow.com/questions/39976756/the-max-connections-in-mysql-5-7 max_connections=1500 # mysql 堆栈内暂存的链接数量 # 当短时间内链接数量超过 max_connections 时,部分链接会存储在堆栈内,存储数量受此参数控制 back_log=256 # 最大链接错误,针对于 client 主机,超过此数量的链接错误将会导致 mysql server 针对此主机执行锁定(禁止链接 ERROR 1129 ) # 此错误计数仅在 mysql 链接握手失败才会计算,一般出现问题时都是网络故障 # refs https://www.cnblogs.com/kerrycode/p/8405862.html max_connect_errors=100000 # mysql server 允许的最大数据包大小 max_allowed_packet=64M # 交互式客户端链接超时(30分钟自动断开) interactive_timeout=1800 # 非交互式链接超时时间(10分钟) # 如果客户端有连接池,则需要协商此参数(refs https://database.51cto.com/art/201909/603519.htm) wait_timeout=600 # 跳过外部文件系统锁定 # If you run multiple servers that use the same database directory (not recommended), # each server must have external locking enabled. # refs https://dev.mysql.com/doc/refman/5.7/en/external-locking.html skip_external_locking=1 # 跳过链接的域名解析(开启此选项后 mysql 用户授权的 host 方式失效) skip_name_resolve=0 # 禁用主机名缓存,每次都会走 DNS host_cache_size=0 ini ########### REPL ########### # 开启 binlog log_bin=mysql-bin # 作为从库时,同步信息依然写入 binlog,方便此从库再作为其他从库的主库 log_slave_updates=1 # server id,默认为 ipv4 地址去除第一段 # eg: 172.16.10.11 => 161011 server_id=161011 # 每次次事务 binlog 刷新到磁盘 # refs http://liyangliang.me/posts/2014/03/innodb_flush_log_at_trx_commit-and-sync_binlog/ sync_binlog=100 # binlog 格式(refs https://zhuanlan.zhihu.com/p/33504555) binlog_format=row # binlog 自动清理时间 expire_logs_days=10 # 开启 relay-log,一般作为 slave 时开启 relay_log=mysql-replay # 主从复制时跳过 test 库 replicate_ignore_db=test # 每个 session binlog 缓存 binlog_cache_size=4M # binlog 滚动大小 max_binlog_size=1024M # GTID 相关(refs https://keithlan.github.io/2016/06/23/gtid/) #gtid_mode=1 #enforce_gtid_consistency=1 ########### InnoDB ########### # 永久表默认存储引擎 default_storage_engine=InnoDB # 系统表空间数据文件大小(初始化为 1G,并且自动增长) innodb_data_file_path=ibdata1:1G:autoextend # InnoDB 缓存池大小 # innodb_buffer_pool_size 必须等于 innodb_buffer_pool_chunk_size*innodb_buffer_pool_instances,或者是其整数倍 # refs https://dev.mysql.com/doc/refman/5.7/en/innodb-buffer-pool-resize.html # refs https://zhuanlan.zhihu.com/p/60089484 innodb_buffer_pool_size=7680M innodb_buffer_pool_instances=10 innodb_buffer_pool_chunk_size=128M # InnoDB 强制恢复(refs https://www.askmaclean.com/archives/mysql-innodb-innodb_force_recovery.html) innodb_force_recovery=0 # InnoDB buffer 预热(refs http://www.dbhelp.net/2017/01/12/mysql-innodb-buffer-pool-warmup.html) innodb_buffer_pool_dump_at_shutdown=1 innodb_buffer_pool_load_at_startup=1 # InnoDB 日志组中的日志文件数 innodb_log_files_in_group=2 # InnoDB redo 日志大小 # refs https://www.percona.com/blog/2017/10/18/chose-mysql-innodb_log_file_size/ innodb_log_file_size=256MB # 缓存还未提交的事务的缓冲区大小 innodb_log_buffer_size=16M # InnoDB 在事务提交后的日志写入频率 # refs http://liyangliang.me/posts/2014/03/innodb_flush_log_at_trx_commit-and-sync_binlog/ innodb_flush_log_at_trx_commit=2 # InnoDB DML 操作行级锁等待时间 # 超时返回 ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction # refs https://ningyu1.github.io/site/post/75-mysql-lock-wait-timeout-exceeded/ innodb_lock_wait_timeout=30 # InnoDB 行级锁超时是否回滚整个事务,默认为 OFF 仅回滚上一条语句 # 此时应用程序可以接受到错误后选择是否继续提交事务(并没有违反 ACID 原子性) # refs https://www.cnblogs.com/hustcat/archive/2012/11/18/2775487.html #innodb_rollback_on_timeout=ON # InnoDB 数据写入磁盘的方式,具体见博客文章 # refs https://www.cnblogs.com/gomysql/p/3595806.html innodb_flush_method=O_DIRECT # InnoDB 缓冲池脏页刷新百分比 # refs https://dbarobin.com/2015/08/29/mysql-optimization-under-ssd innodb_max_dirty_pages_pct=50 # InnoDB 每秒执行的写IO量 # refs https://www.centos.bz/2016/11/mysql-performance-tuning-15-config-item/#10.INNODB_IO_CAPACITY,%20INNODB_IO_CAPACITY_MAX innodb_io_capacity=500 innodb_io_capacity_max=1000 # 请求并发 InnoDB 线程数 # refs https://www.cnblogs.com/xinysu/p/6439715.html#_lab2_1_0 innodb_thread_concurrency=60 # 再使用多个 InnoDB 表空间时,允许打开的最大 ".ibd" 文件个数,不设置默认 300, # 并且取与 table_open_cache 相比较大的一个,此选项独立于 open_files_limit # refs https://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html#sysvar_innodb_open_files innodb_open_files=65535 # 每个 InnoDB 表都存储在独立的表空间(.ibd)中 innodb_file_per_table=1 # 事务级别(可重复读,会出幻读) transaction_isolation=REPEATABLE-READ # 是否在搜索和索引扫描中使用间隙锁(gap locking),不建议使用未来将删除 innodb_locks_unsafe_for_binlog=0 # InnoDB 后台清理线程数,更大的值有助于 DML 执行性能,>= 5.7.8 默认为 4 innodb_purge_threads=4

Hero Image
Trellis Ansible 錯誤的解譯器

Trellis Ansible Bad Interpreter Error Bad Interpreter Error 使用 Ansible 時遇到錯誤的解譯器問題。找不到 Python 2.7: zsh: /usr/local/bin/ansible-vault: bad interpreter: /usr/local/opt/python@2/bin/python2.7: no such file or directory 這是正常的,因為我們檢查 /usr/local/opt 後只看到 Python 3。 安裝 Python 2 brew install python@2 Python 嚴重崩潰 接著在檢查 Ansible 版本時又出現錯誤: ➜ trellis git:(master) ansible --version [1] 19153 abort ansible --version 它在 Python 2.7 上崩潰了,但理論上應該可以正常執行。我決定升級 Ansible。 sudo pip install ansible --upgrade ..... Requirement already satisfied, skipping upgrade: six>=1.4.1 in /usr/local/lib/python2.7/site-packages (from cryptography->ansible) (1.11.0) Requirement already satisfied, skipping upgrade: pycparser in /usr/local/lib/python2.7/site-packages (from cffi>=1.7; platform_python_implementation != "PyPy"->cryptography->ansible) (2.18) Installing collected packages: ansible Found existing installation: ansible 2.7.5 Uninstalling ansible-2.7.5: Successfully uninstalled ansible-2.7.5 Successfully installed ansible-2.9.1 Still I had the Python error and iTerm was showing a MacOS popup that Python was crashing unexpectedly: Python quit unexpectedly. Click Reopen to open the application again. Click Report to see more detailed information and send a report to Apple. Application Specific Information: /usr/lib/libcrypto.dylib abort() called Invalid dylib load. Clients should not load the unversioned libcrypto dylib as it does not have a stable ABI. Invalid DyLib 找到 https://stackoverflow.com/questions/58272830/python-crashing-on-macos-10-15-beta-19a582a-with-usr-lib-libcrypto-dylib 這篇,知道是動態函式庫載入錯誤,於是決定安裝 openssl。

Hero Image
用 iptables 和 ip rule 做負載均衡

用 iptables 和 ip rule 做負載均衡 操作 這裡以一台透過有線 + 無線出口連線到網際網路的 Arch Linux 裝置為例。共有兩個出口,分別使用網卡 eth0 和 eth1。大致對應關係如下: 標記 10 (0xa) - 路由表 #110 - 使用 eth0 出口 標記 11 (0xb) - 路由表 #111 - 使用 eth1 出口 我們會根據封包上的標記值判斷它應該走哪個出口。首先,使用 ip rule 為每個標記值指定一張路由表。 通常預設路由表的權重是 32768。為了讓我們的路由表生效,需要將權重調高一些(例如 31000)。 # 讓帶標記 10 (0xa) 的封包使用 110 號路由表,權重 31000 ip rule add fwmark 10 table 110 prio 31000 # 讓帶標記 11 (0xb) 的封包使用 111 號路由表,權重 31000 ip rule add fwmark 11 table 111 prio 31000 # 如果你的連線更多,可以繼續新增標記 <-> 路由表的對應關係 # #110 路由表的路由 ip route add 10.20.0.0/24 dev eth0 table 110 ip route add default via 10.20.0.254 table 110 # #111 路由表的路由 ip route add 10.25.0.0/24 dev eth1 table 111 ip route add default via 10.25.0.254 table 111 # 如果這條連線已經被標記,將標記設定到封包上 iptables -t mangle -A OUTPUT -j CONNMARK --restore-mark # 如果封包已經有標記,直接放行 iptables -t mangle -A OUTPUT -m mark ! --mark 0 -j ACCEPT # 如果封包沒有被標記 # 把封包標記為 10 (0xa) iptables -t mangle -A OUTPUT -j MARK --set-mark 10 # 每 2 個封包就把一個封包標記為 11 (0xb) iptables -t mangle -A OUTPUT -m statistic --mode nth --every 2 --packet 0 -j MARK --set-mark 11 # 如果你有三條出口,這裡可以類似於 # iptables -t mangle -A OUTPUT -j MARK --set-mark 10 # iptables -t mangle -A OUTPUT -m statistic --mode nth --every 3 --packet 0 -j MARK --set-mark 11 # iptables -t mangle -A OUTPUT -m statistic --mode nth --every 3 --packet 1 -j MARK --set-mark 12 # 把封包的標記儲存到整條連線上,讓整個連線使用同一個出口 iptables -t mangle -A OUTPUT -j CONNMARK --save-mark # 讓封包的出口與我們選擇的一致 iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE 之後可以用 iptables -L OUTPUT -t mangle 看一下規則是否正確,再用 Wireshark 驗證連線是否真的分流。