2018-11-07 阿里云服务器搭建mysql主从复制

 首先我们登录远程阿里云服务器:

Connecting to 47.99.237.97:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Wed Nov  7 13:27:14 2018 from 183.238.79.9

Welcome to Alibaba Cloud Elastic Compute Service !


[root@ape-bz2009 src]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:16:3e:0c:2c:94 brd ff:ff:ff:ff:ff:ff
    inet 192.168.118.170/24 brd 192.168.118.255 scope global dynamic eth0
       valid_lft 315296287sec preferred_lft 315296287sec

连接我们的主mysql服务器:

[root@ape-bz2 ~]# ssh 192.168.118.170
[email protected]'s password: 
Last login: Wed Nov  7 13:26:55 2018 from 192.168.118.173

Welcome to Alibaba Cloud Elastic Compute Service !

[root@ape-bz2009 ~]# ls 
[root@ape-bz2009 ~]# w
 13:32:34 up 17:33,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.118.173  13:26    5:38   0.01s  0.01s -bash
root     pts/1    192.168.118.173  13:32    2.00s  0.01s  0.00s w

安装基础环境包:

[root@ape-bz2009 ~]# yum -y install perl perl-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package 4:perl-5.16.3-292.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package perl-devel.x86_64 4:5.16.3-292.el7 will be installed
--> Processing Dependency: systemtap-sdt-devel for package: 4:perl-devel-5.16.3-292.el7.x86_64
--> Processing Dependency: perl(ExtUtils::ParseXS) for package: 4:perl-devel-5.16.3-292.el7.x86_64
--> Processing Dependency: perl(ExtUtils::MakeMaker) for package: 4:perl-devel-5.16.3-292.el7.x86_64
--> Processing Dependency: perl(ExtUtils::Installed) for package: 4:perl-devel-5.16.3-292.el7.x86_64
--> Processing Dependency: libdb-devel for package: 4:perl-devel-5.16.3-292.el7.x86_64
--> Processing Dependency: gdbm-devel for package: 4:perl-devel-5.16.3-292.el7.x86_64
--> Running transaction check
---> Package gdbm-devel.x86_64 0:1.10-8.el7 will be installed
---> Package libdb-devel.x86_64 0:5.3.21-24.el7 will be installed

[root@ape-bz2009 ~]# yum install -y perl-Data-Dumper
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package perl-Data-Dumper.x86_64 0:2.145-3.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

1.安装mysql 5.6

1.1 先解压mysql5.6 

[root@ape-bz2009 src]# tar xzvf mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz 
mysql-5.6.39-linux-glibc2.12-x86_64/mysql-test/suite/engines/funcs/r/in_string_unique_constraint_error.result
mysql-5.6.39-linux-glibc2.12-x86_64/mysql-test/suite/engines/README
mysql-5.6.39-linux-glibc2.12-x86_64/mysql-test/suite/engines/iuds/t/hindi.txt
mysql-5.6.39-linux-glibc2.12-x86_64/mysql-test/suite/engines/iuds/t/insert_time.test
mysql-5.6.39-linux-glibc2.12-x86_64/mysql-test/suite/engines/iuds/t/delete_year.test
mysql-5.6.39-linux-glibc2.12-x86_64/mysql-test/suite/engines/iuds/t/update_decimal.test
mysql-5.6.39-linux-glibc2.12-x86_64/mysql-test/suite/engines/iuds/t/update_delete_number.test
mysql-5.6.39-linux-glibc2.12-x86_64/mysql-test/suite/engines/iuds/t/type_bit_iuds.test
mysql-5.6.39-linux-glibc2.12-x86_64/mysql-test/suite/engines/iuds/t/update_time.test
mysql-5.6.39-linux-glibc2.12-x86_64/mysql-test/suite/engines/iuds/t/insert_calendar.test
mysql-5.6.39-linux-glibc2.12-x86_64/mysql-test/suite/engines/iuds/t/delete_decimal.test
mysql-5.6.39-linux-glibc2.12-x86_64/mysql-test/suite/engines/iuds/t/strings_charsets_update_delete.test
mysql-5.6.39-linux-glibc2.12-x86_64/mysql-test/suite/engines/iuds/t/disabled.def
mysql-5.6.39-linux-glibc2.12-x86_64/mysql-test/suite/engines/iuds/t/delete_time.test
mysql-5.6.39-linux-glibc2.12-x86_64/mysql-test/suite/engines/iuds/t/strings_update_delete.test
mysql-5.6.39-linux-glibc2.12-x86_64/mysql-test/suite/engines/iuds/t/insert_number.test
mysql-5.6.39-linux-glibc2.12-x86_64/mysql-test/suite/engines/iuds/t/sample.txt
.......................................................................

