您当前的位置:首页 > 计算机 > 软件应用 > 数据库 > MySQL

MySQL-5.7.31 的安装与主从同步的搭建

时间:04-11来源:作者:点击数:

一、安装mysql-5.7.31

(1) 下载安装包

#下载前先卸载mariadb的一些东西
yum remove -y mariadb*

#然后下载安装包
cd /usr/local/soft
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

(2) 解压

#解压
tar -xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

#创建软链接
ln -s mysql-5.7.31-linux-glibc2.12-x86_64 mysql

(3) 新建mysql用户以及数据目录

useradd -r -s /sbin/nologin mysql
mkdir -pv /usr/local/soft/mysql/{data,logbin,logs}

(4) 准备配置文件

vim /etc/my.cnf
#参考以下配置
[client]
port = 3306
socket = /usr/local/soft/mysql/mysql.sock

[mysqld]
##########################################
port = 3306
bind-address = 0.0.0.0
lower_case_table_names=1
character-set-server=utf8mb4
default-storage-engine=innoDB
basedir=/usr/local/soft/mysql
datadir=/usr/local/soft/mysql/data
socket=/usr/local/soft/mysql/mysql.sock
slow_query_log_file=/usr/local/soft/mysql/logs/slow.log
log-error=/usr/local/soft/mysql/logs/mysqld-error.log
sql_mode =STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

##########################################
lower_case_table_names= 1

max_connections = 2500
max_connect_errors = 6000
connect_timeout=10
wait_timeout = 300
interactive_timeout = 300
back_log = 300
open_files_limit = 65535
table_open_cache = 512
max_allowed_packet = 500M
max_heap_table_size = 8M
tmp_table_size = 64M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 64M
thread_cache_size = 32
thread_stack = 512K
ft_min_word_len = 1
skip-external-locking
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 32M
net_retry_count = 100
auto_increment_increment=0
auto_increment_offset=0
explicit_defaults_for_timestamp=false
log_bin_trust_function_creators=1
performance_schema= 0

#####
transaction-isolation = READ-COMMITTED
query_cache_size = 0
query_cache_type = 0

##########################################
log-bin=/usr/local/soft/mysql/logbin/mysql-bin
log-bin-index=/usr/local/soft/mysql/logbin/mysql-bin.index
server-id=1
max_binlog_size = 512M

#####
binlog_format = ROW
log_slave_updates = 0
expire_logs_days = 7
max_relay_log_size = 512M
binlog-ignore-db = mysql
binlog-ignore-db = test
binlog-ignore-db = information_schema
binlog-ignore-db = performance_schema
replicate-ignore-db = mysql
replicate-ignore-db = test
replicate-ignore-db = information_schema
replicate-ignore-db = performance_schema
binlog_cache_size = 1M
max_binlog_cache_size = 15M

##########################################
slow_query_log=1
long_query_time=1
log-queries-not-using-indexes = TRUE
log_throttle_queries_not_using_indexes=1000
min_examined_row_limit=1000
log-slow-admin-statements = TRUE
log-slow-admin-statements = TRUE

##########################################
innodb_file_per_table = 1
innodb_open_files = 500
#####
innodb_buffer_pool_size = 11468M		
innodb_buffer_pool_instances = 4		
innodb_buffer_pool_load_at_startup = on
innodb_buffer_pool_dump_at_shutdown = on 
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_max_dirty_pages_pct = 85
innodb_lock_wait_timeout = 120
innodb_flush_method=O_DIRECT

#####
innodb_flush_log_at_trx_commit = 1

#####undo log#####
innodb_data_file_path = ibdata1:1G:autoextend
#####redo log#####
innodb_log_files_in_group = 2
innodb_max_undo_log_size = 4G

innodb_autoinc_lock_mode  = 2
innodb_buffer_pool_dump_at_shutdown = 1
innodb_buffer_pool_load_at_startup  = 1
innodb_support_xa = 0
innodb_status_file  = 1

###########################################
[mysqld_safe]
user=mysql
log-error=/usr/local/soft/mysql/logs/mysql.log
pid-file=/usr/local/soft/mysql/mysql.pid

(5) 初始化数据库

#初始化前配置
touch /usr/local/soft/mysql/logs/mysql.log
chown -R mysql:mysql /usr/local/soft/mysql

#开始初始化
/usr/local/soft/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/soft/mysql --datadir=/usr/local/soft/mysql/data --lower-case-table-names=1

(6) 配置环境变量

vim /etc/profile
export PATH=/usr/local/soft/mysql/bin:$PATH

source /etc/profile

(7) 准备服务启动脚本,并启动数据库服务

#准备启动脚本
cp /usr/local/soft/mysql/support-files/mysql.server /etc/init.d/mysqld

#设置开机启动
chkconfig --add mysqld

#启动
/etc/init.d/mysqld start

#如果启动报以下错
/etc/init.d/mysqld: line 239: my_print_defaults: command not found
/etc/init.d/mysqld: line 259: cd: /usr/local/mysql: No such file or directory
Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)
##解决方法:
###方式一:
ln -s /usr/local/soft/mysql/bin/my_print_defaults /usr/bin/
###方式二:
vim /etc/init.d/mysqld
#修改以下两项
basedir=/usr/local/soft/mysql
datadir=/usr/local/soft/mysql/data

(8) 登录数据库,并修改密码

#查询root初始化密码
grep password /usr/local/soft/mysql/logs/mysqld-error.log

#登录,输上面命令查询到的密码
mysql -uroot -p

#修改root密码
> alter user user() identified by 'PASSWORD';

#添加用于远程连接的用户
> create user 'ktp_admin'@'%' identified by 'PASSWORD';
> grant all privileges on *.* to 'ktp_admin'@'%';
> flush privileges;

二、主从同步实现

(1) 按以上方式再安装一台mysql主机作为从节点。注意修改my.cnf中的server-id值,不要与主节点相同

vim /etc/my.cnf
...
server-id=3307
...

(2) 在主节点上新建一个用于同步的账号并授权

> grant replication slave on *.* to ktp_repl@'%' identified by 'PASSWORD';
> flush privileges;

(3) 查看主节点的二进制日志及位置

> show master logs;

(4) 从节点上配置同步

> CHANGE MASTER TO
MASTER_HOST= '主节点IP' , 
MASTER_PORT=3306,
MASTER_USER= 'ktp_repl' ,
MASTER_PASSWORD= 'PASSWORD' ,
MASTER_LOG_FILE= '主节点日志文件名' ,
MASTER_LOG_POS=主节点日志文件复制点;

(5) 开启同步

#开启同步
> start slave; 

#查看状态
> show slave status\G;
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门