同步时间
yum -y install ntpdate.x86_64
ntpdate ntp.ntsc.ac.cn
主服务器配置:
#!/bin/bash
yum list installed |grep libaio
if [ $? ne 0 ]; then
yum -y install libaio
fi
echo libaio yes
rm -rf /etc/my.cnf
echo remo my.cnf yes
tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz
echo tar zx yes
cp -r ~/mysql-8.0.33-linux-glibc2.12-x86_64 /usr/local/mysql
echo copy file to /usr/local/mysql yes
mkdir /usr/local/mysql/mysql-files
echo mysql-files yes
grep mysql /etc/passwd
useradd -r -s /sbin/nologin mysql
chown mysql:mysql /usr/local/mysql/mysql-files
chmod 750 /usr/local/mysql/mysql-files
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/
/usr/local/mysql/bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql8
sed -i '$aexport PATH=/usr/local/mysql/bin:$PATH' /etc/profile
source /etc/profile
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
port=3306
log-err=/usr/local/mysql/data/db01-master.err
log-bin=/usr/local/mysql/data/binlog
server-id=10
character_set_server=utf8mb4
开机自启
[root@mysql001 ~]# chkconfig --add mysql8
[root@mysql001 ~]# chkconfig mysql8 on
[[root@mysql001 ~]# chkconfig --list
从服务器配置:(不要初始化)
[root@slave_mysq ~]# vim mysql.sh (修改2行)
[root@slave_mysq ~]# source mysql.sh
修改配置文件
从主服务器上下载 /usr/local/mysql/my.cnf(修改)
[mysqld] (就是里面的内容可以直接复制)
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
port=3310
log-error=/usr/local/mysql/data/db01-slave.err
relay-log=/usr/local/mysql/data/relaylog
server-id=11
character_set_server=utf8mb4
回主服务器(停服务):
[root@master_mysq ~]# service mysql stop
删除下面文件
[root@master_mysq ~]# rm -rf /usr/local/mysql/data/auto.cnf
两台服务都装rsync
yum list installed |grep rsync
yum -y install rsync
同步master中的data到slave中
[root@master_mysq ~]# rsync -av /usr/local/mysql/data root@192.168.1.30:/usr/local/mysql/
从服务器中查看是否同步成功
[root@slave_mysq ~]# ls /usr/local/mysql/
启动主从服务器的服务(不能在从服务器中写数据)(从服务器密码就是主服务器的密码)
[root@master_mysq ~]# service mysql8 start
[root@slave_mysq ~]# service mysql8 start
[root@slave_mysq ~]# mysql -P3310 -pLiyang@2003(不要在里面添加数据,看了就出来)
主从特权 实现
1.master创建授权账号
[root@master_mysq ~]# mysql -pLi_yang2003
mysql> create user 'liyang'@'%' identified by 'abc_123'; (创建用户)
mysql> grant replication slave on *.* to '' 'liyang'@'%'; (设置权限)
mysql> flush privileges; (刷新权限)
mysql> show variadbles like 'server_id' (查看)
2.在master上锁表(不能修改)
mysql> flush tables with read lock;
msql> show master status; (查看)
从服务器配置:
[root@salve_mysq ~]# mysql -P3310 -pLiyang@2003
mysql> change master to
-> master_host='192.168.1.29',
-> master_user='liyang',
-> master_password='abc_123',
-> master_port=3306,
-> master_log_file='binlog.000006', (上面查看后就有了)
-> master_log_pos=866; (上面查看后就有了)
mysql> start slave;
mysql> show slave status\G (看看是否配置成功)
远程登录进入主服务器mysql(需要ssl非对称加密8.0的有这个问题)
mysql> mysql -u liyang -pabc_123 -h 192.168.1.26 -P3306 --get-server-public-key (获得master服务器的公钥) (在从服务器中输入)(然后退出登录本机数据库)
[root@salve_mysq ~]# mysql -P3310 -pLiyang@2003
mysql> stop slave;
mysql> reset slave;
mysql> change master to
-> master_host='192.168.1.29',
-> master_user='liyang',
-> master_password='abc_123',
-> master_port=3306, ·/
-> master_log_file='binlog.000006',
-> master_log_pos=866;
启动服务
mysql> start slave;
mysql> show slave status\G (看看是否配置成功变成yes)
检验