利用mysqld_multi启动管理多实例
官方管理多实例的一个脚本peer
#将之前的目录清空
[[email protected] mysql]# tree /data/mysql/
/data/mysql/
├── mysql_3306
│ ├── data
│ ├── logs
│ ├── my3306.cnf
│ └── tmp
├── mysql_3307
│ ├── data
│ ├── logs
│ ├── my3307.cnf
│ └── tmp
└── yejr_3306
#3307目录规划
3306
/data/mysql/mysql_3306/{data,logs,tmp}
/data/mysql/mysql_3306/my3306.cnf
3307
/data/mysql/mysql_3307/{data,logs,tmp}
/data/mysql/mysql_3307/my3307.cnf
[[email protected] ~]# cat /etc/my.cnf
#my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysql]
prompt="\\[email protected]\\h [\\d]>"
#pager="less -i -n -S"
#tee=/opt/mysql/query.log
no-auto-rehash
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
log = /opt/mysql/mysqld_multi.log
user = mdev
password = mdev4admin
[mysqld]
#misc
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql/mysql_3306/data
port = 3306
socket = /tmp/mysql.sock
event_scheduler = 0
tmpdir = /data/mysql/mysql_3306/tmp
#timeout
interactive_timeout = 300
wait_timeout = 300
#character set
character-set-server = utf8
open_files_limit = 65535
max_connections = 100
max_connect_errors = 100000
#lower_case_table_names =1
#logs
log-output=file
slow_query_log = 1
slow_query_log_file = slow.log
log-error = error.log
log_warnings = 2
pid-file = mysql.pid
long_query_time = 1
#log-slow-admin-statements = 1
#log-queries-not-using-indexes = 1
log-slow-slave-statements = 1
#binlog
#binlog_format = STATEMENT
binlog_format = row
server-id = 23306
log-bin = /data/mysql/mysql_3306/logs/mysql-bin
binlog_cache_size = 4M
max_binlog_size = 256M
max_binlog_cache_size = 1M
sync_binlog = 0
expire_logs_days = 10
#procedure
log_bin_trust_function_creators=1
#
gtid-mode = 0
#relay log
skip_slave_start = 1
max_relay_log_size = 128M
relay_log_purge = 1
relay_log_recovery = 1
relay-log=relay-bin
relay-log-index=relay-bin.index
log_slave_updates
#slave-skip-errors=1032,1053,1062
#skip-grant-tables
#buffers & cache
table_open_cache = 2048
table_definition_cache = 2048
table_open_cache = 2048
max_heap_table_size = 96M
sort_buffer_size = 128K
join_buffer_size = 128K
thread_cache_size = 200
query_cache_size = 0
query_cache_type = 0
query_cache_limit = 256K
query_cache_min_res_unit = 512
thread_stack = 192K
tmp_table_size = 96M
key_buffer_size = 8M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 32M
#myisam
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
#innodb
innodb_buffer_pool_size = 100M
innodb_buffer_pool_instances = 1
innodb_data_file_path = ibdata1:100M:autoextend
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 8M
innodb_log_file_size = 100M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 50
innodb_file_per_table = 1
innodb_rollback_on_timeout
innodb_status_file = 1
innodb_io_capacity = 2000
transaction_isolation = READ-COMMITTED
innodb_flush_method = O_DIRECT
#端口号为3306的实例特殊配置
[mysqld3306]
port = 3306
datadir = /data/mysql/mysql_3306/data
tmpdir = /data/mysql/mysql_3306/tmp
log-bin = /data/mysql/mysql_3306/logs/mysql-bin
server-id = 1003306
socket = /tmp/mysql_3306.sock
#重新配置这几个选项,不与全局配置一样,会直接覆盖上面的全局设置
innodb_buffer_pool_size = 500M
[mysqld3307]
port = 3307
datadir = /data/mysql/mysql_3307/data
tmpdir = /data/mysql/mysql_3307/tmp
log-bin = /data/mysql/mysql_3307/logs/mysql-bin
server-id = 1003307
socket = /tmp/mysql_3307.sock
#重新配置这几个选项,不与全局配置一样,会直接覆盖上面的全局设置
innodb_buffer_pool_size = 500M
#对3306、3307进行初始化
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql_3306/my3306.cnf --initialize
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql_3307/my3307.cnf --initialize
#记住这个初始密码
cat /data/mysql/mysql_3306/data/error.log |grep password
2016-08-21T03:34:16.214559Z 1 [Note] A temporary password is generated for [email protected]: &%mh9C(jsbTF
cat /data/mysql/mysql_3307/data/error.log |grep password
2016-08-21T03:34:25.835638Z 1 [Note] A temporary password is generated for [email protected]: YMgbgJLkB9)r
#使用mysqld_multi启动方式二选一,均可启动:
mysqld_multi start 3306
mysqld_multi start 3307
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql_3306/my3306.cnf &
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql_3306/my3306.cnf &
#检查启动之后有无启动错误
cat /data/mysql/mysql_3306/data/error.log
cat /data/mysql/mysql_3307/data/error.log
mysql -S /tmp/mysql_3306.sock -p #输入密码: &%mh9C(jsbTF
mysql -S /tmp/mysql_3307.sock -p #输入密码: YMgbgJLkB9)r
#进入每个实例只有要修改密码并创建关闭账号
在mysql5.7里用户名与密码不能一样,否则会报错。
(unknown)@localhost [(none)]> alter user user() identified by ‘redhat‘; #修改密码
[email protected] [(none)]> create user ‘mdev‘@‘localhost‘ identified by ‘mdev4admin‘;
[email protected] [(none)]> grant shutdown on *.* to ‘mdev‘@‘localhost‘;
[email protected] [(none)]>select user,host,authentication_string from mysql.user;
+-----------+-----------+-------------------------------------------+
| user | host | authentication_string |
+-----------+-----------+-------------------------------------------+
| root | localhost | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mdev | localhost | *947A6520F0A2FD9A631D42B10246B03F392B35C2 |
+-----------+-----------+-------------------------------------------+
[email protected] [(none)]>flush privileges;
Query OK, 0 rows affected (0.00 sec)
[email protected] [(none)]>show grants for ‘mdev‘@localhost;
+---------------------------------------------+
| Grants for [email protected] |
+---------------------------------------------+
| GRANT SHUTDOWN ON *.* TO ‘mdev‘@‘localhost‘ |
+---------------------------------------------+
1 row in set (0.00 sec)
#手工验证密码关闭3306数据库
mysqladmin -S /tmp/mysql_3306.sock -p -u mdev shutdown #输入密码 mdev4admin
mysqladmin -S /tmp/mysql_3307.sock -p -u mdev shutdown
#关闭3307数据库
mysqladmin -S /tmp/mysql_3306.sock -p shutdown
#因为必须加上-s,才能获取对应端口的密码。
[[email protected] ~]# my_print_defaults -s mysqld_multi mysqld3307
--mysqld=/usr/local/mysql/bin/mysqld_safe
--mysqladmin=/usr/local/mysql/bin/mysqladmin
--log=/opt/mysql/mysqld_multi.log
--user=mdev
--password=mdev4admin
--port=3307
--datadir=/data/mysql/mysql_3307/data
--tmpdir=/data/mysql/mysql_3307/tmp
--log-bin=/data/mysql/mysql_3307/logs/mysql-bin
--server-id=1003307
--socket=/tmp/mysql_3307.sock
--innodb_buffer_pool_size=500M
#所以得修改mysqld_multi的默认脚本
[[email protected] ~]# vi /usr/local/mysql/bin/mysqld_multi
#####################################################################
216#my $com= join ‘ ‘, ‘my_print_defaults‘, @defaults_options, $group;
217my $com= join ‘ ‘, ‘my_print_defaults -s‘, @defaults_options, $group;
[[email protected] ~]# mysqld_multi stop 3306
[[email protected] ~]# cat /opt/mysql/mysqld_multi.log
Stopping MySQL servers
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
2016-08-21T04:50:39.315023Z mysqld_safe mysqld from pid file /data/mysql/mysql_3306/data/mysql.pid ended
[[email protected] ~]# mysqld_multi stop 3307
[[email protected] ~]# cat /opt/mysql/mysqld_multi.log #检查是否成功关闭
Stopping MySQL servers
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
2016-08-21T04:47:55.397112Z mysqld_safe mysqld from pid file /data/mysql/mysql_3307/data/mysql.pid ended
[[email protected] ~]# mysqld_multi start #后面什么都不带,意味着将所有mysql实例启动。
[[email protected] ~]# mysqld_multi start 3306-3310 #3306到3310的全部实例都启动。
[[email protected] ~]# ps -ef|grep mysql #查看启东进程
[[email protected] ~]# cat /opt/mysql/mysqld_multi.log #检查日志确保启动成功
Starting MySQL servers
2016-08-21T04:59:41.925014Z mysqld_safe Logging to ‘/data/mysql/mysql_3306/data/error.log‘.
2016-08-21T04:59:41.932341Z mysqld_safe Logging to ‘/data/mysql/mysql_3307/data/error.log‘.
2016-08-21T04:59:41.946622Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/mysql_3306/data
2016-08-21T04:59:41.952334Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/mysql_3307/data
[[email protected] ~]# mysql -S /tmp/mysql_3307.sock -p
Enter password:
[email protected] [(none)]>show global variables like "%innodb_buffer_pool%";
+-------------------------------------+----------------+
| Variable_name | Value |
+-------------------------------------+----------------+
| innodb_buffer_pool_chunk_size | 134217728 |
| innodb_buffer_pool_dump_at_shutdown | ON |
| innodb_buffer_pool_dump_now | OFF |
| innodb_buffer_pool_dump_pct | 25 |
| innodb_buffer_pool_filename | ib_buffer_pool |
| innodb_buffer_pool_instances | 1 |
| innodb_buffer_pool_load_abort | OFF |
| innodb_buffer_pool_load_at_startup | ON |
| innodb_buffer_pool_load_now | OFF |
| innodb_buffer_pool_size | 536870912| #大概是500M,单独定义的。
+-------------------------------------+----------------+
10 rows in set (0.00 sec)