[root@ape-bz2009 src]# ls
apache-tomcat-8.5.34.tar.gz                           mysql-5.6.39-linux-glibc2.12-x86_64         percona-release-0.1-4.noarch.rpm
jdk-8u191-linux-x64.tar.gz                            mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz  php-5.6.32.tar.bz2
Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz  nginx-1.14.0.tar.gz

 开始安装mysql:

[root@ape-bz2009 src]# mv mysql-5.6.39-linux-glibc2.12-x86_64 /usr/local/mysql


[root@ape-bz2009 mysql]# ls
bin  COPYING  data  docs  include  lib  man  mysql-test  README  scripts  share  sql-bench  support-files

[root@ape-bz2009 mysql]# adduser mysql
[root@ape-bz2009 mysql]# mkdir /data/

报错:

[root@ape-bz2009 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

安装这两个包

[root@ape-bz2009 mysql]# yum -y install libaio.x86_64
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package libaio.x86_64 0:0.3.109-13.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved



[root@ape-bz2009 mysql]# yum -y install libaio-devel.x86_64
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package libaio-devel.x86_64 0:0.3.109-13.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

初始化mysql

[root@ape-bz2009 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
Installing MySQL system tables...2018-11-07 13:52:26 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-11-07 13:52:26 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2018-11-07 13:52:26 0 [Note] ./bin/mysqld (mysqld 5.6.39) starting as process 11811 ...
2018-11-07 13:52:26 11811 [Note] InnoDB: Using atomics to ref count buffer pool pages
2018-11-07 13:52:26 11811 [Note] InnoDB: The InnoDB memory heap is disabled
2018-11-07 13:52:26 11811 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-11-07 13:52:26 11811 [Note] InnoDB: Memory barrier is not used
2018-11-07 13:52:26 11811 [Note] InnoDB: Compressed tables use zlib 1.2.3
2018-11-07 13:52:26 11811 [Note] InnoDB: Using Linux native AIO
2018-11-07 13:52:26 11811 [Note] InnoDB: Using CPU crc32 instructions
2018-11-07 13:52:26 11811 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2018-11-07 13:52:26 11811 [Note] InnoDB: Completed initialization of buffer pool
2018-11-07 13:52:26 11811 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2018-11-07 13:52:26 11811 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2018-11-07 13:52:26 11811 [Note] InnoDB: Database physically writes the file full: wait...
2018-11-07 13:52:26 11811 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2018-11-07 13:52:26 11811 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2018-11-07 13:52:27 11811 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2018-11-07 13:52:27 11811 [Warning] InnoDB: New log files created, LSN=45781
2018-11-07 13:52:27 11811 [Note] InnoDB: Doublewrite buffer not found: creating new
2018-11-07 13:52:27 11811 [Note] InnoDB: Doublewrite buffer created
2018-11-07 13:52:27 11811 [Note] InnoDB: 128 rollback segment(s) are active.
2018-11-07 13:52:27 11811 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-11-07 13:52:27 11811 [Note] InnoDB: Foreign key constraint system tables created
2018-11-07 13:52:27 11811 [Note] InnoDB: Creating tablespace and datafile system tables.
2018-11-07 13:52:27 11811 [Note] InnoDB: Tablespace and datafile system tables created.
2018-11-07 13:52:27 11811 [Note] InnoDB: Waiting for purge to start
2018-11-07 13:52:27 11811 [Note] InnoDB: 5.6.39 started; log sequence number 0
2018-11-07 13:52:27 11811 [Note] Binlog end
2018-11-07 13:52:27 11811 [Note] InnoDB: FTS optimize thread exiting.
2018-11-07 13:52:27 11811 [Note] InnoDB: Starting shutdown...
2018-11-07 13:52:29 11811 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK

Filling help tables...2018-11-07 13:52:29 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-11-07 13:52:29 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2018-11-07 13:52:29 0 [Note] ./bin/mysqld (mysqld 5.6.39) starting as process 11833 ...
2018-11-07 13:52:29 11833 [Note] InnoDB: Using atomics to ref count buffer pool pages
2018-11-07 13:52:29 11833 [Note] InnoDB: The InnoDB memory heap is disabled
2018-11-07 13:52:29 11833 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-11-07 13:52:29 11833 [Note] InnoDB: Memory barrier is not used
2018-11-07 13:52:29 11833 [Note] InnoDB: Compressed tables use zlib 1.2.3
2018-11-07 13:52:29 11833 [Note] InnoDB: Using Linux native AIO
2018-11-07 13:52:29 11833 [Note] InnoDB: Using CPU crc32 instructions
2018-11-07 13:52:29 11833 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2018-11-07 13:52:29 11833 [Note] InnoDB: Completed initialization of buffer pool
2018-11-07 13:52:29 11833 [Note] InnoDB: Highest supported file format is Barracuda.
2018-11-07 13:52:29 11833 [Note] InnoDB: 128 rollback segment(s) are active.
2018-11-07 13:52:29 11833 [Note] InnoDB: Waiting for purge to start
2018-11-07 13:52:29 11833 [Note] InnoDB: 5.6.39 started; log sequence number 1625977
2018-11-07 13:52:29 11833 [Note] Binlog end
2018-11-07 13:52:29 11833 [Note] InnoDB: FTS optimize thread exiting.
2018-11-07 13:52:29 11833 [Note] InnoDB: Starting shutdown...
2018-11-07 13:52:31 11833 [Note] InnoDB: Shutdown completed; log sequence number 1625987
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 ape-bz2009 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 at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

  http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

WARNING: Found existing config file ./my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as ./my-new.cnf,
please compare it with your file and take the changes you need.

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

[root@ape-bz2009 mysql]# echo $?
0

备份配置文件

[root@ape-bz2009 mysql]# mv /etc/my.cnf /etc/my.cnf.bak
[root@ape-bz2009 mysql]# ls /etc/my.cnf.bak
/etc/my.cnf.bak

编辑配置文件:

扫描二维码关注公众号,回复: 4038572 查看本文章
[root@ape-bz2009 mysql]# vi /etc/init.d/mysqld
# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.


basedir=/usr/local/mysql
datadir=/data/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

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
basedir = /usr/local/mysql
datadir = /data/mysql

增加开机启动mysql,并启动mysql

[root@ape-bz2009 mysql]# chkconfig --add mysqld
[root@ape-bz2009 mysql]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

aegis          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
agentwatch     	0:off	1:off	2:on	3:on	4:on	5:on	6:off
mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[root@ape-bz2009 mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/ape-bz2009.err'.
                                                           [  OK  ]
[root@ape-bz2009 mysql]# ps aux |grep mysql
root     11894  0.0  0.1 113260  1588 pts/1    S    13:57   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/ape-bz2009.pid
mysql    12029  4.6 44.3 1303632 450684 pts/1  Sl   13:57   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=ape-bz2009.err --pid-file=/data/mysql/ape-bz2009.pid
root     12058  0.0  0.0 112660   968 pts/1    R+   13:57   0:00 grep --color=auto mysql

修改mysql 登录密码

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| 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                      |
+---------------------------+
28 rows in set (0.00 sec)

mysql> update user set password=password("123456") where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

2. 登录从服务器并安装mysql

[root@ape-bz2011 mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/ape-bz2011.err'.
                                                           [  OK  ]
[root@ape-bz2011 mysql]# ps aux|grep mysql
root     17438  0.0  0.1 113260  1584 pts/1    S    14:32   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/ape-bz2011.pid
mysql    17573  4.2 44.3 1303628 450684 pts/1  Sl   14:32   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=ape-bz2011.err --pid-file=/data/mysql/ape-bz2011.pid
root     17602  0.0  0.0 112660   964 pts/1    R+   14:32   0:00 grep --color=auto mysql

3.配置主服务器:

[root@knightlai ~]# vim /etc/my.cnf
server_id=168
log_bin=master
[root@knightlai ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

//授权从服务器的执行权限
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* to 'repl'@'192.168.118.171' IDENTIFIED BY 'repl';
Query OK, 0 rows affected (0.00 sec)

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

//显示主服务器状态
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| master.000002 |      584 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

4.配置从服务器

[root@ape-bz2011 ~]# vim /etc/my.cnf
server-id=171




mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)

mysql> CHANGE MASTER TO master_host = '192.168.118.170',master_port = 3306,master_user='repl',master_password='repl',master_log_file='master.000002',master_log_pos = 584;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

配置成功了。。

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.118.170
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master.000002
          Read_Master_Log_Pos: 584
               Relay_Log_File: ape-bz2011-relay-bin.000002
                Relay_Log_Pos: 280
        Relay_Master_Log_File: master.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 

测试主从服务器同步,在主服务器上创建test20181107数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database test20181107;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| test20181107       |
+--------------------+
5 rows in set (0.00 sec)

在从服务器上查看数据库发现了刚刚在主上创建的数据库test20181107

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| test20181107       |
+--------------------+
5 rows in set (0.00 sec)

猜你喜欢

转载自blog.csdn.net/a1779078902/article/details/83822193