标签:
首先安装mysql,在上文搭建lnmp环境中已经介绍了,这里就不在赘述。
初始化主从数据库
切换到mysql安装目录下:cd /app/local/mysql
./scripts/mysql_install_db --datadir=/app/local/data/var1 --user=mysql
./scripts/mysql_install_db --datadir=/app/local/data/var2 --user=mysql
编辑my.cnf配置文件
vi /etc/my.cnf
在最下面加入:
[mysqld_multi]
mysqld = /app/local/mysql/bin/mysqld_safe
mysqladmin = /app/local/mysql/bin/mysqladmin
user = root
#password = multipass
log = /app/local/data/mysqld_multi.log
[mysqld3307]
socket = /tmp/mysql3307.sock
port = 3307
pid-file = /tmp/mysql3307.pid
datadir = /app/local/data/var1
user = mysql
#log = /var/log/mysql1.log
log-bin = mysql-bin
binlog_do_db = m_test#要同步复制的数据库
server-id = 1
[mysqld3308]
socket = /tmp/mysql3308.sock
port = 3308
pid-file = /tmp/mysql3308.pid
datadir = /app/local/data/var2
user = mysql
#log = /var/log/mysql2.log
#log-bin = /var/lib/mysql2/mysql-bin-3308
replicate_do_db = m_test
server-id = 2
保存退出后重启mysql服务
通过netstat -nptl 查看mysql是否启动
启动数据库实例: mysqld_multi --defaults-extra-file=/etc/my.cnf start 3307
查看数据库实例状态: mysqld_multi --defaults-extra-file=/etc/my.cnf report
登录主库:mysql -uroot -S /tmp/mysql3307.sock -p
新建同步复制的用户:grant replication slave on *.* to ‘slave3307‘@‘127.0.0.1‘ identified by ‘3307‘;
flush privileges;#刷新权限
查看主从状态:show master status;
mysql> show master status;
+------------------+----------+---------------------------+------------------+-- -----------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | E xecuted_Gtid_Set |
+------------------+----------+---------------------------+------------------+-- -----------------+
| mysql-bin.000005 | 120 | m_test,m_test | | |
+------------------+----------+---------------------------+------------------+-- -----------------+
1 row in set (0.07 sec)
>exit;#退出主库
设置从库:mysql -uroot -S /tmp/mysql3308.sock -p
change master to master_host=‘127.0.0.1‘,master_port=3307,master_user=‘slave3307‘,master_password=‘3307‘,master_log_file=‘mysql-bin.000005‘,master_log_pos=120;
start slave;
有时候在start slave时会提示错误需要先stop slave;然后再change master....;start slave;
show slave status\G查看状态:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: slave3307
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 120
Relay_Log_File: mysql3308-relay-bin.000008
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: m_test,m_test
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: 120
Relay_Log_Space: 623
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
Master_UUID: 5f043771-991e-11e5-b3fc-000c296ef339
Master_Info_File: /app/local/data/var2/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.05 sec)
当以下两个参数是yes时说明同步成功了
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
在主库中新建m_test数据库,登录从库可看见m_test,在主库中通过向数据库m_test添加数据可同步到从库中去
标签:
原文地址:http://www.cnblogs.com/weblm/p/5161787.html