从机器上安装 mysql:
[root@alexis-02 src]# wget http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz
[root@alexis-02 src]# tar xvf mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz
[root@alexis-02 src]# mv mysql-5.6.43-linux-glibc2.12-x86_64/ /usr/local/mysql
[root@alexis-02 mysql]# useradd mysql
[root@alexis-02 mysql]# mkdir /data/
[root@alexis-02 mysql]# mkdir /data/mysql/
[root@alexis-02 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql/
[root@alexis-02 mysql]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
[root@alexis-02 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@alexis-02 mysql]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql
[root@alexis-02 mysql]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@alexis-02 mysql]# ps aux|grep mysqld
root 7488 0.1 0.1 113312 1616 pts/0 S 16:56 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/arslinux-02.pid
mysql 7652 8.7 45.1 1302736 449792 pts/0 Sl 16:56 0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysql.log --pid-file=/data/mysql/arslinux-02.pid --socket=/tmp/mysql.sock
root 7676 0.0 0.0 112724 984 pts/0 R+ 16:57 0:00 grep --color=auto mysqld
[root@alexis-02 mysql]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6926/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 7061/master
tcp6 0 0 :::3306 :::* LISTEN 7652/mysqld
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::* LISTEN 6926/sshd
tcp6 0 0 ::1:25 :::* LISTEN 7061/master
[root@alexis-02 mysql]# chkconfig mysqld on
[root@alexis-02 mysql]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
安装过程参考:https://blog.51cto.com/11530642/2390722
[root@alexis-01 mysql]# vim /etc/my.cnf
server-id = 130
log_bin = alexis
[root@alexis-01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL... SUCCESS!
[root@alexis-01 ~]# ll /data/mysql/
总用量 110748
-rw-rw---- 1 mysql mysql 120 11月 1 17:35 arselinux.000001
-rw-rw---- 1 mysql mysql 19 11月 1 17:35 arselinux.index
-rw-rw---- 1 mysql mysql 128324 11月 1 17:35 arslinux-01.err
-rw-rw---- 1 mysql mysql 5 11月 1 17:35 arslinux-01.pid
-rw-rw---- 1 mysql mysql 56 10月 13 21:55 auto.cnf
-rw-rw---- 1 mysql mysql 12582912 11月 1 17:35 ibdata1
-rw-rw---- 1 mysql mysql 50331648 11月 1 17:35 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 11月 13 21:50 ib_logfile1
drwx------ 2 mysql mysql 4096 10月 21 22:49 mysql
drwx------ 2 mysql mysql 4096 10月 21 22:43 mysql2
drwx------ 2 mysql mysql 4096 10月 13 21:50 performance_schema
drwx------ 2 mysql mysql 6 10月 13 21:50 test
drwx------ 2 mysql mysql 324 10月 28 23:03 zrlog
[root@alexis-01 ~]# mysqldump -uroot -parslinux mysql > /tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.
[root@alexis-01 ~]# mysql -uroot -parslinux -e'create database arslinux'
Warning: Using a password on the command line interface can be insecure.
[root@alexis-01 ~]# mysql -uroot -parslinux arslinux < /tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.
mysql> grant replication slave on *.* to 'repl'@192.168.194.132 identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.01 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| arselinux.000001 | 655378 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
[root@alexis-01 ~]# mysqldump -uroot -parslinux mysql2 > /tmp/mysql2.sql
Warning: Using a password on the command line interface can be insecure.
[root@alexis-01 ~]# mysqldump -uroot -parslinux zrlog > /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.
[root@alexis-01 ~]# mysqldump -uroot -parslinux arslinux > /tmp/arslinux.sql
Warning: Using a password on the command line interface can be insecure.
[root@alexis-02 mysql]# vim /etc/my.cnf
server-id = 132
[root@alexis-02 mysql]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@alexis-02 mysql]# ll /data/mysql/
总用量 110608
-rw-rw---- 1 mysql mysql 5 11月 1 18:19 arslinux-02.pid
-rw-rw---- 1 mysql mysql 56 11月 1 16:56 auto.cnf
-rw-rw---- 1 mysql mysql 12582912 11月 1 18:19 ibdata1
-rw-rw---- 1 mysql mysql 50331648 11月 1 18:19 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 11月 1 16:51 ib_logfile1
drwx------ 2 mysql mysql 4096 11月 1 16:51 mysql
drwx------ 2 mysql mysql 4096 11月 1 16:51 performance_schema
drwx------ 2 mysql mysql 6 11月 1 16:48 test
[root@alexis-02 mysql]# scp 192.168.194.130:/tmp/*.sql /tmp/
arslinux.sql 100% 638KB 14.6MB/s 00:00
mysql2.sql 100% 632KB 14.2MB/s 00:00
mysql.sql 100% 638KB 13.6MB/s 00:00
zrlog.sql 100% 10KB 2.2MB/s 00:00
[root@alexis-02 mysql]# alias 'mysql=/usr/local/mysql/bin/mysql'
[root@alexis-02 mysql]# alias 'mysqldump=/usr/local/mysql/bin/mysqldump'
[root@alexis-02 mysql]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.43 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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> create database arslinux;
Query OK, 1 row affected (0.00 sec)
mysql> create database mysql2;
Query OK, 1 row affected (0.00 sec)
mysql> create database zrlog;
Query OK, 1 row affected (0.00 sec)
mysql> quit;
Bye
[root@alexis-02 mysql]# mysql -uroot arslinux < /tmp/arslinux.sql
[root@alexis-02 mysql]# mysql -uroot mysql2 < /tmp/mysql2.sql
[root@alexis-02 mysql]# mysql -uroot zrlog < /tmp/zrlog.sql
[root@alexis-02 mysql]# ll /data/mysql/
总用量 226264
drwx------ 2 mysql mysql 4096 11月 1 18:28 arslinux
-rw-rw---- 1 mysql mysql 5 11月 1 18:19 arslinux-02.pid
-rw-rw---- 1 mysql mysql 56 11月 1 16:56 auto.cnf
-rw-rw---- 1 mysql mysql 79691776 11月 1 18:29 ibdata1
-rw-rw---- 1 mysql mysql 50331648 11月 1 18:29 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 11月 1 16:51 ib_logfile1
drwx------ 2 mysql mysql 4096 11月 1 16:51 mysql
drwx------ 2 mysql mysql 4096 11月 1 18:28 mysql2
drwx------ 2 mysql mysql 4096 11月 1 16:51 performance_schema
drwx------ 2 mysql mysql 6 11月 1 16:48 test
drwx------ 2 mysql mysql 324 11月 1 18:29 zrlog
[root@alexis-02 mysql]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.43 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='192.168.194.130',master_user='repl',master_password='123456',master_log_file='arselinux.000001',master_log_pos=655378;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.194.130
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: arselinux.000001
Read_Master_Log_Pos: 655378
Relay_Log_File: arslinux-02-relay-bin.000002
Relay_Log_Pos: 283
Relay_Master_Log_File: arselinux.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: 655378
Relay_Log_Space: 462
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: 130
Master_UUID: c164593f-7586-11e9-a13a-000c2924eaf2
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
★★★ status中 Slave_IO_Running 和 Slave_SQL_Running 如果两个都是 Yes 表示配置主从成功
还需关注以下参数
Seconds_Behind_Master: 0 //为主从延迟的时间
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
[root@alexis-01 ~]# mysql -uroot -parslinux
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.6.43-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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> unlock tables;
Query OK, 0 rows affected (0.00 sec)
查看主从服务器两台机器 /data/mysql/auto.cnf 的UUID是否一样,如果一样,删除其中一个,并重启该服务器,再次进行从服务器 mysql 的 change master 操作就能成功了。
在 my.cnf 中定义:可以在主上,也可以在从上
主服务器上
binlog-do-db= //仅同步指定的库
binlog-ignore-db= //忽略指定库
从服务器上
replicate_do_db=
replicate_ignore_db=
replicate_do_table=
replicate_ignore_table=
replicate_wild_do_table= //如arslinux.%, 支持通配符%
replicate_wild_ignore_table=
最常用是最后两条
验证是否同步
主:
mysql> select count(*) user;
+------+
| user |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
从:
mysql> select count(*) user;
+------+
| user |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
主:
mysql> truncate table user;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from user;
Empty set (0.00 sec)
从:
mysql> select * from user;
Empty set (0.01 sec)
mysql> drop table user;
Query OK, 0 rows affected (0.00 sec)
从:
mysql> select * from user;
ERROR 1146 (42S02): Table 'arslinux.user' doesn't exist
如果主从出现错误,可以尝试从上 stop slave,再 start slave,如果还是有问题,那么就要重新做主从了
有人遇到主从不能正常同步,提示 uuid 相同的错误。这是因为克隆机器导致。
https://www.2cto.com/database/201412/364479.html