说明:mysql主从都是二进制安装
mysql主:192.168.8.118
mysql从:192.168.8.125
1,修改mysql主的配置文件
[root@mastermysql mysql]#grep -in server-id /data/mysql/mysql3317/conf/my3317.cnf
11:#server-id = 3317
50:server-id=1
修改完mysql的配置文件后需要重新启动mysql
2,关掉mysql服务重启
[root@mastermysql mysql]# pwd
/usr/local/mysql
[root@mastermysql mysql]# ./bin/mysqladmin -S /data/mysql/mysql3317/run/mysql3317.sock shutdown
[root@mastermysql mysql]# tail -5 /etc/profile
alias my3317stop='cd/usr/local/mysql && ./bin/mysqladmin -S /data/mysql/mysql3317/run/mysql3317.sock shutdown '
使设置的别名生效
[root@mastermysql mysql]# source /etc/profile
3,启动mysql服务
[root@mastermysql mysql]# ./bin/mysqld_safe --defaults-file=/data/mysql/mysql3317/conf/my3317.cnf &
4,进入数据库,创建repl用户,密码为Ron_master_1,192.168.8网段都可登录
[root@mastermysql mysql]# /usr/local/mysql/bin/mysql -S /data/mysql/mysql3317/run/mysql3317.sock --prompt='mysqlmaster3317> '
mysqlmaster3317> CREATE USER 'repl'@'192.168.8.%' IDENTIFIED WITH mysql_native_password BY 'Ron_master_1';
5,赋予repl用户所有权限,并且该网段的所有ip都可登录
mysqlmaster3317> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.8.%';
flush privileges;
6,查看binlog日志
mysqlmaster3317> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 157 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set, 1 warning (0.00 sec)
7,修改mysql从的配置文件
[root@slavemysql mysql]# grep -in server-id /data/mysql/mysql3316/conf/my3316.cnf
11:#server-id = 3316
50:server-id=2
8,关掉mysql服务重启
[root@slavemysql mysql]# ./bin/mysqladmin -S /data/mysql/mysql3316/run/mysql3316.sock shutdown
(1)设置别名
[root@slavemysql mysql]# tail -3 /etc/profile
alias my3316stop='cd /usr/local/mysql && ./bin/mysqladmin -S /data/mysql/mysql3316/run/mysql3316.sock shutdown '
root@slavemysql mysql]# source /etc/profile
9,启动mysql从服务
[root@slavemysqlmysql]# ./bin/mysqld_safe --defaults-file=/data/mysql/mysql3316/conf/my3316.cnf &
10,进入mysql从的数据库
[root@slavemysql mysql]# /usr/local/mysql/bin/mysql -S /data/mysql/mysql3316/run/mysql3316.sock
11,与主库建立关联关系
mysql> change master to
-> master_host='192.168.8.118', # nysql主服务器IP地址
-> master_user='repl', # nysql主服务器用户名称
-> master_password='Ron_master_1', # nysql主服务器用户密码
-> master_port=3317,
-> master_log_file='mysql-bin.000002',
-> master_log_pos=157;
Query OK, 0 rows affected, 9 warnings (0.02 sec)
mysql> start slave;
mysql> flush privileges;
12,查看从节点的状态
show slave status\G;
出现双Yes
13,测试登录repl用户在从mysql上
[root@slavemysql mysql]# mysql -h192.168.8.118 -urepl -p'Ron_master_1' -P3317