序号 | 主机 | IP |
---|---|---|
1 | mysql001(master) | 192.168.1.1 |
2 | mysql002(slave) | 192.168.1.2 |
1.关闭 iptables、NetworkManager及selinux
systemctl stop firewalld && systemctl disable firewalld
systemctl disable --now firewalld NetworkManager
setenforce 0
sed -ri '/^[^#]*SELINUX=/s#=.+$#=disabled#' /etc/selinux/config
2.关闭swap分区
swapoff -a && sysctl -w vm.swappiness=0
sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab
3.设置yum源
yum install -y wget
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum makecache
yum install wget vim lsof net-tools lrzsz screen git gcc gcc-c++ -y
yum install -y libaio-devel
4.启用 ELRepo 仓库
ELRepo 仓库是基于社区的用于企业级 Linux 仓库,提供对 RedHat Enterprise (RHEL) 和 其他基于 RHEL的 Linux 发行版(CentOS、Scientific、Fedora 等)的支持。
ELRepo 聚焦于和硬件相关的软件包,包括文件系统驱动、显卡驱动、网络驱动、声卡驱动和摄像头驱动等。
启用 ELRepo 仓库:
# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
# rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
5.时间同步
切记所有机器需要自行设定ntp,否则不只HA下apiserver通信有问题,各种千奇百怪的问题。
yum -y install ntp
timedatectl set-timezone Asia/Shanghai
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo 'Asia/Shanghai' >/etc/timezone
ntpdate time2.aliyun.com
yum install chrony -y
cp /etc/chrony.conf /etc/chrony.conf.bak
cat>/etc/chrony.conf<< EOF
server ntp1.aliyun.com iburst minpoll 4 maxpoll 10
server ntp2.aliyun.com iburst minpoll 4 maxpoll 10
server ntp3.aliyun.com iburst minpoll 4 maxpoll 10
server ntp4.aliyun.com iburst minpoll 4 maxpoll 10
server ntp5.aliyun.com iburst minpoll 4 maxpoll 10
server ntp6.aliyun.com iburst minpoll 4 maxpoll 10
server ntp7.aliyun.com iburst minpoll 4 maxpoll 10
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony
EOF
systemctl start chronyd.service && systemctl status chronyd.service && systemctl enable chronyd.service
6.配置主机名
hostname --static set-hostname mysql001
7.安装开发部署环境
yum -y install ncurses-devel gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* libmcrypt* libtool-ltdl-devel* libtool make pcre* libaio.*
8.创建数据库软件目录
mkdir /data/services -p
cd /data/services
将文件上传或下载到该目录
9.创建 MySQL 用户组及用户
groupadd mysql
useradd -g mysql -s /sbin/nologin mysql
10.解压缩 mysql 的二进制包,并修改相关权限
http://mirrors.sohu.com/mysql/
https://www.mysql.com/downloads/
cd /data/services
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.41-linux-glibc2.12-x86_64.tar.gz
tar xf mysql-5.7.41-linux-glibc2.12-x86_64.tar.gz
ln -s mysql-5.7.41-linux-glibc2.12-x86_64 mysql
#创建数据目录:
mkdir /data/services/mysql/{data,logs,binlog,mysqltmp}
#对目录进行授权管理:
chown -R mysql.mysql /data/services/mysql
chown -R mysql.mysql /data/services/mysql-5.7.41-linux-glibc2.12-x86_64
设置环境变量
echo 'export PATH=/data/services/mysql/bin:$PATH' >> /etc/profile
source /etc/profile
11.初始化数据库 (可以先配置 /etc/my.cnf文件)
注:
-
之前版本mysql_install_db是在mysql_basedir/script下,5.7放在了mysql_install_db/bin目录下;
-
"--initialize"会生成一个随机密码(~/.mysql_secret),而"--initialize-insecure"不会生成密码
--initialize:
生成的临时密码需要修改
数据库密码的要妈祖密码复杂度
-
--datadir目标目录下不能有数据文件
mysqld --help --verbose| less(查看语法)
/data/services/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/data/services/mysql --datadir=/data/services/mysql/data
2020-06-27T10:37:39.531788Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-06-27T10:37:39.837894Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-06-27T10:37:39.867859Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-06-27T10:37:39.923909Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 39805555-b862-11ea-aa2b-000c291da56b.
2020-06-27T10:37:39.924517Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-06-27T10:37:40.527960Z 0 [Warning] CA certificate ca.pem is self signed.
2020-06-27T10:37:40.926519Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
12.配置mysql配置文件 /etc/my.cnf
简单配置文件:
rm -rf /etc/my.cnf
cat >>/etc/my.cnf<< \EOF
[client]
port = 3306
default-character-set = utf8mb4
socket = /data/services/mysql/mysql.sock
[mysqld]
user=mysql
port = 3306
secure-file-priv=/tmp
datadir=/data/services/mysql/data
socket = /data/services/mysql/mysql.sock
log-error= /data/services/mysql/logs/mysql-err.log
slow_query_log = 1
long_query_time = 5
log_queries_not_using_indexes = 1
slow_query_log_file=/data/services/mysql/logs/mysql_slow_queries.log
pid-file=/data/services/mysql/mysql.pid
lower_case_table_names = 1 #忽略大小写
gtid_mode = ON
enforce-gtid-consistency=true
log-slave-updates=1
server-id = 1 #主从要不一致
log-bin = /data/services/mysql/binlog/mysql-bin
max_binlog_size = 1000M
expire_logs_days = 7
binlog_format = mixed
character-set-server = utf8mb4
init-connect = 'SET NAMES utf8mb4'
default_storage_engine = InnoDB
table_open_cache = 512
tmpdir = /data/services/mysql/mysqltmp
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO
explicit_defaults_for_timestamp = 1
default-time-zone = '+8:00'
max_connections = 3600
max_allowed_packet = 100M
table_open_cache = 512
innodb_buffer_pool_size = 10G
innodb_log_file_size = 512M
innodb_file_per_table = 1
[mysql]
prompt="\\u@\\h :\\d\\r:\\m:\\s>"
auto-rehash
default-character-set = utf8mb4
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
thread_concurrency = 8
EOF
13.配置 mysql 启动文件
cp -f /data/services/mysql/support-files/mysql.server /etc/init.d/mysqld
sed -i 's#^basedir=$#basedir=/data/services/mysql#' /etc/init.d/mysqld
sed -i 's#^datadir=$#datadir=/data/services/mysql/data#' /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
systemctl start mysqld
systemctl daemon-reload && systemctl status mysqld
14.主库更新数据库密码(从库不做操作)
[root@wjg006 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.28 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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.
#设置root密码
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('12345678');
Query OK, 0 rows affected, 1 warning (0.00 sec)
#更新root别名
mysql> update user set user='admin' where user='root';
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#####################################################################################################
[root@db01 ~]# mysql -uadmin -p'12345678'
Enter password:
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
5.7 中用户基本信息
use mysql;
select user,authentication_string,host from mysql.user;
desc mysql.user;
15.数据库连接方式
mysql -uadmin -p'12345678' -h 192.168.1.1 -P 3306
16.主库中创建复制用户
grant replication slave on *.* to 'repl'@'192.168.1.%' identified by '888888';
flush privileges;
SELECT User,Host,authentication_string from mysql.user;
17、 获取主节点当前binary log文件名和位置(position)
mysql> SHOW MASTER STATUS;
18. 从库:192.168.1.2 执行 1-13 部署mysql,注意修改192.168.1.1;
19. 在从(Slave)节点上设置主节点参数
#vim /etc/my.cnf
#增加一下参数,避免从库误写;
read_only = 1
super_read_only = 1
change master to
master_host='192.168.1.1',
master_user='repl',
master_password='888888' ,
MASTER_AUTO_POSITION=1;
start slave;
show slave status\G;
20. 查看主从同步状态
mysql> show slave status\G;
appointment@localhost :(none)09:55:35>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.1
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1849
Relay_Log_File: zcy-fsyy-mysqlslave001-104-relay-bin.000002
Relay_Log_Pos: 2064
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: 1849
Relay_Log_Space: 2294
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: 1
Master_UUID: f72b2d36-8980-11eb-bdde-005056990ed7
Master_Info_File: mysql.slave_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: f72b2d36-8980-11eb-bdde-005056990ed7:1-7
Executed_Gtid_Set: f7082cc4-8980-11eb-b7aa-00505699c950:1-4,
f72b2d36-8980-11eb-bdde-005056990ed7:1-7
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
ERROR:
No query specified
appointment@localhost :(none)09:56:08>
21.创建用户
# readonly
GRANT SELECT ON *.* TO 'readonly'@'%' identified by 'sdfdddd!!';