Msql-51CTO笔记
一、生产环境准备
1、网络环境
CentOS7: 防火墙临时关闭:[root@localhost home]# systemctl stop firewalld 关闭防火墙开机自启:[root@localhost home]# systemctl disable firewalld selinux临时关闭: [root@localhost home]# setenforce 0 selinux永久关闭: [root@localhost home]# sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/sysconfig/selinux CentOs6: 防火墙临时关闭:[root@localhost home]# service iptables stop 关闭防火墙开机自启:[root@localhost home]# chkconfig iptables off selinux临时关闭:[root@localhost home]# setenforce 0 selinux永久关闭:[root@localhost home]# sed -i 's/SELINUX=.*/SELINUX=disbales/g' /etc/sysconfig/selinux
2、 CPU IO调度模型(用于IO读写磁盘效率)
IO模型模式说明参考:https://www.cnblogs.com/cobbliu/p/5389556.html
IO Scheduler设置:(机械用盘建议使用:deadline,固态硬盘建议使用 noop)
查看当前IO调度模式: [root@localhost home]# cat /sys/block/sda/queue/scheduler 设置IO调度模式: Centos7: [root@localhost home]# grubby --update-kernel=ALL --args="elevator=deadline"
3、SWAP设置
查看当前分配比例 [root@localhost home]#cat /proc/sys/vm/swappiness 修改优先使用比例 当前修改: [root@localhost home]# echo 10 > /proc/sys/vm/swappiness 开机调用: [root@localhost home]# echo -e "echo 10 > /proc/sys/vm/swappiness" >> /etc/rc.d/rc.local
4、文件系统设置
mysql 是B+Tree的结构,而XFS也是这种存储格式,与mysql结合更好,并且文件系统类型更稳定。
Cenos7文件系统默认:XFS Centos6 文件系统默认ext4.
设置 Centos6 文件系统为 XFS
1、[root@localhost home]# yum install -y xfsprogs xfsdump
2、格式化文件系统: [root@localhost home]# mkfs.xfs /dev/sdb
5、系统参数设置
Mysql本身就是为应用提供服务的,所以生产上会有大量的连接到Mysql上,并且并发进行文件数据读取,而Linux本身为了防止并发进程过多导致系统崩溃,对进行参数进行了限制,我们直接进行修改即可
1、查看:ulimit -a 1.打开文件的句柄数:open files(防止too many open files错误) 2.针对用户数量限制:max user processes (防止单机多实例,连接数过多拒绝新连接) 2、修改: [root@localhost home]# echo -e "* soft nproc 65535\n* hard nproc 65535\n* soft nofile 65535\n* hard nofile 65535\n" >> /etc/security/limits.conf
二、mysql5.6安装
1、环境准备 1.创建目录: [root@localhost home]# mkdir -p data/{mysql,software} [root@localhost home]# mkdir -p data/mysql/{data,tmp,log} 2.创建用户: [root@localhost home]# useradd mysql 3.安装依赖包: [root@localhost home]# yum install perl perl-devel perl-Data-Dumper libaio-devel -y 3、安装 1.下载包文件 [root@localhost home]# cd /data/software [root@localhost home]# wget https://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz [root@localhost home]# tar -zxvf mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz [root@localhost home]# mv mysql-5.6.34-linux-glibc2.5-x86_64 /usr/local/mysql
2.权限赋值:
[root@localhost home]# chown -R mysql:mysql /usr/local/mysql /data/mysql/*
三、my.cnf 配置文件
[client] port = 3306 socket = /data/mysql/tmp/mysql.sock [mysql] prompt="\u@db \R:\m:\s [\d]> " no-auto-rehash [mysqld] user = mysql port = 3306 basedir = /usr/local/mysql datadir = /data/mysql/data socket = /data/mysql/tmp/mysql.sock pid-file = /data/mysql/tmp/mysql.pid character-set-server=utf8 collation-server = utf8_general_ci #skip-character-set-client-handshake=true #init_connect='insert into auditlog.accesslog(ConnectionID,ConnUserName,PrivMatchName,LoginTime) values(connection_id(),user(),current_user(),now());' skip_name_resolve=1 event_scheduler = on sql_mode='NO_UNSIGNED_SUBTRACTION,NO_ENGINE_SUBSTITUTION' open_files_limit = 65535 innodb_open_files = 65535 back_log=1024 max_connections = 512 max_connect_errors=1000000 interactive_timeout=300 wait_timeout=300 max_allowed_packet = 1024M table_open_cache=2048 table_definition_cache=2048 table_open_cache_instances = 32 thread_cache_size = 128 thread_stack = 512K external-locking = FALSE max_tmp_tables=200 tmp_table_size=100M max_heap_table_size=100G explicit_defaults_for_timestamp = 1 lock_wait_timeout = 3600 auto_increment_increment = 1 auto_increment_offset = 1 autocommit = ON secure_file_priv='' read_only = OFF lower_case_table_names=1 innodb_fast_shutdown = 0 innodb_force_recovery=0 innodb_buffer_pool_dump_at_shutdown = 1 innodb_buffer_pool_load_at_startup = 1 log-error=/data/mysql/log/error.log slow_query_log=ON slow_query_log_file=/data/mysql/log/slow_mysql.log long_query_time=2 innodb_flush_log_at_trx_commit=1 innodb_log_file_size =1G innodb_log_files_in_group=3 innodb_log_group_home_dir=./ sync_binlog = 1 binlog_cache_size = 16M max_binlog_cache_size = 1G max_binlog_size=1G expire_logs_days = 30 default-storage-engine = INNODB #internal_tmp_disk_storage_engine = INNODB transaction_isolation=REPEATABLE-READ innodb_max_dirty_pages_pct = 50 innodb_adaptive_flushing = ON innodb_flush_method = O_DIRECT sort_buffer_size=8M join_buffer_size=8M query_cache_size=0 query_cache_type=0 read_buffer_size = 8M optimizer_switch="index_condition_pushdown=on,mrr=on,mrr_cost_based=on,batched_key_access=off,block_nested_loop=on" read_rnd_buffer_size = 8M innodb_old_blocks_pct=35 #innodb_additional_mem_pool_size= 128M innodb_buffer_pool_size= 1G innodb_buffer_pool_instances = 16 innodb_log_buffer_size =32M bulk_insert_buffer_size=128M innodb_change_buffer_max_size = 50 innodb_doublewrite=on innodb_adaptive_hash_index = on innodb_file_per_table =1 innodb_data_file_path = ibdata1:1024M:autoextend innodb_page_size = 16k #innodb_checksum_algorithm =crc32 innodb_lock_wait_timeout = 35 innodb_rollback_on_timeout = on innodb_sync_spin_loops = 100 innodb_spin_wait_delay = 30 innodb_lru_scan_depth = 4000 innodb_thread_concurrency = 0 innodb_write_io_threads = 2 innodb_read_io_threads = 2 innodb_purge_threads = 2 innodb_io_capacity = 800 innodb_io_capacity_max = 1600 server-id = 3306110 log-bin= /data/mysql/log/binlog-mysql binlog_format=row
四、初始化mysql
1.查看初始化使用帮助:/usr/local/mysql/scripts/mysql_install_db --help 2.初始化当前Mysql:/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/data --defaults-file=/etc/my.cnf --user=mysql 3.初始化完成后会提示你修改密码:/usr/local/mysql/bin/mysqladmin -u root password '123456' 4.查看初始化后数据库文件:ls /data/mysql/data/ 如果包括mysql、information_schema、test目录即可以视为初始化成功