[root@server1 ~]# vim /etc/my.cnf
[root@server1 ~]# cat /etc/my.cnf | tail -n 2
server-id=1//這個server-id可以任意,但是必須是唯一的//這里server1的設(shè)為1, sevrre2的設(shè)為2
log-bin=mysql-bin //日志名稱
[root@server1 ~]# /etc/init.d/mysqld start //mysql的開啟
Initializing MySQL database: [ OK ]
Installing validate password plugin: [ OK ]
Starting mysqld: [ OK ]
[root@server1 ~]# grep password /var/log/mysqld.log
//過濾查看mysql的初始密碼(其他過濾出來的內(nèi)容就不顯示了)2018-08-08T08:51:19.321424Z 1 [Note] A temporary password is generated for root@localhost: u/Klif/g3sFi
[root@server1 ~]# mysql_secure_installation //進(jìn)行初始化Securing the MySQL server deployment.Enter password for user root: The existing password for the user account root has expired. Please set a new password.New password: //輸入用戶密碼8位以上,并且是字母大小寫+特殊字符+數(shù)字Re-enter new password:
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.Remove test database and access to it? (Press y|Y for Yes, any other key for No) : ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.All done!
[root@server1 ~]# mysql -p
Enter password: //新密碼登陸即可
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.17-log MySQL Community Server (GPL)Copyright (c) 2000, 2016, 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.mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)mysql> grant replication slave on *.* to repl@'172.25.54.%' identified by '5820hhXM!@#';
//創(chuàng)建同步帳戶,并給予權(quán)限
Query OK, 0 rows affected, 1 warning (0.04 sec)mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 843 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)mysql>
server2登陸測試
[root@server2 ~]# mysql -u repl -p -h 172.25.54.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.17-log MySQL Community Server (GPL)Copyright (c) 2000, 2016, 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.mysql> quit
Bye
配置slave
[root@server2 ~]# vim /etc/my.cnf
[root@server2 ~]# cat /etc/my.cnf | tail -n 1
server-id=2//只需服務(wù)id即可
[root@server2 ~]# /etc/init.d/mysqld start
Starting mysqld: [ OK ]
[root@server2 ~]# grep password /var/log/mysqld.log
2018-08-08T08:52:10.646512Z 1 [Note] A temporary password is generated for root@localhost: Ddp=y*gOr0uq
[root@server2 ~]# mysql_secure_installation //初始化過程同server1
[root@server2 ~]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.17 MySQL Community Server (GPL)Copyright (c) 2000, 2016, 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.mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)mysql> change master to master_host='172.25.54.1',master_user='repl',master_password='5820hhXM!@#',master_log_file='mysql-bin.000003',master_log_pos=843;
//與master建立認(rèn)證, 所有信息需要與master的一樣//master主機(jī)ip,被授權(quán)的用戶,密碼,日志文件,pos號
Query OK, 0 rows affected, 2 warnings (0.99 sec)mysql> start slave;
Query OK, 0 rows affected (0.07 sec)mysql> show slave status\G
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 172.25.54.1Master_User: replMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000003Read_Master_Log_Pos: 843Relay_Log_File: server2-relay-bin.000002Relay_Log_Pos: 320Relay_Master_Log_File: mysql-bin.000003Slave_IO_Running: Yes //yes代表I/O線程沒問題Slave_SQL_Running: Yes //yes代表SOL線程沒問題Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 843Relay_Log_Space: 529Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1Master_UUID: 37ab8283-9ae8-11e8-ba69-525400d23507Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row in set (0.00 sec)mysql>
測試數(shù)據(jù)同步
在master上進(jìn)行數(shù)據(jù)庫操作
mysql> create database test;
Query OK, 1 row affected (0.14 sec)mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)mysql> use test;
Database changed
mysql> create table userlist (-> username varchar(15) not null,-> password varchar(25) not null);
Query OK, 0 rows affected (0.66 sec)mysql> desc userlist;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(15) | NO | | NULL | |
| password | varchar(25) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.02 sec)mysql> insert into userlist values ('user1','147');
Query OK, 1 row affected (0.07 sec)mysql> insert into userlist values ('user2','258');
Query OK, 1 row affected (0.10 sec)mysql> insert into userlist values ('user3','369');
Query OK, 1 row affected (0.06 sec)mysql> quit
Bye
[root@server1 ~]#
在slave上邊驗證
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> select * from userlist;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 147 |
| user2 | 258 |
| user3 | 369 |
+----------+----------+
3 rows in set (0.00 sec)mysql> quit
Bye
[root@server2 ~]#
[root@server2 ~]# vim /etc/my.cnf
[root@server2 ~]# cat /etc/my.cnf | tail -n 3
server-id=2
gtid_mode=ON
enforce-gtid-consistency=true
[root@server2 ~]# /etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
[root@server2 ~]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.17 MySQL Community Server (GPL)Copyright (c) 2000, 2016, 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.mysql> stop slave; //停止slave工作
Query OK, 0 rows affected (0.03 sec)mysql> change master to master_host='172.25.54.1',master_user='repl',master_password='5820hhXM!@#',MASTER_AUTO_POSITION = 1;
//使用gtid重新與master建立認(rèn)證
Query OK, 0 rows affected, 2 warnings (0.52 sec)mysql> start slave;
Query OK, 0 rows affected (0.03 sec)mysql> show slave status\G
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 172.25.54.1Master_User: replMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000004Read_Master_Log_Pos: 154Relay_Log_File: server2-relay-bin.000002Relay_Log_Pos: 367Relay_Master_Log_File: mysql-bin.000004Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 154Relay_Log_Space: 576Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1Master_UUID: 37ab8283-9ae8-11e8-ba69-525400d23507Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 1Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row in set (0.00 sec)mysql>
測試
在master上進(jìn)行數(shù)據(jù)庫操作
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| userlist |
+----------------+
1 row in set (0.00 sec)mysql> delete from userlist where username='user3';
Query OK, 1 row affected (0.12 sec)mysql> Bye
[root@server1 ~]#
在slave上邊驗證
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> select * from userlist;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 147 |
| user2 | 258 |
+----------+----------+
2 rows in set (0.00 sec)mysql> Bye
[root@server2 ~]#