网站建设知识
Mysql主从复制安装笔记
2025-07-22 10:02  点击:0

Mysql主从复制安装笔记

卸载旧版本

查看MySQL

rpm -qa|grep mysql

卸载MySQL

rpm -e --nodeps mysql-libs-5.1.73-5.el6_6.x86_64

删除数据目录(如果有)

ls  -l  /var/lib|grep mysqlrm  -rf  /var/lib/mysql

下载并安装

安装环境是CentOS6,所以应该选用el6的安装包;CentOS7应该选用el7安装包。

下载mysql
python
wget dev.mysql/get/Downloads/MySQL-5.7/mysql-5.7.16-1.el6.x86_64.rpm-bundle.tar

解压

 tar -xvf mysql-5.7.16-1.el6.x86_64.rpm-bundle.tar

按照依赖关系依次安装rpm包
依赖关系依次为common→libs→client→server

rpm -ivh mysql-community-common-5.7.16-1.el6.x86_64.rpm rpm -ivh mysql-community-libs-5.7.16-1.el6.x86_64.rpm rpm -ivh mysql-community-client-5.7.16-1.el6.x86_64.rpm rpm -ivh mysql-community-server-5.7.16-1.el6.x86_64.rpm 

启动mysqld服务

cd /usr/sbin/service mysqld start

检查mysqld运行状态

service mysqld statusmysqld (pid  3558) is running...

查看临时登录密码

vim /var/log/mysqld.log2016-12-02T09:17:59.902923Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).2016-12-02T09:18:00.794353Z 0 [Warning] InnoDB: New log files created, LSN=457902016-12-02T09:18:01.057547Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.2016-12-02T09:18:01.196139Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 38e817fa-b870-11e6-a9dc-3417ebd14ee0.2016-12-02T09:18:01.228360Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.2016-12-02T09:18:01.228821Z 1 [Note] A temporary password is generated for root@localhost: l8u.+s3QMQh!

发现临时密码为 y2xpqZthtt)s
用户临时密码登陆mysql

mysql -uroot -p//设置validate_password_policy 0,1,2 级别低中高set global validate_password_policy=0; //设置validate_password_length 长度set global validate_password_length=4; //修改root密码alter user 'root'@'localhost' identified by 'root@bi';mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || sys                |+--------------------+4 rows in set (0.00 sec)show tables;+---------------------------+| Tables_in_mysql           |+---------------------------+| columns_priv              || db                        || engine_cost               || event                     || func                      || general_log               || gtid_executed             || help_category             || help_keyword              || help_relation             || help_topic                || innodb_index_stats        || innodb_table_stats        || ndb_binlog_index          || plugin                    || proc                      || procs_priv                || proxies_priv              || server_cost               || servers                   || slave_master_info         || slave_relay_log_info      || slave_worker_info         || slow_log                  || tables_priv               || time_zone                 || time_zone_leap_second     || time_zone_name            || time_zone_transition      || time_zone_transition_type || user                      |+---------------------------+31 rows in set (0.00 sec)

mysql主从复制配置

修改主服务器master:

  vi /etc/my.cnf       [mysqld]       log-bin=mysql-bin   //[必须]启用二进制日志       server-id=222      //[必须]服务器唯一ID,默认是1,一般取IP最后一段

修改从服务器slave:

   vi /etc/my.cnf       [mysqld]       log-bin=mysql-bin   //[不是必须]启用二进制日志       server-id=226      //[必须]服务器唯一ID,默认是1,一般取IP最后一段

重启两台服务器的mysql

/etc/init.d/mysqld restart

在主服务器上建立帐户并授权slave:

GRANT REPLICATION SLAVE ON *.* to 'bi_user'@'%' identified by 'bi123456';

登录主服务器的mysql,查询master的状态

show master status;+------------------+----------+--------------+------------------+-------------------+| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+------------------+-------------------+| mysql-bin.000001 |      440 |              |                  |                   |+------------------+----------+--------------+------------------+-------------------+1 row in set (0.00 sec)

配置从服务器Slave:

change master to master_host='10.1.33.102',master_user='bi_user',master_password='bi123456',master_log_file='mysql-bin.000004',master_log_pos=440; 

启动从服务器复制功能

start slave;   

在主服务器上授权

grant all privileges on *.* to bi_user@'%'