1. MySQL的方式及区别
在MySQL启动遇到故障怎么排查
/etc/init/d/mysql start | restart | stop 这个脚本是一般安装后由mysql.server 自动生成,如果是直接解压使用的需要手动添加。
设置mysql自动启动:chkconfig mysql on,如果线上库,我建议关闭自动启动,线上库本着一个原则,出错了就停下来。或者脚本来控制批量的启动与停止。
/etc/init.d/mysql ( mysql.server)
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf &
启动:mysql -S /tmp/mysql3302.sock
关闭:mysqladmin -S /tmp/mysql3376.sock shutdown
mysql.server 调用的mysqld_safe,mysqld_safe调用的mysqld,一组MySQL用一个唯一个端口号,所有组之间的端口号最好不要重复。
2.新建一组服务器上多实例:
mkdir -p /data/mysql/mysql3377/{data,logs,tmp}
cp /data/mysql/mysql3376/my3376.cnf /data/mysql/mysql3377/my3377.cnf
cd /data/mysql/mysql3377
chown -R mysql:mysql mysql3377
sed -i ‘s/3376/3377/g‘ my3377.cnf
准备工作做完开始初始化操作:
cd /usr/local/mysql/
./script/mysql_db_install --defaults-file=/data/mysql/mysql3377/my3377.cnf OR /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3377/my3377.cnf &
启动:mysql -S /tmp/mysql3302.sock
关闭:mysqladmin -S /tmp/mysql3377.sock shutdown
创建多实例的基本思路:创建基础目录并修改权限,拷贝配置文件,替换端口号,初始化,启动,关闭。
多实例是可以写在同一个my.cnf里面,但是为了后续管理维护方便尽量单独一个实例使用单独的my.cnf
3.mysqld_mutli 的启动方式:
mysqld_multi 可以调用mysqld_safe,也可以调用mysqld,但是如果mysqld_multi,建议调用mysqld_safe,mysqld_multi的配置文件必须是 /etc/my.cnf。
[mysqld_multi]
mysqld=/usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
#用来做关闭mysql使用
user = root
log=/usr/local/mysql/m.log
[mysqld3376]
port=3376
socket=/tmp/mysql3376.sock
datadir=/data/mysql/mysql3376/data
server-id=1003376
log-bin=/data/mysql/mysql3376/logs/mysql-bin
tmpdir=/data/mysql/mysql3376/tmp
innodb_log_group_home_dir = /data/mysql/mysql3376/logs
innodb_buffer_pool_size=200M
单机多实例在配置文件应该注意什么问题:
port port=3377
datadir socket=/tmp/mysql3377.sock
socket datadir=/data/mysql/mysql3377/data
server-id server-id=1003377
log-bin=/data/mysql/mysql3377/logs/mysql-bin
tmpdir=/data/mysql/mysql3377/tmp
innodb_log_group_home_dir = /data/mysql/mysql3377/logs
mysqld配置文件的加载顺序:/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
帮助文档:msqyld --verbose --help
安装有错误的时候要会看error log,就是错误日志,所有的问题都会记录在这里
selinux一定要禁,在生产环境中也要这样,如果公司有硬件防火墙的话最好也将iptables关掉,如果没有的话就控制好iptables开放的端口
ibdata1 大小算法:6400*16/1024,如果你的ibdata1变得很庞大很有可能是启用了共享表空间,共享表空间,即使把数据清理掉后,也不会回收空间,这种怎么处理:
1. dump出来
2. 启用独立表空间导入
如果改小了能启动吗 可以auto_extend
双实例my.cnf实际配置:
上传二进制文件到/usr/local/ && mkdir -p /software/mysql
解压:
tar -xzvf mysql-5.6.29-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.29-linux-glibc2.5-x86_64 mysql
创建用户、创建mysql用户和组, 设置mysql登入密码:
groupadd mysql
useradd -g mysql -d /home/mysql mysql -d意思是指定home 可以使用man查看useradd的用法
Changing password for user mysql.
New password: mysql
创建目录:
mkdir -p /data/mysql/mysql3376/{data,logs}
mkdir -p /data/mysql/mysql3377/{data,logs}
授权:
chown -R mysql:mysql /data/mysql/mysql3376/
chown -R mysql:mysql /data/mysql/mysql3377/
分别在mysql3376和mysql3377目录下创建参数文件
vi my3376.cnf
[client]
port = 3376
socket = /tmp/mysql.sock
# The MySQL server
[mysqld]
# Basic
port = 3376
user = mysql
basedir = /usr/local/mysql ##二进制安装文件存放路径
datadir = /data/mysql/mysql3376/data ##数据文件存放路径
socket = /tmp/mysql3376.sock
log_bin = mysql3376-bin
log_error = /data/mysql/mysql3376/logs/error3376.log
slow_query_log_file = /data/mysql/mysql3306/logs/slow3376.log
skip-external-locking
skip-name-resolve
log-slave-updates
explicit_defaults_for_timestamp
relay_log=mysql3376-relay-log
relay_log_index=mysql3376relay-log-index
server-id = 337601
character_set_server = utf8 ###设置字符集
slow_query_log =1
long_query_time = 1
binlog_format = ROW ###binlog格式
max_binlog_size = 512M
binlog_cache_size = 2M
expire-logs-days = 15 ##binlog日志过期天数
lower_case_table_names=1 ##不区分大小写
back_log = 3000
max_connections=1000 ###设置最大连接数
max_user_connections=1000
max_connect_errors=900
interactive_timeout=7200
connect_timeout = 20
slave_net_timeout=60
max_relay_log_size = 512M
performance_schema=0
key_buffer_size = 16M
max_allowed_packet = 1024M
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
join_buffer_size = 1M
myisam_sort_buffer_size = 16M
tmp_table_size = 16M
max_heap_table_size = 64M
query_cache_type=0
query_cache_size = 0
bulk_insert_buffer_size = 16M
thread_cache_size = 100
thread_stack = 192K
skip-slave-start
# InnoDB
innodb_data_file_path = ibdata1:200M:autoextend
innodb_buffer_pool_size = 1024M
innodb_buffer_pool_instances = 8
innodb_log_file_size = 1000M
innodb_log_buffer_size = 16M
innodb_log_files_in_group = 3
innodb_flush_log_at_trx_commit = 1 ### 阿里设置为1
innodb_lock_wait_timeout = 50
innodb_sync_spin_loops = 100
innodb_max_dirty_pages_pct = 75
innodb_thread_concurrency = 0 ####5.6.29默认值为16,阿里RDS里面设置为0,0表示不限制
innodb_thread_sleep_delay = 1000
innodb_concurrency_tickets = 1000
log_bin_trust_function_creators = 1
innodb_flush_method = O_DIRECT
innodb_file_per_table = 1 设置独立表空
innodb_read_io_threads = 4
innodb_write_io_threads = 4
innodb_io_capacity = 2000
innodb_io_capacity_max = 4000
innodb_file_format = Barracuda
innodb_purge_threads=1
innodb_change_buffering=all
innodb_stats_on_metadata=OFF
[mysqldump]
quick
max_allowed_packet = 1024M
[mysql]
no-auto-rehash
max_allowed_packet = 1024M
prompt = ‘\[email protected]\h:\p\d mysql> ‘
default_character_set = utf8
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
#malloc-lib= /usr/local/mysql/lib/mysql/libjemalloc.so
3377实例参数
以下参数需要修改为和3376不一样
port datadir socket server-id binlog命名
cd /data/mysql/mysql3377/
cp /data/mysql/mysql3376/my3376.cnf my3377.cnf
sed -i ‘s/3376/3377/g‘ my3377.cnf
最后参数为
#more my3377.cnf
[client]
port = 3377
socket = /tmp/mysql.sock
# The MySQL server
[mysqld]
# Basic
port = 3377
user = mysql
basedir = /usr/local/mysql ##二进制安装文件存放路径
datadir = /data/mysql/mysql3377/data ##数据文件存放路径
socket = /tmp/mysql3377.sock
log_bin = mysql3377-bin
log_error = /data/mysql/mysql3376/logs/error3377.log
slow_query_log_file = /data/mysql/mysql3307/logs/slow3377.log
skip-external-locking
skip-name-resolve
log-slave-updates
explicit_defaults_for_timestamp
relay_log=mysql3377-relay-log
relay_log_index=mysql3377relay-log-index
server-id = 337701
character_set_server = utf8 ###设置字符集
slow_query_log =1
long_query_time = 1
binlog_format = ROW ###binlog格式
max_binlog_size = 512M
binlog_cache_size = 2M
expire-logs-days = 15 ##binlog日志过期天数
lower_case_table_names=1 ##不区分大小写
back_log = 3000
max_connections=1000 ###设置最大连接数
max_user_connections=1000
max_connect_errors=900
interactive_timeout=7200
connect_timeout = 20
slave_net_timeout=60
max_relay_log_size = 512M
performance_schema=0
key_buffer_size = 16M
max_allowed_packet = 1024M
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
join_buffer_size = 1M
myisam_sort_buffer_size = 16M
tmp_table_size = 16M
max_heap_table_size = 64M
query_cache_type=0
query_cache_size = 0
bulk_insert_buffer_size = 16M
thread_cache_size = 100
thread_stack = 192K
skip-slave-start
# InnoDB
innodb_data_file_path = ibdata1:200M:autoextend
innodb_buffer_pool_size = 1024M
innodb_buffer_pool_instances = 8
innodb_log_file_size = 1000M
innodb_log_buffer_size = 16M
innodb_log_files_in_group = 3
innodb_flush_log_at_trx_commit = 1 ### 阿里设置为1
innodb_lock_wait_timeout = 50
innodb_sync_spin_loops = 100
innodb_max_dirty_pages_pct = 75
innodb_thread_concurrency = 0 ####5.6.29默认值为16,阿里RDS里面设置为0,0表示不限制
innodb_thread_sleep_delay = 1000
innodb_concurrency_tickets = 1000
log_bin_trust_function_creators = 1
innodb_flush_method = O_DIRECT
innodb_file_per_table = 1 设置独立表空
innodb_read_io_threads = 4
innodb_write_io_threads = 4
innodb_io_capacity = 2000
innodb_io_capacity_max = 4000
innodb_file_format = Barracuda
innodb_purge_threads=1
innodb_change_buffering=all
innodb_stats_on_metadata=OFF
[mysqldump]
quick
max_allowed_packet = 1024M
[mysql]
no-auto-rehash
max_allowed_packet = 1024M
prompt = ‘\[email protected]\h:\p(\d) mysql> ‘
default_character_set = utf8
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
#malloc-lib= /usr/local/mysql/lib/mysql/libjemalloc.so
数据库初始化:
cd /usr/local/mysql/
./scripts/mysql_install_db --defaults-file=/data/mysql/mysql3376/my3376.cnf
./scripts/mysql_install_db --defaults-file=/data/mysql/mysql3377/my3377.cnf