在高访问量服务环境下,单机配置mysql服务将无法满足频繁高速的数据读写操作。一旦mysql出现问题造成数据丢失,无法恢复。因此,在mysql服务上启用主从备份功能,支持读写分离技术。最靠可的是搭建负载均衡分布式数据库系统,更加可靠、稳定。
192.168.213.135(master) 192.168.213.136(salver)
yum install mysql*
[liang@localhost Desktop]$ sudo service mysqld restart
[sudo] password for liang: Stopping mysqld: [ OK ] Initializing MySQL database: Installing MySQL system tables... OK Filling help tables... OK To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands: /usr/bin/mysqladmin -u root password 'new-password' /usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password' Alternatively you can run: /usr/bin/mysql_secure_installation which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers. See the manual for more instructions. You can start the MySQL daemon with: cd /usr ; /usr/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd /usr/mysql-test ; perl mysql-test-run.pl Please report any problems with the /usr/bin/mysqlbug script! [ OK ] Starting mysqld: [ OK ] [liang@localhost Desktop]$ sudo mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, 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>
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');
Query OK, 0 rows affected (0.00 sec)
mysql> quit
[liang@localhost Desktop]$ sudo mysql -uroot -p Enter password:
mysql> CREATE USER liang IDENTIFIED BY '123456';
mysql> create database liang;
mysql> grant all privileges on liang.* to liang@localhost identified by '123456';
[liang@localhost html]$ sudo mysql -uliang -p [sudo] password for liang:
Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, 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 | | liang | | test | +--------------------+ 3 rows in set (0.00 sec)
mysql> use liang;
mysql> create table osinfo (id int primary key,os char(20),ttl int check(ttl>0));
mysql> insert into osinfo(id,os,ttl) values(1,'win10',128); mysql> insert into osinfo(id,os,ttl) values(2,'ubuntu15',64);
mysql> select * from osinfo;
+----+----------+------+ | id | os | ttl | +----+----------+------+ | 1 | win10 | 128 | | 2 | ubuntu15 | 64 | +----+----------+------+ 2 rows in set (0.00 sec)
[root@localhost Desktop]# vi /etc/my.cnf
############################## server-id = 1 log-bin=mysql-bin binlog-do-db = liang binlog-ignore-db = mysql,test,information_schema ##############################
[root@localhost Desktop]# service mysqld restart
Stopping mysqld: [ OK ] Starting mysqld:
mysql> grant replication slave on *.* to 'liang'@'192.168.213.136' identified by '123456';
mysql> flush privileges;
[root@localhost Desktop]# mysql -h 192.168.213.135 -u liang -p
Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, 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>
[root@localhost Desktop]# vi /etc/my.cnf
################################# server-id=2 master-host= 192.168.213.135 master-port=3306 master-user=liang master-password=123456 replicate-do-db=liang master-retry-count = 999 master-connect-retry = 60 #################################
[root@localhost Desktop]# service mysqld restart
Stopping mysqld: [ OK ] Starting mysqld: [ OK ]
mysql> show master status;
ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 4 Current database: *** NONE *** +------------------+----------+--------------+-------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+-------------------------------+ | mysql-bin.000001 | 106 | liang | mysql,test,information_schema | +------------------+----------+--------------+-------------------------------+ 1 row in set (0.00 sec)
mysql> show slave status;
+-------------------------------------------------------+-----------------+-------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+ | Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | +-------------------------------------------------------+-----------------+-------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+ | Waiting to reconnect after a failed master event read | 192.168.213.135 | liang | 3306 | 60 | | 4 | mysqld-relay-bin.000001 | 4 | | No | Yes | liang | | | | | | 0 | | 0 | 0 | 106 | None | | 0 | No | | | | | | NULL | No | 0 | | 0 | | +-------------------------------------------------------+-----------------+-------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+ 1 row in set (0.00 sec)
Slave_SQL_Running、Slave_IO_Running 为 YES 即主从备份开启。但是这里Slave_IO_Running为 NO,所以现在主从备份并没有开启。两种原因造成当前错误:程序可能在slave上进行了写操作,也可能是slave机器重起后,事务回滚造成的,一般是后者。
mysql> show slave status\G;
*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.213.135 Master_User: liang Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 382 Relay_Log_File: mysqld-relay-bin.000004 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: liang 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: 382 Relay_Log_Space: 828 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)
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT iptables -A INPUT -p udp --dport 3306 -j ACCEPT
mysql> insert into osinfo(id,os,ttl) values(3,'win7',128);
mysql> select * from osinfo;
+----+----------+------+ | id | os | ttl | +----+----------+------+ | 2 | ubuntu15 | 64 | | 1 | win10 | 128 | | 3 | win7 | 128 | +----+----------+------+ 3 rows in set (0.00 sec)
mysql> select * from osinfo;
+----+----------+------+ | id | os | ttl | +----+----------+------+ | 2 | ubuntu15 | 64 | | 1 | win10 | 128 | | 3 | win7 | 128 | +----+----------+------+ 3 rows in set (0.00 sec)