MySQL DBA 001

Install

1.1 环境优化

  1. 关闭selinux和iptables

  2. I/O调度系统建议调整到deadline模式,cat /sys/block/sda/queue/scheduler

  3. swappiness分区设置cat /proc/sys/vm/swappiness

  4. 文件系统建议悬着xfs,xfs支持动态扩容

  5. 操作系统限制

    nproc: 是操作系统级别对每个用户创建的进程数的限制

    nofile: 每个进程可以打开的文件数的限制

    cat /etc/security/limits.conf << EOF
    * soft nproc 65535
    * hard nproc 65535
    * soft nofile 65535
    * soft nofile 65535
    EOF
    
  6. 关闭numa

    numa --interleave=all /usr/local/mysql/bin/mysqld_safe --default-file=/etc/my.cnf &
    

1.2 MySQL 5.6 安装

三部曲,一步走

# 一部曲
groupadd mysql
useradd -g mysql mysql -s /sbin/nologin
cd /usr/local
tar xzvf mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
ln -s mysql-5.6.47-linux-glibc2.12-x86_64 mysql
chown -R mysql:mysql mysql
# 二部曲
mkdir -p /data/mysql
chown -R mysql:mysql /data
# 三部曲
cat > /etc/my.cnf << EOF
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb
[mysqld]
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql
open_files_limit = 65535
back_log = 103
max_connections = 512
max_connect_errors = 100000
table_open_cache = 512
external-locking = FALSE
max_allowed_packet = 128M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 51
query_cache_size = 32M 
tmp_table_size = 96M
max_heap_table_size = 96M
slow_query_log = 1 
slow_query_log_file = /data/mysql/slow.log
log-error = /data/mysql/error.log
long_query_time = 0.5
server-id = 1323306
log-bin =/data/mysql/mysql-bin
sync_binlog =1
binlog_cache_size = 4M
max_binlog_cache_size = 128M
max_binlog_size = 1024M
expire_logs_days = 7 
key_buffer_size = 32M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
character-set-server = utf8mb4
default-storage-engine=InnoDB
binlog_format=row 
#gtid_mode=on 
#log_slave_updates=l
#enforce_gtid_consistency=l
interactive_timeout=300
wait_timeout=300
transaction_isolation = REPEATABLE-READ
innodb_buffer_pool_size = 1434M
innodb_data_file_path = ibdatal:1024M:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 256M
innodb_log_files_in_group =2
innodb_max_dirty_pages_pct =50
innodb_file_per_table = 1
innodb_locks_unsafe_for_binlog =0 
[mysqldump]
quick_max_allowed_packet = 32M
EOF

# 一步走
## 初始化
cd /usr/local/mysql/scripts
./mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql --defaults-file=/etc/my.cnf --user=mysql
## 启动, 默认读取配置文件顺序/etc/my.cnf -> /etc/mysql/my.cnf -> /usr/local/mysql/etc/my.cnf -> ~/my.cnf
cd /usr/local/mysql/bin/ 
./mysqld_safe --defaults-file=/etc/my.cnf &

初始化报错:

FATAL ERROR: please install the following Perl modules before executing ./mysql_install_db:
Data::Dumper

解决: yum install -y autoconf

1.3 创建密码

use mysql;
update user set password=password('root123') where user='root';
flush privileges

# 5.6 要清理无密码root用户
delete from user where user != 'root' or host != 'localhost';

1.4 关闭数据库

cd /usr/local/mysql/bin 
./mysqladmin -uroot -proot123 shutdown

1.5 主要数据库

show databases;

information_schema数据库是在安装 MySQL过程中的初始化阶段自动生成的。它提供了访问数据库中元数据的所有信息,可以理解为数据字典,它就是 MySQL的信息库,里面保存着关于 MySQL服务器所维护的所有其他数据库的信息,如数据库名、数据库里面的表、表的数据类型和访问权限等。但该库是只读库,只能进行 select操作。我们在 information schema下用得比较多的表有:

  • tables(记录所有表的基本信息,访问该表可收集表的统计信息)。
  • ROCESSLIST(查看当前数据库的连接)。
  • GLOBAL STATUS(查看数据库运行的各种状态值)
  • GLOBAL VARIABLES(查看数据库中的参数)
  • PARTITIONS(数据库中表分区的情况)。
  • INNODB LOCKS、 INNODB TRX、 INNODB LOCK WAITS这三张表用来监控数据库中锁的情况。

1.6 MySQL 5.7 安装差别

  • 初始化废弃了 mysql_install_db 使用./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/ --user=mysql --initialize
  • 初始化生成随机密码,在日志文件中。--initialize-insecure参数可以不生成随机密码
  • 密码字段用authentication_string字段替换pssword
原文地址:https://www.cnblogs.com/drfung/p/13492004.html