二进制安装
(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