基础准备

         主机名       镜像       IP
         MySQL1 CentOS-7-x86_64-DVD-1511.iso 192.168.200.10
         MySQL2 CentOS-7-x86_64-DVD-1511.iso 192.168.200.20

    基础环境

    1. MySQL节点1
    2. [root@mysql ~]# hostnamectl set-hostname mysql1
    3. [root@mysql ~]# bash
    4. [root@mysql1 ~]# hostnamectl
    5. Static hostname: mysql1
    6. Icon name: computer-vm
    7. Chassis: vm
    8. Machine ID: eefd7b411d7347bdac743e0d446ba0b4
    9. Boot ID: 0ea3f72a474f4542bbf3be2ef30ff74c
    10. Virtualization: vmware
    11. Operating System: CentOS Linux 7 (Core)
    12. CPE OS Name: cpe:/o:centos:centos:7
    13. Kernel: Linux 3.10.0-327.el7.x86_64
    14. Architecture: x86-64
    15. [root@mysql1 ~]# setenforce 0
    16. [root@mysql1 ~]# systemctl stop firewalld
    17. [root@mysql1 ~]# vim /etc/hosts
    18. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
    19. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
    20. 192.168.200.10 mysql1
    21. 192.168.200.20 mysql2
    22. [root@mysql1 ~]# yum install -y mariadb mariadb-server
    23. [root@mysql1 ~]# systemctl start mariadb
    24. [root@mysql1 ~]# systemctl enable mariadb
    25. Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
    26. MySQL节点2
    27. [root@localhost ~]# hostnamectl set-hostname mysql2
    28. [root@localhost ~]# bash
    29. [root@mysql2 ~]# hostnamectl
    30. Static hostname: mysql2
    31. Icon name: computer-vm
    32. Chassis: vm
    33. Machine ID: eefd7b411d7347bdac743e0d446ba0b4
    34. Boot ID: eded95cc080048098ffcd98d284c5ede
    35. Virtualization: vmware
    36. Operating System: CentOS Linux 7 (Core)
    37. CPE OS Name: cpe:/o:centos:centos:7
    38. [root@mysql2 ~]# setenforce 0
    39. [root@mysql2 ~]# systemctl stop firewalld
    40. [root@mysql2 ~]# vim /etc/hosts
    41. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
    42. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
    43. 192.168.200.10 mysql1
    44. 192.168.200.20 mysql2
    45. Kernel: Linux 3.10.0-327.el7.x86_64
    46. Architecture: x86-64
    47. [root@mysql2 ~]# yum install -y mariadb mariadb-server
    48. [root@mysql2 ~]# systemctl start mariadb
    49. [root@mysql2 ~]# systemctl enable mariadb
    50. Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

    基础环境安装

    1. MySQL节点1
    2. [root@mysql1 ~]# mysql_secure_installation
    3. NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
    4. SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
    5. In order to log into MariaDB to secure it, we'll need the current
    6. password for the root user. If you've just installed MariaDB, and
    7. you haven't set the root password yet, the password will be blank,
    8. so you should just press enter here.
    9. Enter current password for root (enter for none):
    10. OK, successfully used password, moving on...
    11. Setting the root password ensures that nobody can log into the MariaDB
    12. root user without the proper authorisation.
    13. Set root password? [Y/n] y
    14. New password:
    15. Re-enter new password:
    16. Password updated successfully!
    17. Reloading privilege tables..
    18. ... Success!
    19. By default, a MariaDB installation has an anonymous user, allowing anyone
    20. to log into MariaDB without having to have a user account created for
    21. them. This is intended only for testing, and to make the installation
    22. go a bit smoother. You should remove them before moving into a
    23. production environment.
    24. Remove anonymous users? [Y/n] y
    25. ... Success!
    26. Normally, root should only be allowed to connect from 'localhost'. This
    27. ensures that someone cannot guess at the root password from the network.
    28. Disallow root login remotely? [Y/n] n
    29. ... skipping.
    30. By default, MariaDB comes with a database named 'test' that anyone can
    31. access. This is also intended only for testing, and should be removed
    32. before moving into a production environment.
    33. Remove test database and access to it? [Y/n] y
    34. - Dropping test database...
    35. ... Success!
    36. - Removing privileges on test database...
    37. ... Success!
    38. Reloading the privilege tables will ensure that all changes made so far
    39. will take effect immediately.
    40. Reload privilege tables now? [Y/n] y
    41. ... Success!
    42. Cleaning up...
    43. All done! If you've completed all of the above steps, your MariaDB
    44. installation should now be secure.
    45. Thanks for using MariaDB!
    46. [root@mysql1 ~]# vim /etc/my.cnf
    47. [mysqld]
    48. datadir=/var/lib/mysql
    49. socket=/var/lib/mysql/mysql.sock
    50. log_bin=mysql-bin \\开启MySQL binlog功能启用二进制日志
    51. binlog_ignore_db=mysql \\此参数表示不记录指定的数据库的二进制日志。
    52. server_id=10 \\服务器唯一ID
    53. datadir=/var/lib/mysql \\MySQL默认的数据文档存储目录为/var/lib/mysql
    54. socket=/var/lib/mysql/mysql.sock \\启动时指定socket文件的路径
    55. # Disabling symbolic-links is recommended to prevent assorted security risks
    56. symbolic-links=0
    57. # Settings user and group are ignored when systemd is used.
    58. # If you need to run mysqld under a different user or group,
    59. # customize your systemd unit file for mariadb according to the
    60. # instructions in http://fedoraproject.org/wiki/Systemd
    61. [mysqld_safe]
    62. log-error=/var/log/mariadb/mariadb.log
    63. pid-file=/var/run/mariadb/mariadb.pid
    64. #
    65. # include all files from the config directory
    66. #
    67. !includedir /etc/my.cnf.d
    68. [root@mysql1 ~]# systemctl restart mariadb
    69. [root@mysql1 ~]# mysql -uroot -p000000
    70. Welcome to the MariaDB monitor. Commands end with ; or \g.
    71. Your MariaDB connection id is 2
    72. Server version: 5.5.68-MariaDB MariaDB Server
    73. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    74. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    75. MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "000000";
    76. Query OK, 0 rows affected (0.00 sec)
    77. MariaDB [(none)]> grant replication slave on *.* to 'user'@'mysql2' identified by '000000';
    78. Query OK, 0 rows affected (0.00 sec)
    79. MariaDB [(none)]>
    80. MySQL节点2
    81. 与节点1一样MySQL先初始化再改配置
    82. [root@mysql2 ~]# mysql_secure_installation
    83. NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
    84. SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
    85. In order to log into MariaDB to secure it, we'll need the current
    86. password for the root user. If you've just installed MariaDB, and
    87. you haven't set the root password yet, the password will be blank,
    88. so you should just press enter here.
    89. Enter current password for root (enter for none):
    90. OK, successfully used password, moving on...
    91. Setting the root password ensures that nobody can log into the MariaDB
    92. root user without the proper authorisation.
    93. Set root password? [Y/n] y
    94. New password:
    95. Re-enter new password:
    96. Password updated successfully!
    97. Reloading privilege tables..
    98. ... Success!
    99. By default, a MariaDB installation has an anonymous user, allowing anyone
    100. to log into MariaDB without having to have a user account created for
    101. them. This is intended only for testing, and to make the installation
    102. go a bit smoother. You should remove them before moving into a
    103. production environment.
    104. Remove anonymous users? [Y/n] y
    105. ... Success!
    106. Normally, root should only be allowed to connect from 'localhost'. This
    107. ensures that someone cannot guess at the root password from the network.
    108. Disallow root login remotely? [Y/n] n
    109. ... skipping.
    110. By default, MariaDB comes with a database named 'test' that anyone can
    111. access. This is also intended only for testing, and should be removed
    112. before moving into a production environment.
    113. Remove test database and access to it? [Y/n] y
    114. - Dropping test database...
    115. ... Success!
    116. - Removing privileges on test database...
    117. ... Success!
    118. Reloading the privilege tables will ensure that all changes made so far
    119. will take effect immediately.
    120. Reload privilege tables now? [Y/n] y
    121. ... Success!
    122. Cleaning up...
    123. All done! If you've completed all of the above steps, your MariaDB
    124. installation should now be secure.
    125. Thanks for using MariaDB!
    126. [root@mysql2 ~]# vim /etc/my.cnf
    127. [mysqld]
    128. datadir=/var/lib/mysql
    129. socket=/var/lib/mysql/mysql.sock
    130. log_bin = mysql-bin
    131. binlog_ignore_db = mysql
    132. server_id = 20
    133. datadir=/var/lib/mysql
    134. socket=/var/lib/mysql/mysql.sock
    135. # Disabling symbolic-links is recommended to prevent assorted security risks
    136. symbolic-links=0
    137. # Settings user and group are ignored when systemd is used.
    138. # If you need to run mysqld under a different user or group,
    139. # customize your systemd unit file for mariadb according to the
    140. # instructions in http://fedoraproject.org/wiki/Systemd
    141. [mysqld_safe]
    142. log-error=/var/log/mariadb/mariadb.log
    143. pid-file=/var/run/mariadb/mariadb.pid
    144. #
    145. # include all files from the config directory
    146. #
    147. !includedir /etc/my.cnf.d
    148. [root@mysql2 ~]# systemctl restart mariadb
    149. [root@mysql2 ~]# mysql -uroot -p000000
    150. Welcome to the MariaDB monitor. Commands end with ; or \g.
    151. Your MariaDB connection id is 2
    152. Server version: 5.5.68-MariaDB MariaDB Server
    153. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    154. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    155. MariaDB [(none)]> change master to
    156. -> master_host='mysql1',master_user='user',master_password='000000';
    157. Query OK, 0 rows affected (0.00 sec)
    158. MariaDB [(none)]>
    159. MariaDB [(none)]> start slave;
    160. Query OK, 0 rows affected (0.00 sec)
    161. MariaDB [(none)]> show slave status\G
    162. *************************** 1. row ***************************
    163. Slave_IO_State: Waiting for master to send event
    164. Master_Host: mysql1
    165. Master_User: user
    166. Master_Port: 3306
    167. Connect_Retry: 60
    168. Master_Log_File: mysql-bin.000001
    169. Read_Master_Log_Pos: 529
    170. Relay_Log_File: mariadb-relay-bin.000002
    171. Relay_Log_Pos: 813
    172. Relay_Master_Log_File: mysql-bin.000001
    173. Slave_IO_Running: Yes
    174. Slave_SQL_Running: Yes
    175. Replicate_Do_DB:
    176. Replicate_Ignore_DB:
    177. Replicate_Do_Table:
    178. Replicate_Ignore_Table:
    179. Replicate_Wild_Do_Table:
    180. Replicate_Wild_Ignore_Table:
    181. Last_Errno: 0
    182. Last_Error:
    183. Skip_Counter: 0
    184. Exec_Master_Log_Pos: 529
    185. Relay_Log_Space: 1109
    186. Until_Condition: None
    187. Until_Log_File:
    188. Until_Log_Pos: 0
    189. Master_SSL_Allowed: No
    190. Master_SSL_CA_File:
    191. Master_SSL_CA_Path:
    192. Master_SSL_Cert:
    193. Master_SSL_Cipher:
    194. Master_SSL_Key:
    195. Seconds_Behind_Master: 0
    196. Master_SSL_Verify_Server_Cert: No
    197. Last_IO_Errno: 0
    198. Last_IO_Error:
    199. Last_SQL_Errno: 0
    200. Last_SQL_Error:
    201. Replicate_Ignore_Server_Ids:
    202. Master_Server_Id: 10
    203. 1 row in set (0.00 sec)

    3.验证数据库主从服务
    1)主节点创建数据库
    先在主节点 mysql1 中创建库 bai,并在库 bai 中创建表 bai命令如下:

    1. 节点1:
    2. [root@mysql1 ~]# mysql -uroot -p000000
    3. Welcome to the MariaDB monitor. Commands end with ; or \g.
    4. Your MariaDB connection id is 4
    5. Server version: 5.5.68-MariaDB MariaDB Server
    6. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    7. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    8. MariaDB [(none)]> create database bai;
    9. Query OK, 1 row affected (0.00 sec)
    10. MariaDB [(none)]>
    11. MariaDB [(none)]> use bai;
    12. Database changed
    13. MariaDB [bai]> create table bai(id int not null primary key,name varchar(50),addr varchar(255));

    2)从节点验证复制功能
    登录 mysql2 节点的数据库,查看数据库列表。找到 bai 数据库,查询表.

    1. 节点2
    2. [root@mysql2 ~]# mysql -uroot -p000000
    3. Welcome to the MariaDB monitor. Commands end with ; or \g.
    4. Your MariaDB connection id is 5
    5. Server version: 5.5.68-MariaDB MariaDB Server
    6. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    7. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    8. MariaDB [(none)]> show databases;
    9. +--------------------+
    10. | Database |
    11. +--------------------+
    12. | information_schema |
    13. | bai |
    14. | mysql |
    15. | performance_schema |
    16. +--------------------+
    17. 4 rows in set (0.01 sec)
    18. MariaDB [(none)]> use bai;
    19. Reading table information for completion of table and column names
    20. You can turn off this feature to get a quicker startup with -A
    21. Database changed
    22. MariaDB [bai]> show tables;
    23. +---------------+
    24. | Tables_in_bai |
    25. +---------------+
    26. | bai |
    27. | company |
    28. +---------------+
    29. 2 rows in set (0.00 sec)