Mysql主從複製,前段時間生產環境部署了一套主從複製的架構,當時現找了不少資料,如今記錄下mysql
主機名 | IP | 操做系統 | 角色 | 軟件版本 |
MysqlDB_Master | 192.168.0.1 | CentOS release 7.1 | Master | Mysql 5.6.36 |
MysqlDB_Slave | 192.168.0.2 | CentOS release 7.1 | Slave | Mysql 5.6.36 |
以前都是yum直接安裝的,此次嘗試使用源碼安裝,安裝包以下:sql
首先安裝前提環境:數據庫
[root@MysqlDB_Master soft] yum groupinstall 'Development tools' -y [root@MysqlDB_Master soft] tar -xf cmake-3.8.1.tar.gz [root@MysqlDB_Master soft] cd cmake-3.8.1 [root@MysqlDB_Master cmake-3.8.1] ./bootstrap [root@MysqlDB_Master cmake-3.8.1] gmake && gmake install
安裝mysql:bootstrap
[root@MysqlDB_Master soft] tar -xf mysql-5.6.36.tar.gz [root@MysqlDB_Master soft] cd mysql-5.6.36 [root@MysqlDB_Master mysql-5.6.36] cmake -DCMAKE_INSTALL_PREFIX=/data/opt/mysql -DMYSQL_DATADIR=/data/opt/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1 [root@MysqlDB_Master mysql-5.6.36] make && make install
mysql配置相關:vim
[root@MysqlDB_Master mysql-5.6.36] mkdir -p /data/opt/mysql/{3306,binlog,tmp} [root@MysqlDB_Master mysql-5.6.36] chown -R mysql.mysql /data/opt/mysql [root@MysqlDB_Master mysql-5.6.36] cp support-files/my-default.cnf /etc/my.conf [root@MysqlDB_Master mysql-5.6.36] vim /etc/my.conf =================================== [client] port = 3306 socket = /data/opt/mysql/3306/mysql.sock #The MySQL Server [mysqld] server-id=209 innodb_flush_log_at_trx_commit=1 sync_binlog=1 port = 3306 user = mysql socket = /data/opt/mysql/3306/mysql.sock pid-file = /data/opt/mysql/3306/mysql.pid basedir = /data/opt/mysql datadir = /data/opt/mysql/data tmpdir = /data/opt/mysql/tmp open_files_limit = 10240 lower_case_table_names=1 explicit_defaults_for_timestamp sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES #Buffer max_allowed_packet = 256M max_heap_table_size = 256M net_buffer_length = 8k sort_buffer_size = 2M join_buffer_size = 4M read_buffer_size = 2M read_rnd_buffer_size = 16M #Log log-bin = /data/opt/mysql/binlog/mysql-bin binlog_cache_size = 32M max_binlog_cache_size = 512M max_binlog_size = 512M binlog_format = mixed log_output = FILE log-error = /data/opt/mysql/binlog/mysql-error.log slow_query_log = 1 slow_query_log_file = /data/opt/mysql/binlog/slow_query.log general_log = 0 general_log_file = /data/opt/mysql/binlog/general_query.log expire-logs-days = 14 #InnoDB innodb_data_file_path = ibdata1:2048M:autoextend innodb_log_file_size = 256M innodb_log_files_in_group = 3 innodb_buffer_pool_size = 1024M [mysql] no-auto-rehash prompt = (\u@\h)[\d]>\_ default-character-set = gbk =================================== #添加mysql環境變量 [root@MysqlDB_Master mysql-5.6.36] echo 'export PATH=$PATH:/data/opt/mysql/bin' >> /etc/profile && source /etc/profile #初始化數據庫 [root@MysqlDB_Master mysql-5.6.36] cd /data/opt/mysql [root@MysqlDB_Master mysql] ./scripts/mysql_install_db --defaults-file=/etc/my.cnf --user=mysql [root@MysqlDB_Master mysql] ./bin/mysqld_safe --user=mysql & #添加service,並配置開機啓動 [root@MysqlDB_Master mysql] cp /soft/mysql-5.6.36/support-files/mysql.server /etc/init.d/mysqld [root@MysqlDB_Master mysql] chmod +x /ect/init.d/mysqld
[root@MysqlDB_Master mysql] chkconfig --add mysqld
[root@MysqlDB_Master mysql] chkconfig mysqld on
優化mysql默認配置,配置訪問權限:
服務器
[root@MysqlDB_Master ~] mysql -uroot -p #清空默認數據 (root@localhost)[(none)] > select * from mysql.db \G (root@localhost)[(none)] > truncate table mysql.db; (root@localhost)[(none)] > flush privileges; (root@localhost)[(none)] > select * from mysql.db \G #配置訪問權限 (root@localhost)[(none)] > use mysql; (root@localhost)[(none)] > desc user; (root@localhost)[(none)] > grant all privileges on *.* to root@"%" identified by "root"; (root@localhost)[(none)] > update user set Password=password('XXXXXX') where User='root'; (root@localhost)[(none)] > select Host,User,Password from user where User='root'; (root@localhost)[(none)] > flush privileges; (root@localhost)[(none)] > exit;
以上,Mysql源碼安裝完成,在MysqlDB_Slave服務器上進行相同配置便可。架構
關於主從的配置,以前在配置文件裏已經填寫,「server-id」值主從不一樣,從值大於主值,主要配置以下:socket
[mysqld] server-id=209 #InnoDB innodb_flush_log_at_trx_commit=1 sync_binlog=1
鏈接到主庫,配置同步帳號並受權從服務器鏈接:ide
(root@localhost)[(none)] grant replication slave,reload,super on *.* to 'repl'@'192.168.0.2' identified by 'backup'; (root@localhost)[(none)] flush tables with read lock; (root@localhost)[(none)] show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000007 | 321| | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
鏈接到從庫,配置主服務器IP以及同步帳號等信息:優化
(root@localhost)[(none)] > change master to master_host='192.168.0.1',master_user='repl',master_password='backup',master_log_file='mysql-bin.000007',master_log_pos=321; (root@localhost)[(none)] > start slave; (root@localhost)[(none)] > show slave status \G
確認主從節點同步正常,主要確認如下值:
Master_Log_File: mysql-bin.000007 Relay_Master_Log_File: mysql-bin.000007 Slave_IO_Running: Yes Slave_SQL_Running: Yes
鏈接到主庫,解除鎖,並建立新DB:
(root@localhost)[(none)] unlock tables;
(root@localhost)[(none)] create database testDB;
鏈接到從庫,查看新建DB是否同步過來:
(root@localhost)[(none)] show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| testDB |
+--------------------+
以上,Mysql集羣,主從同步配置完成。
1.Mysql5.7安裝須要引入boost庫
tar -xf boost_1_59_0.tar.gz -C /usr/local/ cd /usr/local/ ln -s boost_1_59_0 boots cmake -DCMAKE_INSTALL_PREFIX=/data/opt/mysql -DMYSQL_DATADIR=/data/opt/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1 -DENABLE_DOWNLOADS=1 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost make -j `grep processor /proc/cpuinfo | wc -l` && make install
2.Mysql5.7密碼字段改變
5.6是Password,5.7是authentication_string
3.Mysql5.7修改密碼
update user set authentication_string=password('XXXXXX') where User='root';
4.Mysql5.7初始化
data目錄不須要手動創建:
./bin/mysqld --initialize-insecure --user=mysql --basedir=/data/opt/mysql --datadir=/data/opt/mysql/data
5.Mysql5.7默認日誌時間是UTC
在配置文件中[mysqld]段落中添加log_timestamps=SYSTEM