数据库相关
mysql的主从
主库配置
#修改root密码和开启远程连接
select host,user from user where user='root';
update user set host = '%' where user ='root' and host='::1';
alter user 'root'@'localhost' identified by 'password' password expire nev
er;
alter user 'root'@'localhost' identified with mysql_native_password by 'Ne
twisd*8';
#创建dba账户
create user 'dba'@'%' identified by 'Netwisd*8';
grant replication slave on *.* to dba@'%';
select host,user from user where user='dba';
update user set host = '%' where user ='dba' and host='::1';
alter user 'dba'@'%' identified by 'Netwisd*8' password expire never;
alter user 'dba'@'%' identified with mysql_native_password by 'Netwisd*8';
flush privileges;
修改my.cnf配置文件,并重启mysql
log-bin=mysql-bin
server-id=1
binlog-ignore-db=information_schema
binlog-ignore-db=sys
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
binlog_format=mixed
max_connections = 1000
innodb_buffer_pool_size = 8G # 占总内存的一半
innodb_log_file_size = 1G # 日志文件大小,可以根据实际工作负载调整
max_allowed_packet = 64M
thread_cache_size = 8 # 根据CPU核心数调整
table_open_cache = 2000
tmp_table_size = 256M
max_heap_table_size = 256M
根据配置可以参考以下配置:
max_connections = 1000
innodb_buffer_pool_size = 8G # 占总内存的一半
innodb_log_file_size = 1G # 日志文件大小,可以根据实际工作负载调整
max_allowed_packet = 64M
thread_cache_size = 8 # 根据CPU核心数调整
table_open_cache = 2000
tmp_table_size = 256M
max_heap_table_size = 256M
max_connections = 1000
innodb_buffer_pool_size = 8G # 占总内存的一半
innodb_log_file_size = 1G # 日志文件大小,可以根据实际工作负载调整
max_allowed_packet = 64M
thread_cache_size = 8 # 根据CPU核心数调整
table_open_cache = 2000
tmp_table_size = 256M
max_heap_table_size = 256M
max_connections = 2000
innodb_buffer_pool_size = 16G # 占总内存的一半
innodb_log_file_size = 2G # 日志文件大小,可以根据实际工作负载调整
max_allowed_packet = 64M
thread_cache_size = 8 # 根据CPU核心数调整
table_open_cache = 4000
tmp_table_size = 512M
max_heap_table_size = 512M
获取主position值
use mysql;
show master status;
记录 file 和 position 对应的值
从库配置
#修改root密码和开启远程连接
select host,user from user where user='root';
update user set host = '%' where user ='root' and host='::1';
alter user 'root'@'localhost' identified by 'Netwisd*8' password expire nev
er;
alter user 'root'@'localhost' identified with mysql_native_password by 'Ne
twisd*8';
flush privileges;
vi /etc/my.cnf #增加以下内容,优化参考参考上面。
log-bin=mysql-bin
server-id=2
登录mysql后执行以下命令
use mysql;
change master to master_host='主ip', master_port = 主端口 ,master_user='dba',master_password='Netwisd*8',master_log_file='主show master 对应的file值',master_log_pos= 主show master 对应的position值;
start slave ;
show slave status\G;
#
Slave_IO_Running及Slave_SQL_Running进程必须正常运行,即YES状态,否则说明同步失败。
flush privileges;
quit;
Last modified: 20 一月 2025