search
HomeDatabaseMysql Tutorialmysql5.1.73配置主从服务器_MySQL

一、安装MySQL

这里就详解,请度娘。

二、配置MySQL主服务器(10.241.226.110)

 

mysql -uroot -p #进入MySQL控制台
create database testdb; #建立数据库testdb

#授权用户mysqlcopy只能从10.241.226.111这个IP访问主服务器10.241.226.110上面的数据库,并且只具有数据库备份的权限
grant replication slave on *.* to 'mysqlcopy'@'10.241.226.111' identified by '123456' with grant option;

 

三、把MySQL主服务器10.241.226.110中的数据库testdb导入到MySQL从服务器10.241.226.111中

1、导出数据库testdb

mysqldump -u root -p testdb> /home/testdbbak.sql

备注:在导出之前可以先进入MySQL控制台执行下面命令

flush tables with read lock; #数据库只读锁定命令,防止导出数据库的时候有数据写入

unlock tables; #解除锁定

2、导入数据库到MySQL从服务器

mysql -u root -p #进入从服务器MySQL控制台

create databasetestdb; #创建数据库

usetestdb #进入数据库

source /home/testdbbak.sql #导入备份文件到数据库

mysql -u mysqlcopy-h10.241.226.110 -p #测试在从服务器上登录到主服务器

 

四、配置MySQL主服务器的my.cnf文件

1、vim /etc/my.cnf #编辑配置文件,在[mysqld]部分添加下面内容

log_bin=mysql-bin  #启动MySQ二进制日志系统,注意:如果原来的配置文件中已经有这一行,就不用再添加了。
binlog-do-db=testdb   #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行
binlog-ignore-db=mysql    #不同步mysql系统数据库
server_id = 1   #设置服务器id,为1表示主服务器,注意:如果原来的配置文件中已经有这一行,就不用再添加了。
socket =/var/lib/mysql/mysql.sock
character-set-server=utf8

2、service mysqld restart #重启MySQL
3、mysql -u root -p #进入mysql控制台
4、show master status; 查看主服务器,出现以下类似信息
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 120 | testdb | mysql |
+------------------+----------+--------------+------------------+

注意:这里记住File的值:mysql-bin.000001和Position的值:120,后面会用到。

五、配置MySQL从服务器的my.cnf文件

1、vi /etc/my.cnf #编辑配置文件,在[mysqld]部分添加下面内容

 

log_bin=mysql-bin   #启动MySQ二进制日志系统,注意:如果原来的配置文件中已经有这一行,就不用再添加了。
replicate-do-db=testdb
replicate-ignore-db=mysql
read_only=1
server_id = 2    #配置文件中已经有一行server-id=1,修改其值为2,表示为从数据库
socket = /var/lib/mysql/mysql.sock
character-set-server=utf8 

 

2、service mysqld restart #重启MySQL

3、mysql -u root -p #进入MySQL控制台

4、slave stop; #停止slave同步进程

5、change master to master_host='10.241.226.110',master_user='mysqlcopy',master_password='123456',master_log_file='mysql-bin.000001,master_log_pos=120; #执行同步语句

6、slave start; #开启slave同步进程

7、SHOW SLAVE STATUS\G #查看slave同步信息,部分内容如下:

 

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.241.226.111
                  Master_User: mysqlcopy
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 377
               Relay_Log_File: localhost-relay-bin.000003
                Relay_Log_Pos: 540
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: DB_CLOUDCORE
          Replicate_Ignore_DB: mysql
           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: 377
              Relay_Log_Space: 717
              Until_Condition: None
               Until_Log_File:

 

注意查看:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

以上这两个参数的值为Yes,即说明配置成功!

六、测试MySQL主从服务器双机热备是否成功

1、进入MySQL主服务器

mysql -u root -p #进入主服务器MySQL控制台

use testdb; #进入数据库

CREATE TABLE test ( id int not null); #创建test表

2、进入MySQL从服务器

mysql -u root -p #进入MySQL控制台

usetestdb; #进入数据库

show tables;

会看到有一个新建的表test,表示数据库同步成功

 

问题:

1、ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'

解:由于mysql 默认的mysql.sock 是在/var/lib/mysql/mysql.sock,但linux系统总是去/tmp/mysql.sock查找,所以会报错

为mysql.sock增加软连接(相当于windows中的快捷方式)。
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft