MySQL AB 复制
一主多从架构
节点 |
系统 |
mysql版本 |
ip |
master |
centos:7 |
5.7 |
192.168.1.237 |
slave |
centos:7 |
5.7 |
192.168.1.238 |
作用:客户端的数据全部写入mysql主服务器中,主服务器将数据同步至从服务器,当用户读取数据时在从服务器中进行读取,极大降低了主服务器的压力,提高了整体业务的水平
两台主机都操作
一:安装mysql数据库
解压mysql tar文件
tar zxvf mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz
将解压的移动到 /usr/local/mysql 目录
mv mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql
添加系统mysql组和mysql用户
groupadd mysql
useradd -r -g mysql mysql (添加完成后可用id mysql查看)
cd /usr/local/mysql
修改当前目录拥有者为mysql用户
chown -R mysql:mysql ./
初始化数据库
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
此时生成了临时密码(下图有)
如果报这个错
bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory 解决方法:yum install -y libaio numactl.x86_64
//安装后在初始化就OK了
添加开机启动
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
修改 vi /etc/init.d/mysqld
添加路径 在46行
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
#chkconfig --add mysqld
#systemctl daemon-reload (刷新开机启动配置)
配置文件
vi /etc/my.cnf
[mysqld]
#可以免密码登录
#skip-grant-tables
###############################基础设置#####################################
#Mysql服务的唯一编号 每个mysql服务Id需唯一
server-id = 1
#服务端口号 默认3306
port = 3306
lower_case_table_names=1 #1代表关闭大小写区别 0代表开启大小写
default_storage_engine=InnoDB #存储引擎 lnnoDB
character_set_server=utf8
init_connect='SET NAMES utf8'
参数参考:(下面加粗部分无需操作)
配置默认编码为utf8
修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,如下所示:
(若/etc/没有my.cnf文件,可手动添加一个)
#Vi /etc/my.cnf
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
修改端口号、开放端口
#vi /etc/my.cnf
添加port=3306
Mysql默认是3306端口,若需要更改端口,需要先把seLinux关闭运行命令:setenforce 0
重新启动mysql服务使配置生效:
#systemctl restart mysqld
添加远程用户访问
查询下当前访问信息
mysql> select Host,User from mysql.user;
创建一个Dwz用户 任何地址都可以登录。密码Dwz123.
mysql>grant all privileges on *.* to 'Dwz'@'%'identified by 'Dwz123.' with grant option;
all代表接受所有操作,比如 select,insert,delete....; *.* 代表所有库下面的所有表;% 代表这个用户允许从任何地方登录;为了安全期间,这个%可以替换为你允许的ip地址;
然后刷新mysql用户权限相关表;
mysql>flush privileges ;
开启防火墙的情况下-设置远程端口
#firewall-cmd --zone=public --add-port=3306/tcp --permanent
端口配置生效 #firewall-cmd --reload
success
默认配置文件路径:
配置文件:/etc/my.cnf
日志文件:/var/log//var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid
查询数据库是否启动
#systemctl status mysqld.service
查询端口号是否启动
netstat -apn|grep 3306
tcp6 0 0 :::3306 :::* LISTEN 19960/mysqld
unix 3 [ ] STREAM CONNECTED 33061 1/systemd
刷新数据库
flush privileges;
其他liunx主机远程连接本机mysql数据库(其他主机已安装mysql):
mysql -u Dwz -p -h 192.168.208.134
*Dwz之前创建好的用户名
192.168.208.134 是要远程的ip
启动数据库
systemctl start mysql
修改root本地登录密码
先把mysql的命令设置为环境变量
/etc/profile中添加语句
vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin
source /etc/profile
service mysqld start
#mysql -uroot -p
输入密码:
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
重启数据库后密码生效。
方法二:
该方法先修改mysql配置文件使其可以无密码登录,跳过密码验证一步
修改/etc/my.cnf
#vi /etc/my.cnf
在[mysqld]下添加skip-grant-tables如图
在修改密码方法二修改配置文件跳过密码的情况下
#Mysql -uroot -p
也会出现这个提示,直接回车即可进入数据库
mysql> use mysql
Database changed!
mysql> update user set password=password('自定义密码') where user='root';
提示mysql> update user set password=password('VRGV2019.') where user='root';
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
执行:
Mysql>update user set authentication_string=password('123456') where user='root';
systemctl restart mysqld
***********重启数据库后新密码生效
再把配置文件改回去,重新启动数据库
master节点操作
添加配置
vim /etc/my.cnf
在[mysqld]下添加一下参数
log-bin=mysql-bin #启动二进制日志系统
binlog-do-db=test #二进制需要同步的数据库名,如果需要同步多个库,例如要再同步westos库,再添加一行“binlog-do-db=westos”,以此类推
server-id=1 #必须为 1到 232–1之间的一个正整数值
binlog-ignore-db=mysql #禁止同步 mysql 数据库
重新启动数据库
systemctl restart mysqld
配置主服务器用户并清空日志
mysql> show privileges;
mysql> grant replication client, replication slave on *.* to 'tongwz'@'192.168.1.%' identified by 'tongwz';
Query OK, 0 rows affected (0.00 sec)
mysql> reset master;
Query OK, 0 rows affected (0.02 sec)
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 107 |
+------------------+-----------+
1 row in set (0.00 sec)
slave节点操作
修改配置文件
cat /etc/my.cnf|grep server-id
server-id = 1
sed -i 's+server-id = 1+server-id = 2+g' /etc/my.cnf
cat /etc/my.cnf|grep server-id
server-id = 2
重启数据库
systemctl restart mysqld
创建配置连接
mysql -uroot -p
输入密码:
mysql> change master to
-> master_host='192.168.1.237',
-> master_user='tongwz',
-> master_password='tongwz',
-> master_port=3306,
-> master_log_file='mysql-bin.000001',
-> master_log_pos=107;
Query OK, 0 rows affected (0.01 sec)
此处的 master_log_file和master_log_pos与master中的数值对应
查看slave状态
先查看一下
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.11
Master_User: larry
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 107
Relay_Log_File: serv08-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
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: 107
Relay_Log_Space: 107
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: NULL
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: 0
1 row in set (0.00 sec)
ERROR:
No query specified
正式开启slave
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
从服务器查看是否和主服务器通信成功。如果出现 Slave_IO_Running和Slave_SQL_Running都是yes,则证明配置成功
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.11
Master_User: larry
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 107
Relay_Log_File: serv08-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000001
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: 107
Relay_Log_Space: 410
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
1 row in set (0.00 sec)
ERROR:
No query specified
此时,slave已经配置好,咱进一步查看
cd /usr/local/mysql/data
cat relay-log.info
...
253
mysql-bin.000001
107
[root@serv08 data]# cat master.info
18
mysql-bin.000001
107
192.168.1.237
tongwz
tongwz
3306
测试:
此次只同步了test数据库
在master节点操作test数据库下创建表
mysql> use test;
Database changed
mysql> create table test(id int(11));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test values(1);
Query OK, 1 row affected (0.00 sec)
mysql> show tables;
+-------------------+
| Tables_in_larrydb |
+-------------------+
| test |
+-------------------+
1 row in set (0.00 sec)
在slave节点查看
mysql> use test;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_larrydb |
+-------------------+
| test |
+-------------------+
1 row in set (0.00 sec)