• mysql学习(3)-linux下mysql主从复制


    前言:为什么MySQL要做主从复制(读写分离)?
    通俗来讲,如果对数据库的读和写都在同一个数据库服务器中操作,业务系统性能会降低。
    为了提升业务系统性能,优化用户体验,可以通过做主从复制(读写分离)来减轻主数据库的负载。
    而且如果主数据库宕机,可快速将业务系统切换到从数据库上,可避免数据丢失。

    本文提纲

    一.主从复制的概念

    二.配置主从复制

    MySQL主从复制(读写分离)和集群的区别:

    一.主从复制相关概念

    (读写分离):一般需要两台及以上数据库服务器即可(一台用于写入数据,一台用于同步主的数据并用于数据查询操作)。
    局限性:
    (1)配置好主从复制之后,同一张表,只能对一个服务器写操作。如果在从上执行了写操作,而之后主也操作了这张表,或导致主从不同步;据说可以配置成主主方式,但我还没有研究到。
    (2)主数据库服务器宕机,需要手动将业务系统切换到从数据库服务器。无法做到高可用性(除非再通过部署keepalive做成高可用方案)。
    2.集群是由N台数据库服务器组成,数据的写入和查询是随机到任意一台数据库服务器的,其他数据库服务器会自动同步数据库的操作。
    任何一台数据库宕机,不会对整个集群造成大的影响。

    二.配置主从复制

    1.准备工作:
    (1)配置MySQL主从复制(读写分离)之前,需要在主从两台服务器先安装好MySQL5.6。
    (2)目前最新的MySQL5.6 GA版本是MySQL5.6.22
    操作系统:centos6.3

    之前有讲过源码包安装mysql5.6,这里不做解释:

    主数据库IP:192.168.100.241
    从数据库IP:192.168.100.242

    2.修改主从数据库的my.cnf文件

    先修改主配置文件模版 /etc/my.cnf(最好先备份原配置文件)

    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
    [client]
    port=3306
    socket=/var/lib/mysql/mysql-241.sock
    default-character-set=utf8
    
    [mysqld]
    explicit_defaults_for_timestamp
    sync_binlog=1
    server-id=1
    port=3306
    socket=/var/lib/mysql/mysql-241.sock
    pid-file=/var/lib/mysql/mysql-241.pid
    
    #user=mysql
    
    datadir=/usr/local/mysql/data
    log-bin=/usr/local/mysql/data/mysqlmaster-bin
    log-error=/usr/local/mysql/logs/error.log
    slow_query_log_file=/usr/local/mysql/logs/slow.log
    inlog_format=mixed
    slow_query_log
    long_query_time=10 
    wait_timeout=31536000
    interactive_timeout=31536000
    max_connections=500
    max_user_connections=490
    max_connect_errors=2
    character_set_server=utf8
    skip-external-locking
    key_buffer_size = 128M
    max_allowed_packet = 5M
    table_open_cache = 512
    sort_buffer_size = 2M
    read_buffer_size = 2M
    read_rnd_buffer_size = 8M
    myisam_sort_buffer_size = 64M
    thread_cache_size = 8
    query_cache_size = 32M
    # Try number of CPU's*2 for thread_concurrency
    #thread_concurrency = 4
    binlog-ignore-db=mysql
    binlog-ignore-db=information_schema
    replicate_ignore_db=mysql
    replicate_ignore_db=information_schema
    expire-logs-days=10
    skip-slave-start
    skip-name-resolve
    lower_case_table_names=1
    log_bin_trust_function_creators=1
    
    # InnoDB
    innodb_data_home_dir=/usr/local/mysql/data
    innodb_log_group_home_dir=/usr/local/mysql/logs
    innodb_data_file_path=ibdata1:128M:autoextend
    innodb_buffer_pool_size=2G
    innodb_log_file_size=10M
    innodb_log_buffer_size=8M
    innodb_lock_wait_timeout=50
    innodb_file_per_table
    innodb_flush_log_at_trx_commit=1
    
    #sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
    sql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_AUTO_VALUE_ON_ZERO
    
    [mysqldump]
    quick
    max_allowed_packet = 16M
    
    [mysql]
    no-auto-rehash
    
    [myisamchk]
    key_buffer_size = 256K
    sort_buffer_size = 256K
    read_buffer = 256K
    write_buffer = 256K
    
    [mysqlhotcopy]
    interactive-timeout

    3.从配置文件模版

    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
    [client]
    port=3306
    socket=/var/lib/mysql/mysql-242.sock
    default-character-set=utf8
    
    [mysqld]
    sync_binlog=1
    server-id=2
    port=3306
    socket=/var/lib/mysql/mysql-242.sock
    pid-file=/var/lib/mysql/mysql-242.pid
    user=mysql
    datadir=/usr/local/mysql/data
    tmpdir=/usr/local/mysql/temp/
    log-bin=/usr/local/mysql/data/mysqlslave-bin
    log-error=/usr/local/mysql/logs/error.log
    slow_query_log_file=/usr/local/mysql/logs/slow.log
    binlog_format=mixed
    slow_query_log
    long_query_time=10 
    wait_timeout=31536000
    interactive_timeout=31536000
    max_connections=500
    max_user_connections=490
    max_connect_errors=2
    character_set_server=utf8
    skip-external-locking
    key_buffer_size = 128M
    max_allowed_packet = 5M
    table_open_cache = 512
    sort_buffer_size = 2M
    read_buffer_size = 2M
    read_rnd_buffer_size = 8M
    myisam_sort_buffer_size = 64M
    thread_cache_size = 8
    query_cache_size = 32M
    # Try number of CPU's*2 for thread_concurrency
    thread_concurrency = 4
    binlog-ignore-db=mysql
    binlog-ignore-db=information_schema
    replicate_ignore_db=mysql
    replicate_ignore_db=information_schema
    expire-logs-days=10
    #skip-slave-start
    skip-name-resolve
    lower_case_table_names=1
    log_bin_trust_function_creators=1
    
    # InnoDB
    innodb_data_home_dir=/usr/local/mysql/data
    innodb_log_group_home_dir=/usr/local/mysql/logs
    innodb_data_file_path=ibdata1:128M:autoextend
    innodb_buffer_pool_size=2G
    innodb_log_file_size=10M
    innodb_log_buffer_size=8M
    innodb_lock_wait_timeout=50
    innodb_file_per_table
    innodb_flush_log_at_trx_commit=1
    
    #sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
    sql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_AUTO_VALUE_ON_ZERO
    
    [mysqldump]
    quick
    max_allowed_packet = 16M
    
    [mysql]
    no-auto-rehash
    
    [myisamchk]
    key_buffer_size = 256K
    sort_buffer_size = 256K
    read_buffer = 256K
    write_buffer = 256K
    
    [mysqlhotcopy]
    interactive-timeout

    修改之后要重启mysql:

    /etc/init.d/mysql restart 或者service mysql restart (前提是安装了服务)

    4.解决配置过程中遇到的问题

    注意:修改my.cnf文件之后重启服务一般会遇到问题

    MySQL: Starting MySQL….. ERROR! The server quit without updating PID file
    1.问题
    [root@localhost mysql]# /etc/rc.d/init.d/mysql status MySQL is not running, but lock file (/var/lock/subsys/mysql[FAILED] [root@localhost mysql]# /etc/rc.d/init.d/mysql start Starting MySQL...The server quit without updating PID file (/usr/local/mysql/data/localhost.localdomain.pid). [FAILED] 2.原因 没有初始化权限表

    [root@localhost ~]# cd /usr/local/mysql

    
    

    [root@localhost mysql]# chown -R mysql.mysql .
    [root@localhost mysql]# su - mysql
    [mysql@localhost ~]$ cd /usr/local/mysql
    [mysql@localhost mysql]$ scripts/mysql_install_db
    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:

    ./bin/mysqladmin -u root password 'new-password'
    ./bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

    
    

    Alternatively you can run:
    ./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 . ; ./bin/mysqld_safe &

    
    

    You can test the MySQL daemon with mysql-test-run.pl
    cd ./mysql-test ; perl mysql-test-run.pl

    
    

    Please report any problems with the ./bin/mysqlbug script!

    
    

    [mysql@localhost mysql]$ /usr/local/mysql/bin/mysqld_safe --user=mysql &
    [1] 11767

    [mysql@localhost mysql]$ 120502 07:01:17 mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
    120502 07:01:17 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
    [mysql@localhost mysql]$ /etc/rc.d/init.d/mysql status
    MySQL running (11830)                                      [  OK  ]

    [mysql@localhost mysql]$ /etc/rc.d/init.d/mysql start
    Starting MySQL                                             [  OK  ]

    注意:master和slave的server-id一定不能相同,这里master:server-id=1,slave:server-id=2

    配置时因为粗心将两个server-id=1,查了好多资料才解决。

    5.设置主从同步

    登录到主数据库:
    (1).在主数据库上创建用于主从复制的账户(192.168.100.242换成你的从数据库IP):

    GRANT REPLICATION SLAVE ON *.* TO 'java'@'192.168.100.242' IDENTIFIED BY 'java@123456';

    (2).主数据库锁表(禁止再插入数据以获取主数据库的的二进制日志坐标):

    FLUSH TABLES WITH READ LOCK;

    (3).打开MySQL命令行:

     
    #mysql -uroot -p
    mysql>
     SHOW MASTER STATUS;
    
    +------------------------+----------+--------------+------------------+-------------------+
    
    |
     File                   | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    
    +------------------------+----------+--------------+------------------+-------------------+
    
    |
     mysqlmaster-bin.000002 |      120 |              |                  |                   |
    +------------------------+----------+--------------+------------------+-------------------+
     row in set (0.00 sec)
    
    mysql>exit;

    查询结果显示:在这个例子中,二进制日志文件是mysqlmaster-bin.000002,位置是120,记录下这两个值,稍后要用到。

    (4).在主数据库上使用mysqldump命令备份一下数据库:

    mysqldump -uroot -p jobs > /usr/local/mysql/jobs.sql

    SSH登录到从数据库:

    (1).通过FTP、SFTP或其他方式,将上一步备份的主数据库快照jobs.sql上传到从数据库某个路径,例如我放在了/user/local/mysql目录下;
    (2).从导入主库的备份:

    #创建数空据库
    CREATE DATABASE `jobs` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
    #mysqldump 恢复数据
    mysql -uroot -p -B jobs </usr/local/mysql/jobs.sql

    (3).给从数据库设置复制的主数据库信息(注意修改MASTER_LOG_FILE和MASTER_LOG_POS的值)

    #停止从数据库的复制线程
    stop slave;
    #给从数据库设置复制的主数据库信息
    CHANGE MASTER TO MASTER_HOST='192.168.100.241',MASTER_USER='java',MASTER_PASSWORD='java@1228',MASTER_LOG_FILE='mysqlmaster-bin.000002',
    MASTER_LOG_POS=120;
    #启动从数据库的复制线程
    start slave;

    (4).接着查询数据库的slave状态:

    mysql> SHOW slave STATUS G
    
    #如果下面两个参数都是Yes,则说明主从配置成功!
    
    Slave_IO_Running:Yes
    
    Slave_SQL_Running:Yes

    (4).接下来你可以在主数据库上创建数据库、表、插入数据,然后看从数据库是否同步了这些操作.

     可能会遇到 "slave_io_running: no"问题

    解决办法

    mysql>stop slave ; 
    mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1; 
    mysql>start slave ; 

    然后在查看主从状态

    mysql> SHOW slave STATUS G
    
    #如果下面两个参数都是Yes,则说明主从配置成功!
    
    Slave_IO_Running:Yes
    
    Slave_SQL_Running:Yes
  • 相关阅读:
    分享一道关于类、实例加载和初始化顺序的基础面试题
    IDEA部署 java Web项目 常见配置
    jsp和servlet开发过程中参数传递乱码问题总结
    Java String引起的常量池、String类型传参、“==”、“equals”、“hashCode”问题 细节分析
    利用反射创建实例强制转换为接口失败小结
    oracle与sqlserver的十大区别
    js闭包的用途 【转】
    实体框架EF笔记
    利用存储过程进行分页
    关于ASP.NET运行机制原理。。。个人总结
  • 原文地址:https://www.cnblogs.com/weiguo21/p/4776537.html
Copyright © 2020-2023  润新知