在centos7下面搭建mysql 5.7 (具体版本是:5.7.36),选择使用虚拟机的方式来实现。mysql 5.7使用yum的方案安装,选择yum的方式安装的原因是比较快速的安装好mysql server,然后切换mysql的datadir目录,而本地安装的操作相对烦锁一些。
centos7[centos1] : ip 192.168.32.131 master mysql5.7
centos7[centos2] : ip 192.168.32.130 slave mysql5.7
在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB。
[root@localhost ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
使用上面的命令就直接下载了安装用的Yum Repository,就可以直接yum安装了。
[root@localhost ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm
[root@localhost ~]# yum -y install mysql-community-server
[root@localhost ~]# systemctl start mysqld.service
[root@localhost lib]# systemctl status mysqld
此时MySQL已经开始正常运行,不过要想进入MySQL还得先找出此时root用户的密码,通过如下命令可以在日志文件中找出密码:
[root@localhost lib]# grep "password" /var/log/mysqld.log
[root@localhost lib]# mysql -uroot -pd8Xnjv:7GvP6
输入初始密码,此时不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
这里有个问题,新密码设置的时候如果设置的过于简单会报错:
原因是因为MySQL有密码设置的规范,具体是与validate_password_policy的值有关:
MySQL完整的初始密码规则可以通过如下命令查看:
mysql> SHOW VARIABLES LIKE 'validate_password%'; +--------------------------------------+-------+ | Variable_name | Value | +--------------------------------------+-------+ | validate_password_check_user_name | OFF | | validate_password_dictionary_file | | | validate_password_length | 4 | | validate_password_mixed_case_count | 1 | | validate_password_number_count | 1 | | validate_password_policy | LOW | | validate_password_special_char_count | 1 | +--------------------------------------+-------+ 7 rows in set (0.01 sec)
mysql> set global validate_password_policy=0; mysql> set global validate_password_length=1;
但此时还有一个问题,就是因为安装了Yum Repository,以后每次yum操作都会自动更新,需要把这个卸载掉:
[root@localhost ~]# yum -y remove mysql57-community-release-el7-10.noarch
a)关闭selinux
[root@localhost lib]# setenforce 0
b) 打开防火墙端口
[root@localhost lib]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
[root@localhost lib]# firewall-cmd --reload #注意,开放端口后一定要reload一下,否则不会立即生效
[root@localhost lib]# firewall-cmd --list-ports
3306/tcp
MySQL安装后默认安装在/var/lib/mysql
目录下
配置文件为/etc/my.cnf
根据场景,需要将/var/lib/mysql
移动到/home/mysql57/data
[root@localhost lib]# systemctl stop mysqld
[root@localhost lib]# systemctl status mysqld
mkdir /home/mysql57/data
cp -a /var/lib/mysql/* /home/mysql57/data
chown mysql:mysql -R /home/mysql57/data
touch /etc/my.cnf
vi /etc/my.cnf
1 # For advice on how to change settings please see 2 # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html 3 4 [mysqld] 5 # 6 # Remove leading # and set to the amount of RAM for the most important data 7 # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. 8 # innodb_buffer_pool_size = 128M 9 # 10 # Remove leading # to turn on a very important data integrity option: logging 11 # changes to the binary log between backups. 12 # log_bin 13 # 14 # Remove leading # to set options mainly useful for reporting servers. 15 # The server defaults are faster for transactions and fast SELECTs. 16 # Adjust sizes as needed, experiment to find the optimal values. 17 # join_buffer_size = 128M 18 # sort_buffer_size = 2M 19 # read_rnd_buffer_size = 2M 20 #datadir=/var/lib/mysql #新的数据目录 21 datadir=/home/mysql57/data 22 #socket=/var/lib/mysql/mysql.sock # 这里连接的sock也改为新的去 23 socket=/home/mysql57/data/mysql.sock 24 25 #开启bin log日志,这个后续 主从复制时会用到,注意的是:一定要加server-id的配置,否则binlog无法开启,针对mysql5.7的配置 26 log-bin=/home/mysql57/mysql-bin 27 server-id=1 28 29 # Disabling symbolic-links is recommended to prevent assorted security risks 30 symbolic-links=0 31 32 #log-error=/var/log/mysqld.log # 错误日志的配置路径 33 log-error=/home/mysql57/mysqld.log # 这个pid的file,尝试过修改,但是改动后,就会服务启不来,这里保持原来的,不影响 34 pid-file=/var/run/mysqld/mysqld.pid 35 #pid-file=/home/mysql57/mysqld.pid 36 37 # 字符集以及编码配置 38 collation-server = utf8_unicode_ci 39 init-connect='SET NAMES utf8' 40 character-set-server = utf8 41 sql_mode=NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION 42 43 [client] 44 default-character-set=utf8 45 [mysql] 46 default-character-set=utf8
[root@localhost lib]# systemctl start mysqld
[root@localhost lib]# systemctl status mysqld
在my.cnf的配置中,一定要注意的是:pid-file=/var/run/mysqld/mysqld.pid 这个最好是保留原来的,实际测试中,尝试改动过,但是一直mysql就启动不了,这里不浪费时间去折腾了。
通过上面的/etc/my.cnf的配置后,实现主从复制就非常简单了。
注意上面的配置中的:
log-bin=/home/mysql57/mysql-bin
server-id=1
由于mysql 的主从是通过binlog来实现的。所以必须加上这两项配置。
# vi /etc/my.cnf
[mysqld]
log-bin=/home/mysql57/mysql-bin //[必须]启用二进制日志 ------新增
server-id=11 //[必须]服务器唯一ID,slave01此处为72 ------新增
[root@master ~]# mysql -u root -p 123456
mysql> GRANT REPLICATION SLAVE ON *.* to 'copier'@'%' identified by 'copy123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show master status;
[root@slave01 ~]# mysql -u root -p 123456
Enter password:
...
mysql> change master to master_host='192.168.32.131',master_user='copier',master_password='copy123',master_log_file='mysql-bin.000004',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
#启动 从mysql
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
注意:
change master to master_host='192.168.32.131',master_user='copier',master_password='copy123',master_log_file='mysql-bin.000004',master_log_pos=154;
192.168.32.131 这是master的ip地址,实际中填写成自己的master地址。
master_log_file='mysql-bin.000004' 按照 show master status;查询出来的 binlog file文件填写,我这里是 mysql-bin.00004,起始地址:154 后面的master_log_pos=154
mysql> show slave status;
在master服务上,登陆mysql ,测试以下数据,在master上面创建数据库hello_db;
在slave上面查看是否同步过来。
可以发现,正常同步过来了。
查看同步信息的日志:
show processlist;
状态正常,至此主从复制配置成功。