debian9 使用mysqld_multi 来配置安装单机多实例以及主从
默认已经apt install 好了mysql-server, 目的只是测试下mysqld_multi 来配置单机多实例和主从. 这里没有用docker来直接启不同的mysql实例.
默认的mysql已经启用并且占用3306端口, 新加的3307和3308 分别为 从(3307)和主(3308).
mysql 3306 独立运行, 使用配置/etc/mysql/my.cnf (软链到 /etc/mysql/mysql.cnf)
mysql的主从基于binlong, 所以主需要开启binlong, 主上有一个log dump的县城, 用来和从的I/O线程传递binlog, 从将主的binlog事件(sql语句)同步到从机并记录在relaylog 里, 从根据relaylog的sql语句按序执行.
单机多实例配置
新加两个实例, 分别是3307和3308端口.
首先是增加配置, 在/etc/mysql/ 下新建一个文件 mysql_multi.cnf
[client]
default-character-set=utf8
default-character-set=utf8mb4
[mysql]
prompt=\\u@\\h:\\p \\d \\r:\\m:\\s>
default-character-set = utf8
[mysqld_multi]
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user = root
password = 123456
log = /var/log/mysql/mysqld_multi.log
[mysqld3308]
default-storage-engine=INNODB
init-connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_general_ci
user = mysql
port = 3308
basedir = /usr
datadir = /var/lib/mysql3308
tmpdir = /tmp
socket = /var/run/mysqld/mysqld3308.sock
pid-file = /var/run/mysqld/mysqld3308.pid
log-error = /var/log/mysql/mysql3308.log
lc-messages-dir = /usr/share/mysql
skip-external-locking
bind-address = 0.0.0.0
net_buffer_length = 640K
net_read_timeout = 60
net_write_timeout = 60
connect_timeout = 10
tmp_table_size = 256M
skip-name-resolve
max_allowed_packet = 16M
table_open_cache = 512
read_buffer_size = 2M
read_rnd_buffer_size = 8M
thread_stack = 192K
thread_cache_size = 8
slow-query-log = 1
long_query_time = 10
log_queries_not_using_indexes = 1
expire_logs_days = 30
[mysqld3307]
port = 3307
basedir = /usr
datadir = /var/lib/mysql3307
tmpdir = /tmp
socket = /var/run/mysqld/mysqld3307.sock
pid-file = /var/run/mysqld/mysqld3307.pid
log-error = /var/log/mysql/mysql3307.log
user = mysql
接下来创建两个实例的目录
#mkdir /var/lib/mysql3307 /var/lib/mysql3308
#chown -R mysql:mysql /var/lib/mysql3307 /var/lib/mysql3308
初始化实例
#mysqld --initialize --basedir=/usr --datadir=/var/lib/mysql3307 --user=mysql
这里会有个密码1, 简单记下
#mysqld --initialize --basedir=/usr --datadir=/var/lib/mysql3308 --user=mysql
这里会有个密码2, 简单记下
不想记住密码的话, 用参数–initialize-insecure
为了简单我这里都用的root/123456 作为mysql帐号密码
mysqld_multi start/stop/report/reload [server-id]
命令一看就明白. 开始/停止/报告/重启含加载
启动
mysqld_multi --defaults-extra-file=/etc/mysql/mysql_multi.cnf start
接下来进去修改下密码
/usr/bin/mysqladmin -u root -p password "123456" -S /var/run/mysqld/mysqld3307.sock
输入刚才记的密码1
然后尝试登录
mysql -uroot -p -P3307 -S /var/run/mysqld/mysqld3307.sock
注意, 这里指定了SOCK参数, 如果想不指定sock参数, 可以用-hserver_ip 来链接(不能用localhost或者域名, 还没授权)
mysql -uroot -p -h127.0.0.1 -P3307
查看下权限, 没有就授权, 简单就授权了所有
>show grants for 'root';
>grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
>flush privileges;
3308 重复上面操作
这时单机就启了3个实例了. 包含一个原本的3306, 通过mysqld_multi 启的3307和3308, 各实例间没有关系.
现在配置3307为从, 3308为主.
进入3308 创建ssss的DB和aaatest的表
create database ssss;
use ssss;
CREATE TABLE `aaatest` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` varchar(10) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `aaatest` (`content`) VALUES ('aaa'),('bbb'),('ccc'),('ddd');
然后把这个库和表导入到3307
在[mysqld3308] 里面增加
server-id=3308
log-bin=/var/lib/mysql3308/mysql-bin-3308
binlog_do_db = ssss #要同步的库, 这里我创建了一个ssss的DB, 里面有一个表aaatest
binlog-ignore-db = mysql #不同步mysql库和test库
binlog-ignore-db = test
log-slave-updates
sync_binlog=1
auto_increment_offset = 1
auto_increment_increment = 1
expire_logs_days = 7
log_bin_trust_function_creators = 1
在[mysqld3307] 里面增加
server-id=3307
#replicate-do-db=ssss
replicate-wild-do-table = ssss.%
replicate-wild-ignore-table = test.%
接下来重启下3307和3308
mysqld_multi --defaults-extra-file=/etc/mysql/mysql_multi.cnf reload
然后登录3308, 执行
show master status;
记下File
接下来去3307来启动slave和配置master (mysql5.5 后执行在mysql 里面执行命令来配置启用master slave同步)
mysql 3307>CHANGE MASTER TO
MASTER_HOST='127.0.0.1',
MASTER_USER='root',
MASTER_PORT=3308,
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin-3308.000004',
MASTER_LOG_POS=0;
mysql 3307>slave start;
mysql 3307>show slave status\G;
可以看见在主库3308上已经多了一个Binlog Dump的线程
分别查询下数据.
在3308主上插入一条, 从库执行查询
这样单机多实例主从就实现了. 里面有权限的一些配置, 这个按需配置就行了. 还有一些主从参数的优化查下官方手册就可以了.