mysql主从配置
1.安装、配置MySQL
2.启动mysql服务及同步主从服务器的数据 [root@webserver ~]# /etc/init.d/mysqld start Starting MySQL SUCCESS! [root@localhost ~]# /etc/init.d/mysqld start Starting MySQL SUCCESS! [root@localhost ~]# rsync -avLP [email protected]:/data/mysql/ /data/mysql/ //将主服务器上的数据与从服务器上的数据同步 3.配置主(master) [root@webserver ~]# vi /etc/my.cnf //修改或添加 server-id=1 log-bin=mysql-bin 同时也可以添加以下参数: binlog-do-db=db1,db2 #需要同步的库 binlog-ignore-db=db1,db2 #忽略不同步的库 [root@webserver ~]# /etc/init.d/mysqld restart //修改配置文件后重启服务 Shutting down MySQL.. SUCCESS! Starting MySQL.. SUCCESS! [root@webserver ~]# mysql -uroot //本机的mysql未设密码 mysql> grant replication slave on *.* to ‘repl‘@‘192.168.1.119‘ identified by ‘123456‘; //repl是为slave端设置的访问master端mysql数据的用户,密码为123456;192.168.1.119为slave的ip。 Query OK, 0 rows affected (0.06 sec) mysql> flush privileges; //刷新库,内存的数据写入磁盘 Query OK, 0 rows affected (0.00 sec) mysql> flush tables with read lock; //锁定数据库,此时不允许更改任何数据 Query OK, 0 rows affected (0.00 sec) mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000032 | 106 | | mysql | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) 4.设置从(slave) [root@localhost ~]# vi /etc/my.cnf server-id = 2 #这个数值不能和主一样 replicate-do-db=db1,db2 和 replicate-ignore-db=db1,db2 #意义同主的那两个可选参数,若主上配置过了则从就不用配置 [root@localhost ~]# /etc/init.d/mysqld restart //重启服务 Shutting down MySQL. SUCCESS! Starting MySQL. SUCCESS! [root@localhost ~]# mysql -uroot mysql> slave stop; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> change master to master_host=‘192.168.1.111‘, master_port=3306, master_user=‘repl‘, master_password=‘123456‘, master_log_file=‘mysql-bin.000032‘, master_log_pos=106; //MASTER_HOST:主服务器的IP。MASTER_USER:配置主服务器时建立的用户名。MASTER_PASSWORD:用户密码。MASTER_PORT:主服务器mysql端口,若为默认端口可省略。 master_log_file和master_log_pos要和主上一致。 Query OK, 0 rows affected (0.05 sec) mysql> slave start; 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.1.111 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000032 Read_Master_Log_Pos: 106 Relay_Log_File: localhost-relay-bin.000002 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000032 Slave_IO_Running: Yes //yes表示配置成功 Slave_SQL_Running: Yes //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: 106 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: 1 row in set (0.00 sec) ERROR: No query specified 5.测试主从: mysql> unlock tables; //主上数据库已锁,此为解锁 Query OK, 0 rows affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | aming | | mysql | | test | +--------------------+ 4 rows in set (0.05 sec) mysql> create database cangls; Query OK, 1 row affected (0.00 sec) mysql> use cangls; Database changed mysql> select database(); +------------+ | database() | +------------+ | cangls | +------------+ 1 row in set (0.00 sec) mysql> show tables; Empty set (0.00 sec) mysql> CREATE TABLE `pre_forum_post` ( -> `pid` int(10) unsigned NOT NULL, -> `fid` mediumint(8) unsigned NOT NULL DEFAULT ‘0‘, -> `tid` mediumint(8) unsigned NOT NULL DEFAULT ‘0‘, -> `first` tinyint(1) NOT NULL DEFAULT ‘0‘, -> `author` varchar(15) NOT NULL DEFAULT ‘‘, -> `authorid` mediumint(8) unsigned NOT NULL DEFAULT ‘0‘, -> `subject` varchar(80) NOT NULL DEFAULT ‘‘, -> `dateline` int(10) unsigned NOT NULL DEFAULT ‘0‘, -> `message` mediumtext NOT NULL, -> `useip` varchar(15) NOT NULL DEFAULT ‘‘, -> `port` smallint(6) unsigned NOT NULL DEFAULT ‘0‘, -> `invisible` tinyint(1) NOT NULL DEFAULT ‘0‘, -> `anonymous` tinyint(1) NOT NULL DEFAULT ‘0‘, -> `usesig` tinyint(1) NOT NULL DEFAULT ‘0‘, -> `htmlon` tinyint(1) NOT NULL DEFAULT ‘0‘, -> `bbcodeoff` tinyint(1) NOT NULL DEFAULT ‘0‘, -> `smileyoff` tinyint(1) NOT NULL DEFAULT ‘0‘, -> `parseurloff` tinyint(1) NOT NULL DEFAULT ‘0‘, -> `attachment` tinyint(1) NOT NULL DEFAULT ‘0‘, -> `rate` smallint(6) NOT NULL DEFAULT ‘0‘, -> `ratetimes` tinyint(3) unsigned NOT NULL DEFAULT ‘0‘, -> `status` int(10) NOT NULL DEFAULT ‘0‘, -> `tags` varchar(255) NOT NULL DEFAULT ‘0‘, -> `comment` tinyint(1) NOT NULL DEFAULT ‘0‘, -> `replycredit` int(10) NOT NULL DEFAULT ‘0‘, -> `position` int(8) unsigned NOT NULL AUTO_INCREMENT, -> PRIMARY KEY (`tid`,`position`), -> UNIQUE KEY `pid` (`pid`), -> KEY `fid` (`fid`), -> KEY `authorid` (`authorid`,`invisible`), -> KEY `dateline` (`dateline`), -> KEY `invisible` (`invisible`), -> KEY `displayorder` (`tid`,`invisible`,`dateline`), -> KEY `first` (`tid`,`first`) -> ) ENGINE=MyISAM DEFAULT CHARSET=gbk -> ; Query OK, 0 rows affected (0.04 sec) mysql> show tables; +------------------+ | Tables_in_cangls | +------------------+ | pre_forum_post | +------------------+ 1 row in set (0.01 sec) mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000032 | 1808 | | mysql | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) 从服务: mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | aming | | cangls | | mysql | | test | +--------------------+ 5 rows in set (0.04 sec) mysql> use cangls; Database changed mysql> show tables; +------------------+ | Tables_in_cangls | +------------------+ | pre_forum_post | +------------------+ 1 row in set (0.00 sec) mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.111 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000032 Read_Master_Log_Pos: 1808 Relay_Log_File: localhost-relay-bin.000002 Relay_Log_Pos: 1953 Relay_Master_Log_File: mysql-bin.000032 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: 1808 Relay_Log_Space: 2112 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: 1 row in set (0.01 sec) ERROR: No query specified
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。