一、基础环境
| 实验用途 | IP地址 | 操作系统 |
|---|---|---|
| MySQL主服务器 | 192.168.0.198 | Centos7.8 |
| MySQL从服务器 | 192.168.0.204 | Centos7.8 |
需要关闭selinux,防火墙。
| 软件名称 | 版本 |
|---|---|
| MySQL数据库 | 5.7.37 |
二、安装基础环境
1.关闭防火墙
$ systemctl stop firewalld.service
$ systemctl disable firewalld.service
2.关闭selinux
$ setenforce 0 #临时关闭
$ vim /etc/sysconfig/selinux #永久关闭
把SELINUX=enforcing替换为
SELINUX=disabled
3.查询linux里自带的mariadb,如有需要进行卸载
$ rpm -qa | grep maria
mariadb-5.5.56-2.el7.x86_64
mariadb-server-5.5.56-2.el7.x86_64
mariadb-libs-5.5.56-2.el7.x86_64
$ yum remove mariadb-server-5.5.56-2.el7.x86_64 #卸载mariabd
4.安装依赖包(如果未安装可能初始化失败)
$ yum install libaio*
二、部署mysql
1.下载软件包并解压
$ wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
$ tar xzf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
$ mv mysql-5.7.37-linux-glibc2.12-x86_64 /opt/mysql #移动mysql到/opt目录
2.创建mysql用户
$ groupadd mysql -g 512
$ useradd -u 512 -g mysql -s /sbin/nologin -d /home/mysql mysql
3.创建data数据目录并授权
$ mkdir /opt/mysql/data
$ chown -R mysql:mysql /opt/mysql
4.备份旧配置文件并创建新配置文件
$ mv /etc/my.cnf /etc/my.cnf.bak
$ cat >>/etc/my.cnf<<EOF
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
[mysqld]
basedir=/opt/mysql
port = 3306
socket = /tmp/mysql.sock
datadir = /opt/mysql/data
log-error=/opt/mysql/data/error.log
default_storage_engine = InnoDB
skip-external-locking
key_buffer_size = 32M
max_allowed_packet = 100G
table_open_cache = 128
sort_buffer_size = 768K
net_buffer_length = 4K
read_buffer_size = 768K
read_rnd_buffer_size = 256K
myisam_sort_buffer_size = 8M
thread_cache_size = 16
query_cache_size = 16M
tmp_table_size = 32M
#skip-name-resolve
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=mysql-bin
binlog_format=row
server-id = 1
expire_logs_days = 10
slow_query_log=1
slow-query-log-file=/opt/mysql/data/mysql-slow.log
long_query_time=3
#log_queries_not_using_indexes=on
innodb_data_home_dir = /opt/mysql/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /opt/mysql/data
innodb_buffer_pool_size = 128M
innodb_log_file_size = 64M
innodb_log_buffer_size = 16M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
innodb_max_dirty_pages_pct = 90
innodb_read_io_threads = 1
innodb_write_io_threads = 1
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
# INSTALL PLUGIN validate_password SONAME 'validate_password.so'; #如果报错需要安装插件
#validate_password_length = 8 #密码长度
#validate_password_policy=1 #密码策略
#validate_password_mixed_case_count=1 #大小写数量
#validate_password_special_char_count=0 #符号数量
#validate_password_number_count=1 #数字数量
#default_password_lifetime=60 #过期时间
[mysqldump]
quick
max_allowed_packet = 500M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 768K
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
EOF
5.初始化数据库
$ /opt/mysql/bin/mysqld --initialize --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data
# 如无报错说明初始化成功
6.将服务加入到开机自启动
$ cp /opt/mysql/support-files/mysql.server /etc/init.d/mysqld
$ chmod 700 /etc/init.d/mysqld
$ chkconfig --add mysqld
$ chkconfig --level 2345 mysqld on
$ /etc/init.d/mysqld start
Starting MySQL. [ OK ]
7.添加环境变量
$ cat >> /etc/profile <<EOF
export PATH=$PATH:/opt/mysql/bin
EOF
$ source /etc/profile
8.修改密码
$ grep password /opt/mysql/data/error.log | awk -F "root@localhost: " '{print $2}'
kn+EeJ1),IHe
$ mysql -uroot -p
Enter password: # 这里输入上方的密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37-log
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Admin@1234'; #修改root本地密码
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE USER 'xiaozhi'@'%' IDENTIFIED BY "xiaozhi#123"; # 创建远程访问用户
mysql> grant all on *.* to xiaozhi@"%"; #进行授权
Query OK, 0 rows affected (0.01 sec)
三、配置mysql主从
配置mysql主从前需要先确定一下信息
- 需要保证/etc/my.conf的server-id要与主mysql不一样,和其他从节点也要不一样
- 需要确定主数据库是否为新创建数据库,如不是需要手动备份数据,导入到从数据库。
1.创建主从授权用户(主库操作)
$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37-log
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant replication slave on *.* to 'rep'@'192.168.0.204'identified by 'Rep@admin123'; #创建主从复制用过户,这里授权到指定ip
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show master status; #查看主节点信息
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 940 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
2.配置主从(从库操作)
$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37-log
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> change master to master_host='192.168.0.198',master_user='rep',master_password='Rep@admin123',master_log_file='mysql-bin.000002',master_log_pos=940; # 配置主从
Query OK, 0 rows affected, 2 warnings (0.03 sec)
# master_host=主数据库ip,master_user=用户名,master_password=密码,master_log_file=binlog文件,master_log_pos=posid
mysql> start slave; # 启动从节点
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.198
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 940
Relay_Log_File: hecs-365873-0001-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 940
Relay_Log_Space: 538
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2
Master_UUID: e90b221f-f1d9-11ec-b3ae-fa163e5b86a2
Master_Info_File: /opt/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
# 当Slave_IO_Running: Yes,Slave_SQL_Running: Yes都是yes的时候说明我们主从已经生效
当Slave_IO_Running: Yes,Slave_SQL_Running: Yes两个都是yes的时候说明我们主从已经成功
该博客详细介绍了如何在Centos7.8环境下搭建MySQL 5.7.37的基础环境,包括关闭防火墙和selinux,卸载mariadb,安装MySQL数据库,配置环境变量,初始化数据库,设置用户权限,并完成了主从复制的配置,确保了主从同步的正常运行。
3015

被折叠的 条评论
为什么被折叠?



