二進位制安裝
(CentOS7 + MariaDB 10.2.8)
安裝前準備
1. 關閉NUMA
numctl -interleave=all /path/mysqdd --defaults-file=/path/my.cnf
https://access.redhat.com/solutions/23216
2. 檢查NUMA
numactl --show
numactl --hardware
限制設定
ulimit -a檢視
主要檢視open files,max user processes的限制設定。
open files系統開啟檔案過多,會報 OS error code 24: Too many open files的錯誤。
直接修改/etc/security/limits.conf檔案,需重啟OS生效。
vim /etc/security/limits.conf
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
3. Swap
原則:要麼不分配,要麼最多分配4GB
檢視:
sysctl -a | grep vm.swappiness
臨時性修改(等號兩邊沒有空格):
sysctl vm.swappiness=0
sysctl -p
永久修改(需重啟OS生效):
vim /etc/sysctl.conf
vm.swappiness=0
如果沒有,在最後新增一行。
4. CPU
關閉硬體的節能模式,開啟高效能模式
5. 檔案系統
建議XFS
mysql資料庫所在/data目錄單獨一個掛載分割槽
6. SELinux & iptables
臨時性關閉SELinux
setenforce 0
永久關閉
vim /etc/sysconfig/selinux
SELINUX=disabled
臨時性關閉iptables
service iptables stop
永久關閉
chkconfig --del iptables
如果是CentOS 7下關閉firewall
systemctl stop firewall
systemctl disable firewall
7. IO排程
raid
正式安裝
1. 建立賬號
groupadd mysql
執行的mysql賬號不能登入linux
useradd -g mysql -d /usr/local/mysql -s /sbin/nologin -M mysql
id mysql
2. 基本軟體安裝
mkdir -p /opt/mysql
cd /opt/mysql
wget https://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-10.2.8/bintar-linux-x86_64/mariadb-10.2.8-linux-x86_64.tar.gz
tar -zxvf mariadb-10.2.8-linux-x86_64.tar.gz
cd /usr/local/
ln -s /opt/mysql/mariadb-10.2.8-linux-x86_64 mysql
chown -R mysql:mysql mysql mysql/
3. 資料庫相關的目錄
mkdir -p /data/mysql/mysql3306/{data,log,tmp}
cd /data/
chown -R mysql:mysql mysql
配置檔案:/etc/my.cnf
以下為4G記憶體,4核邏輯CPU的my.cnf參考,需要根據生產實際情況而調整:
[client]
port = 3306
socket = /data/mysql/mysql3306/tmp/mysql3306.sock
[mysql]
auto-rehash
prompt = "\u@\h [\d]"
default-character-set = utf8mb4
[mysqld]
user = mysql
port = 3306
socket = /data/mysql/mysql3306/tmp/mysql3306.sock
basedir = /usr/local/mysql
datadir = /data/mysql/mysql3306/data
tmpdir = /data/mysql/mysql3306/tmp
skip_name_resolve = 1
#character set
character-set-server = utf8mb4
open_files_limit = 65535
max_connections = 200
max_connect_errors = 100000
#logs
log-error = /data/mysql/mysql3306/log/error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql3306/log/slow.log
#binlog
log-bin = /data/mysql/mysql3306/log/mysql-bin
binlog_format = row
binlog_cache_size = 4M
max_binlog_cache_size = 1000M
max_binlog_size = 500M
sync_binlog = 1
expire_logs_days = 7
#replication
#server-id format: port of mysql + last two segments of ip
server-id = 3306137244
relay_log = /data/mysql/mysql3306/log/relay-bin
log_slave_updates = 1
#innodb
innodb_data_file_path = ibdata1:1G:autoextend
innodb_buffer_pool_size = 2560M
innodb_buffer_pool_instances = 8
innodb_flush_log_at_trx_commit = 1
innodb_flush_method = O_DIRECT
innodb_file_per_table = 1
innodb_io_capacity = 2000
innodb_max_dirty_pages_pct = 50
innodb_log_file_size = 1G
innodb_log_buffer_size = 32M
innodb_rollback_on_timeout = 1
innodb_status_file = 1
#transaction_isolation = READ-COMMITTED
#buffer & cache
table_open_cache = 2048
table_definition_cache = 2048
max_heap_table_size = 32M
sort_buffer_size = 4M
join_buffer_size = 4M
thread_cache_size = 100
query_cache_size = 0
query_cache_type = 0
query_cache_min_res_unit = 512
thread_stack = 512K
tmp_table_size = 96M
key_buffer_size = 32M
read_buffer_size = 8M
read_rnd_buffer_size = 8M
bulk_insert_buffer_size = 64M
#time
default-time-zone = `+08:00`
[mysqldump]
quick
max_allowed_packet = 128M
[myisamchk]
key_buffer_size = 16M
sort_buffer_size = 32M
4. 初始化
cd /usr/local/mysql
./scripts/mysql_install_db --user=mysql
如果不是配置檔案不是預設目錄,指定一下。(–defaults-file=/etc/my.cnf必須是第一個引數)
./scripts/mysql_install_db --defaults-file=/etc/my3307.cnf --user=mysql
注意:一定要用相對路徑來安裝,否則會報:FATAL ERROR: Could not find ./bin/my_print_defaults的錯誤。
5. 啟動
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
/etc/init.d/mysql start
或
service mysql start
如果要加入系統自動啟動(生產環境一般不會設定自動啟動)
chkconfig --add mysql
官網有提到使用以下啟動方式: (https://mariadb.com/kb/en/the-mariadb-library/installing-mariadb-binary-tarballs)
/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf &
不能使用service msyql stop的方法來關閉,可使用/etc/init.d/mysql stop或mysqladmin -uroot -p shutdown或kill 程式來關閉(KILL的方式不推薦)
6. 連線mysql
如果在shell下輸入mysql顯示command not found 是環境變數沒有設定好。
echo `export PATH=$PATH:/usr/local/mysql/bin` >>/etc/profile
source /etc/profile
在本機使用socket檔案連線
mysql -S /data/mysql/mysql3306/tmp/mysql3306.sock -uroot -p
使用TCP/IP連線
mysql -uroot -p -h127.0.0.1
- 安全加固
ALTER USER `root`@`localhost` IDENTIFIED BY `my_password`;
DELETE FROM mysql.user WHERE user = ``;
FLUSH PRIVILEGES;
DROP DATABASE IF EXISTS `test`;
- 關閉mysql
/etc/init.d/mysql stop
或
service mysql stop