CentOS 6.4 系统下的MySQL的主从库配置

首先了解到一、 二
一、(MySQL下创建用户并且赋予权限)root用户创建yong用户的SQL语句
 CREATE USER ‘yong‘@‘localhost‘
 IDENTIFIED BY ‘yong2022‘;
 GRANT USAGE ON * . * TO ‘yong‘@‘localhost‘
 IDENTIFIED BY ‘yong2022‘ WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;
 
 FLUSH PRIVILEGES ;

二、(数据库登录)
1、后台启动mysql服务
/data/mysql/bin/mysqld_safe&
 【
     2、新安装数据库,设置mysql用户名密码
     /data/mysql/bin/mysqladmin -u root password ‘yong2022‘ 
 】  
3、用户登录数据库
/data/mysql/bin/mysql -u root -pyong2022


三、MySQL主从库配置
 
目的: 将192.168.73.31(主机)的root用户下的math和mathlog这两个database 备份到 192.168.73.32(从机)的root用户下,其他database不要求备份

1、在主机中创建用户repl, 一般不给该用户赋予访问root的权限
CREATE USER ‘repl‘@‘192.168.73.31‘
 IDENTIFIED BY ‘yong2022‘;
2、改变主机的mysql配置文件:/data/mysql/etc/my.cnf【其中和主从相关语句已用 "# ****" 标注】

   [client]
   port            = 3306
   socket          = /tmp/mysql.sock

   [mysqld]
   datadir         = /data/mysql/var
   port            = 3306
   socket          = /tmp/mysql.sock
   skip_external_locking
   key_buffer_size = 384M
   max_allowed_packet = 1M
   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
   thread_concurrency = 4
   log_bin_trust_function_creators = 1/

   # handler socket
   loose_handlersocket_port = 9998
   loose_handlersocket_port_wr = 9999
   loose_handlersocket_threads = 16
   loose_handlersocket_threads_wr = 1
   open_files_limit = 65535

   log-bin=mysql-bin
   server-id       = 1                                                            # ****
   read-only = 0
   binlog-do-db =math                                                      # ****
   binlog-do-db =mathlog                                                 # ****
   binlog-ignore-db=information_schema                        # **** 不需要备份的database
   binlog-ignore-db=test                                                   # **** 不需要备份的database
       
   # innodb
   innodb_data_home_dir = /data/mysql/var
   innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend
   innodb_log_group_home_dir = /data/mysql/var
   innodb_buffer_pool_size = 384M
   innodb_additional_mem_pool_size = 20M
   innodb_log_file_size = 100M
   innodb_log_buffer_size = 8M
   innodb_flush_log_at_trx_commit = 1
   innodb_lock_wait_timeout = 50

   [mysqldump]
   quick
   max_allowed_packet = 4M

   [mysql]
   no-auto-rehash

   [myisamchk]
   key_buffer_size = 256M
   sort_buffer_size = 256M
   read_buffer = 2M
   write_buffer = 2M

   [mysqlhotcopy]
   interactive-timeout

3、登录到主机mysql的root用户,执行以下语句使从库有访问repl的权限
mysql> GRANT REPLICATION SLAVE ON *.* TO ‘repl‘@‘192.168.73.31‘
 IDENTIFIED BY ‘yong2022‘;
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO ‘repl‘@‘192.168.73.31‘
IDENTIFIED BY ‘yong2022‘;
执行语句:
mysql> show master status;
此时,记录下来File: xxxx 和  Position: yyyy 的值
4、改变从机的mysql配置文件:/data/mysql/etc/my.cnf 【其中和主从相关语句已用 "# ****" 标注】

   [client]
   port            = 3306
   socket          = /tmp/mysql.sock

   [mysqld]
   datadir         = /data/mysql/var
   port            = 3306
   socket          = /tmp/mysql.sock
   skip_external_locking
   key_buffer_size = 384M
   max_allowed_packet = 1M
   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
   thread_concurrency = 4
   log_bin_trust_function_creators = 1/

   # handler socket
   loose_handlersocket_port = 9998
   loose_handlersocket_port_wr = 9999
   loose_handlersocket_threads = 16
   loose_handlersocket_threads_wr = 1
   open_files_limit = 65535

   log-bin=mysql-bin
   server-id = 2                                                                  # ****区别于主机
   master-host=192.168.73.31                                       # ****
   master-user=repl                                                         # ****
   master-password=yong2022                                     # ****
   master-port=3306                                                       # ****
   master-connect-retry=60                                            # ****
   replicate-do-db=math                                                 # ****
   replicate-do-db=mathlog                                            # ****
   replicate-ignore-db=information_schema                # ****
   replicate-ignore-db=test                                             # ****

   # innodb
   innodb_data_home_dir = /data/mysql/var
   innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend
   innodb_log_group_home_dir = /data/mysql/var
   innodb_buffer_pool_size = 384M
   innodb_additional_mem_pool_size = 20M
   innodb_log_file_size = 100M
   innodb_log_buffer_size = 8M
   innodb_flush_log_at_trx_commit = 1
   innodb_lock_wait_timeout = 50

   [mysqldump]
   quick
   max_allowed_packet = 16M

   [mysql]
   no-auto-rehash

   [myisamchk]
   key_buffer_size = 256M
   sort_buffer_size = 256M
   read_buffer = 2M
   write_buffer = 2M

   [mysqlhotcopy]
   interactive-timeout

5、登录从机的root用户,执行以下语句
mysql> stop slave;
mysql> change master to master_host=‘192.168.73.31‘,
              master_user=‘repl‘,

              master_password=‘yong2022‘,
              master_log_file=‘xxxx‘,
              master_log_pos=yyyy;
mysql> start slave;
mysql> show slave status\G(这条SQl语句加";"会报错)
6、当    
   Slave_IO_Running: Yes
   Slave_SQL_Running: Yes
 表示主从连接建立成功。
7、测试:可在主机的math下做操作,到从库root用户下验证查看。
本文经过测试。
 参考博文:
 
http://www.cnblogs.com/wanglikai91/archive/2012/04/17/2454503.html
 http://www.cnblogs.com/yangligogogo/articles/1939938.html
 http://wangwei007.blog.51cto.com/68019/965575

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。