Mysql多数据源 一(主从同步)

 mysql版本:Mysql 5.7配置

1:复制mysql文件到别的位置

   由于下载的mysql默认情况下没有my-default.ini配置文件所以我从网上复制了一份

   如下:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# server_id = .....
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# bin log file name of the log to create
# log_bin= .....
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

2:修改复制后mysql的配置文件(把my-default.ini改成my.ini),并在复制后的的根目录下新建data目录

3:修改my.ini配置文件

4:在命令行以管理员身份输入以下两句命令

      ->mysqld -initialize -insecure -user=mysql会发现data目录下多一些文件

    -> 为了保险起见把主服务器的data目录下的目录复制过来

  ->执行mysqld install  ”名字“  把该数据库同步到电脑服务中(多了一个mysql-3309)

   

 5:启动mysql-3309看图形化界面能否打开

6:打开之后显示表不存在(表信息是存在mysql中的ibdata1中)所以复制整个原文件的data目录复制过来

        在这里,我把全部的data目录复制过来会报错,查看日志发现里面的uuid相同

        所有在后面我就只复制了数据库和数据库中的表信息(数据库中表的信息存在data目录下的ibdata1文件下,所以把这个文件复制过来就行 )

再次打开就存在了

开始主从同步(通过二进制log日志来同步的,主数据库发生修改,添加,删除操作后会产生二进制log日志,从数据库通过同步二进制log日志,来完成主从同步,又称主从复制)

7:打开主文件下的my.init,修改配置

    ->1:server-id (主从数据库的该属性不能重复,是你这个mysql服务器的唯一标识)

   ->2:log-bin=mysql-bin(注意mysql-bin是一个存放二进制log文件的路径,我这里指定了一个mysql当前安装目录的mysql-bin文件夹)

   ->新添加一个binlog-do-db=longlong_bike(指定你需要复制的数据库)

8:打开从服务器的my.ini进行配置

   ->server-id

   ->log-bin

   ->添加一个replicate-do-db

   

9:打开图形化界面

在主数据库中进行查询

    -> show master status(记住position)

在从数据库中进行查询 

   ->change master to master_host='127.0.0.1',master_port=3306,master_user='root',master_password='admin',master_log_file='mysql-bin.000001',master_log_pos=154

   host:连接的主数据库,port:连接的主端口... pos:是上面查询出的position

   ->出现以上结果后,在执行start slave查询语句

执行show slave status语句 看Slave_IO_Running和Salve_SQL_Running是否为yes

 一个负责与主机的io通信,一个负责自己的slave mysql进程

最后校验是否同步

猜你喜欢

转载自blog.csdn.net/qq_36957587/article/details/84261259
今日推荐