主配置环境(准备工作)

    1. //移动至文件夹下
    2. [root@localhost ~]# cd /usr/local/src/
    3. //查看
    4. [root@localhost src]# ls
    5. mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
    6. //解压
    7. [root@localhost src]# tar -zxf mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
    8. [root@localhost src]# ls
    9. mysql-5.6.47-linux-glibc2.12-x86_64 mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
    10. //将文件移动到MySQL下
    11. [root@localhost src]# mv mysql-5.6.47-linux-glibc2.12-x86_64 /usr/local/mysql
    12. [root@localhost src]#
    13. [root@localhost src]# cd /usr/local/mysql/
    14. [root@localhost mysql]#
    15. [root@localhost mysql]#
    16. [root@localhost mysql]# ls
    17. bin data docs include lib LICENSE man mysql-test README scripts share
    18. //改个主机名字没啥用
    19. [root@localhost mysql]# vim /etc/hostname
    20. [root@localhost mysql]# bash
    21. [root@bai mysql]#
    22. //创建一个MySQL用户
    23. [root@bai mysql]# useradd mysql
    24. [root@bai mysql]#
    25. //创建一个存放MySQL数据的目录
    26. [root@bai mysql]# mkdir /data/
    27. //安装脚本依赖
    28. [root@bai mysql]# yum install perl-Module-Install -y
    29. //编译安装 --指定用户 --指定数据目录
    30. [root@bai mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
    31. //验证
    32. [root@bai mysql]# echo $?
    33. 0
    34. //复制配置文件
    35. [root@wang mysql]# cp support-files/my-default.cnf /etc/my.cnf
    36. cp:是否覆盖"/etc/my.cnf" y
    37. //更改配置文件
    38. [root@wang mysql]# vim /etc/my.cnf
    39. basedir = /usr/local/mysql
    40. datadir = /data/mysql
    41. port = 3306
    42. server_id = 156
    43. socket = /tmp/mysql.sock
    44. //拷贝并发送给从服务器
    45. [root@wang mysql]# scp /etc/my.cnf root@192.168.142.157:/etc/
    46. The authenticity of host '192.168.142.157 (192.168.142.157)' can't be established.
    47. ECDSA key fingerprint is 8b:b3:c9:67:cb:cc:e9:c8:f1:38:eb:01:2f:cf:60:89.
    48. Are you sure you want to continue connecting (yes/no)? yes
    49. Warning: Permanently added '192.168.142.157' (ECDSA) to the list of known hosts.
    50. root@192.168.142.157's password:
    51. my.cnf 100% 1141 1.1KB/s 00:00
    52. //复制启动文件
    53. [root@wang mysql]# cp support-files/mysql.server /etc/init.d/mysqld
    54. //修改启动文件
    55. [root@wang mysql]# vim /etc/init.d/mysqld
    56. basedir=/usr/local/mysql
    57. datadir=/data/mysql
    58. //将启动文件拷贝发送给从服务器
    59. [root@wang mysql]# scp /etc/init.d/mysqld root@192.168.142.157:/etc/init.d/
    60. root@192.168.142.157's password:
    61. mysqld 100% 10KB 10.3KB/s 00:00
    62. //更改完配置文件后重启
    63. [root@wang mysql]# /etc/init.d/mysqld start
    64. Starting MySQL.Logging to '/data/mysql/wang.err'.
    65. . SUCCESS!
    66. //检查是否启动MySQL端口 可以下载依赖
    67. [root@wang mysql]# ps -ef |grep mysql
    68. root 10552 1 0 17:33 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/wang.pid
    69. mysql 10729 10552 2 17:33 pts/1 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=wang.err --pid-file=/data/mysql/wang.pid --socket=/tmp/mysql.sock --port=3306
    70. root 10783 10488 0 17:34 pts/1 00:00:00 grep --color=auto mysql

    搭建从配置环境

    1. //移动
    2. [root@localhost ~]# cd /usr/local/src/
    3. //查看压缩包
    4. [root@localhost src]# ls
    5. mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
    6. //解压
    7. [root@localhost src]# tar -zxf mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
    8. [root@localhost src]# ls
    9. mysql-5.6.47-linux-glibc2.12-x86_64 mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
    10. //将解压完的文件移动到MySQL目录下
    11. [root@localhost src]# mv mysql-5.6.47-linux-glibc2.12-x86_64 /usr/local/mysql
    12. //移动
    13. [root@localhost src]# cd /usr/local/mysql/
    14. [root@localhost mysql]# ls
    15. bin data docs include lib LICENSE man mysql-test README scripts share
    16. //改个主机名
    17. [root@localhost mysql]# vim /etc/hostname
    18. [root@localhost mysql]# bash
    19. //创建一个MySQL用户
    20. [root@bai mysql]# useradd mysql
    21. //创建一个存放MySQL数据的目录
    22. [root@bai mysql]# mkdir /data/
    23. //安装脚本依赖
    24. [root@bai mysql]# yum install perl-Module-Install -y
    25. //编译安装
    26. [root@bai mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
    27. //验证是否出错
    28. [root@bai mysql]# echo $?
    29. 0
    30. [root@bai mysql]# /etc/init.d/mysqld start
    31. Starting MySQL.Logging to '/data/mysql/bai.err'.
    32. . SUCCESS!
    33. [root@bai mysql]# ps -ef |grep mysql
    34. root 10552 1 0 17:33 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/bai.pid
    35. mysql 10729 10552 2 17:33 pts/1 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=bai.err --pid-file=/data/mysql/bai.pid --socket=/tmp/mysql.sock --port=3306
    36. root 10783 10488 0 17:34 pts/1 00:00:00 grep --color=auto mysql

    主配置及从配置 - 图1

    主服务器开始配置

    1. //更改配置文件 (注意log_bin这是记录主服务器的操作的 有他才能进行剩下的操作)
    2. [root@wang mysql]# vim /etc/my.cnf
    3. //名字随便起
    4. log_bin=bailinux1
    5. server_id = 156
    6. //更改完配置文件要重启
    7. [root@wang mysql]# /etc/init.d/mysqld restart
    8. Shutting down MySQL.. SUCCESS!
    9. Starting MySQL.. SUCCESS!
    10. [root@wang mysql]# cd /data/mysql/
    11. [root@wang mysql]#
    12. [root@wang mysql]# ls
    13. //如果没有这两个文件后面的主从配置就没办法做了
    14. auto.cnf ib_logfile1 test bai1.000001
    15. ibdata1 mysql wang.err bai1.index
    16. ib_logfile0 performance_schema wang.pid
    17. //将MySQL所需的环境变量定义一下 不定义的话进入MySQL需要带着路径/usr/local/mysql/bin/进入MySQL
    18. [root@wang mysql]# export PATH=$PATH:/usr/local/mysql/bin/
    19. //把mysql库备份并恢复成ytl库,作为测试数据做了一个备份
    20. [root@wang mysql]# mysqldump -uroot mysql > /tmp/mysql.sql
    21. //定义完MySQL环境变量后要把他写入vim /etc/profile配置文件中使他永久生效
    22. [root@wang mysql]# vim /etc/profile
    23. //最后一行填写
    24. export PATH=$PATH:/usr/local/mysql/bin/
    25. //保存一下
    26. [root@wang mysql]# source /etc/profile
    27. //启动
    28. [root@wang mysql]# chkconfig mysqld on
    29. //这是那个备份的文件
    30. [root@wang mysql]# ls -la /tmp/mysql.sql
    31. -rw-r--r--. 1 root root 698597 12 8 18:04 /tmp/mysql.sql
    32. //接下来创建一个用户
    33. [root@wang mysql]# mysql -uroot -e "create database bsq"
    34. // 把备份文件导进去
    35. [root@wang mysql]# mysql -uroot bsq < /tmp/mysql.sql
    36. [root@wang mysql]# ls -la
    37. 总用量 111652
    38. drwx------. 6 mysql mysql 4096 12 8 18:21 .
    39. drwxr-xr-x. 3 root root 18 12 8 16:51 ..
    40. -rw-rw----. 1 mysql mysql 56 12 8 17:33 auto.cnf
    41. -rw-rw----. 1 mysql mysql 12582912 12 8 18:22 ibdata1
    42. -rw-rw----. 1 mysql mysql 50331648 12 8 18:22 ib_logfile0
    43. -rw-rw----. 1 mysql mysql 50331648 12 8 16:51 ib_logfile1
    44. drwx------. 2 mysql mysql 4096 12 8 16:51 mysql
    45. drwx------. 2 mysql mysql 4096 12 8 16:52 performance_schema
    46. drwx------. 2 mysql mysql 6 12 8 16:51 test
    47. -rw-rw----. 1 mysql mysql 7606 12 8 17:52 wang.err
    48. -rw-rw----. 1 mysql mysql 6 12 8 17:52 wang.pid
    49. drwx------. 2 mysql mysql 4096 12 8 18:22 bsq
    50. -rw-rw----. 1 mysql mysql 698600 12 8 18:22 bai1.000001
    51. -rw-rw----. 1 mysql mysql 18 12 8 17:52 bai1.index
    52. 这时我们发现ytlinux1.000001的字节大小变成698600大小了这是说明它把我们的创建库的过程
    53. 完全的记录了下来(这时我们就会想到我们是不是可以把刚创建的这个库和库文件删除,然后再通过这个二进制备份文件恢复呢? 当然可以,只要保证数据的完整性就可以了)
    54. //创建用作同步数据库的用户
    55. [root@wang mysql]# mysql -uroot
    56. //授权
    57. mysql> grant replication slave on *.* to 'repl'@192.168.142.157 identified by 'password';
    58. Query OK, 0 rows affected (0.00 sec)
    59. //锁住,目前的数距保持当前的状态。这样才能保持数据的一致性
    60. mysql> flush tables with read lock;
    61. Query OK, 0 rows affected (0.01 sec)
    62. //查看当前的状态
    63. mysql> show master status;
    64. +-----------------+----------+--------------+------------------+-------------------+
    65. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    66. +-----------------+----------+--------------+------------------+-------------------+
    67. | bai1.000001 | 698812 | | | |
    68. +-----------------+----------+--------------+------------------+-------------------+
    69. 1 row in set (0.00 sec)
    70. mysql> quit
    71. Bye
    72. //把主服务器上的库复制到从服务器上
    73. [root@wang mysql]# scp /tmp/mysql.sql root@192.168.142.157:/tmp/
    74. root@192.168.142.157's password:
    75. mysql.sql 100% 682KB 682.2KB/s 00:00
    76. //查看大小
    77. [root@wang mysql]# ls -la /tmp/mysql.sql
    78. -rw-r--r--. 1 root root 698597 12月 8 18:04 /tmp/mysql.sql

    主配置及从配置 - 图2
    从服务器开始配置

    1. //定义mysql环境变量
    2. [root@bai ~]# export PATH=$PATH:/usr/local/mysql/bin/
    3. //把MySQL环建变量的路径写入配置文件
    4. [root@bai ~]# vim /etc/profile
    5. //写配置文件的最后面即可
    6. export PATH=$PATH:/usr/local/mysql/bin/
    7. //
    8. [root@bai ~]# source /etc/profile
    9. //启动
    10. [root@bai ~]# chkconfig mysqld on
    11. //修改配置文件
    12. [root@bai ~]# vim /etc/my.cnf
    13. server_id = 157
    14. [root@bai ~]# /etc/init.d/mysqld restart
    15. Shutting down MySQL.. SUCCESS!
    16. Starting MySQL.. SUCCESS!
    17. //这是通过主服务器复制过来的库,看看于主服务上上的大小是否一致
    18. [root@bai mysql]# ls -la /tmp/mysql.sql
    19. -rw-r--r--. 1 root root 698597 12 8 19:40 /tmp/mysql.sql
    20. //把数据库到同名库里面
    21. [root@bai ~]# mysql -uroot
    22. //创建库
    23. mysql> create database bsq;
    24. Query OK, 1 row affected (0.00 sec)
    25. mysql> quit
    26. Bye
    27. [root@bai ~]# mysql -uroot bsq < /tmp/mysql.sql
    28. [root@bai ~]#
    29. [root@bai ~]#
    30. [root@bai ~]# mysql -uroot
    31. //查看库
    32. mysql> show databases;
    33. +--------------------+
    34. | Database |
    35. +--------------------+
    36. | information_schema |
    37. | mysql |
    38. | performance_schema |
    39. | test |
    40. | bsq |
    41. +--------------------+
    42. 5 rows in set (0.00 sec)
    43. //切换库
    44. mysql> use ytl;
    45. Reading table information for completion of table and column names
    46. You can turn off this feature to get a quicker startup with -A
    47. Database changed
    48. mysql>
    49. //查看表
    50. mysql> show tables;
    51. +---------------------------+
    52. | Tables_in_bsq |
    53. +---------------------------+
    54. | columns_priv |
    55. | db |
    56. | event |
    57. | func |
    58. | general_log |
    59. | help_category |
    60. | help_keyword |
    61. | help_relation |
    62. | help_topic |
    63. | innodb_index_stats |
    64. | innodb_table_stats |
    65. | ndb_binlog_index |
    66. | plugin |
    67. | proc |
    68. | procs_priv |
    69. | proxies_priv |
    70. | servers |
    71. | slave_master_info |
    72. | slave_relay_log_info |
    73. | slave_worker_info |
    74. | slow_log |
    75. | tables_priv |
    76. | time_zone |
    77. | time_zone_leap_second |
    78. | time_zone_name |
    79. | time_zone_transition |
    80. | time_zone_transition_type |
    81. | user |
    82. +---------------------------+
    83. 28 rows in set (0.00 sec)
    84. mysql> quit
    85. Bye
    86. [root@bai ~]# cd /data/mysql/
    87. [root@bai mysql]# ls -la
    88. 总用量 110624
    89. drwx------. 6 mysql mysql 4096 12 8 21:41 .
    90. drwxr-xr-x. 3 root root 18 12 8 16:52 ..
    91. -rw-rw----. 1 mysql mysql 56 12 8 17:36 auto.cnf
    92. -rw-rw----. 1 mysql mysql 7602 12 8 19:33 bai.err
    93. -rw-rw----. 1 mysql mysql 6 12 8 19:33 bai.pid
    94. -rw-rw----. 1 mysql mysql 12582912 12 8 21:45 ibdata1
    95. -rw-rw----. 1 mysql mysql 50331648 12 8 21:45 ib_logfile0
    96. -rw-rw----. 1 mysql mysql 50331648 12 8 16:52 ib_logfile1
    97. drwx------. 2 mysql mysql 4096 12 8 16:52 mysql
    98. drwx------. 2 mysql mysql 4096 12 8 16:52 performance_schema
    99. drwx------. 2 mysql mysql 6 12 8 16:52 test
    100. drwx------. 2 mysql mysql 4096 12 8 21:45 bsq
    101. //实现它的主从
    102. //
    103. mysql> stop slave;
    104. Query OK, 0 rows affected, 1 warning (0.01 sec)
    105. //
    106. mysql> change master to master_host='192.168.142.156',master_user='repl',master_password='000000', master_log_file='bai1.000001',master_log_pos=698812;
    107. Query OK, 0 rows affected, 2 warnings (0.01 sec)
    108. //
    109. mysql> start slave;
    110. Query OK, 0 rows affected (0.00 sec)
    111. //
    112. mysql> show slave status\G;
    113. *************************** 1. row ***************************
    114. Slave_IO_State: Waiting for master to send event
    115. Master_Host: 192.168.142.156
    116. Master_User: repl
    117. Master_Port: 3306
    118. Connect_Retry: 60
    119. Master_Log_File: bai1.000001
    120. Read_Master_Log_Pos: 410
    121. Relay_Log_File: client-relay-bin.000002
    122. Relay_Log_Pos: 277
    123. Relay_Master_Log_File: bai1.000001
    124. Slave_IO_Running: Yes
    125. Slave_SQL_Running: Yes
    126. Replicate_Do_DB: // 同步那些库 这些配置都是可以写在著配置文件的my.cnf
    127. Replicate_Ignore_DB: // 不同步哪些库
    128. Replicate_Do_Table: // 同步哪些表
    129. Replicate_Ignore_Table:
    130. Replicate_Wild_Do_Table: // 同步哪个库.表 常用
    131. Replicate_Wild_Ignore_Table: // 忽略哪个库的哪个表
    132. Last_Errno: 0 // 错误信息
    133. Last_Error:
    134. Skip_Counter: 0
    135. Exec_Master_Log_Pos: 410
    136. Relay_Log_Space: 451
    137. Until_Condition: None
    138. Until_Log_File:
    139. Until_Log_Pos: 0
    140. Master_SSL_Allowed: No
    141. Master_SSL_CA_File:
    142. Master_SSL_CA_Path:
    143. Master_SSL_Cert:
    144. Master_SSL_Cipher:
    145. Master_SSL_Key:
    146. Seconds_Behind_Master: 0
    147. Master_SSL_Verify_Server_Cert: No
    148. Last_IO_Errno: 0 // 线程错误信息
    149. Last_IO_Error:
    150. Last_SQL_Errno: 0
    151. Last_SQL_Error:
    152. Replicate_Ignore_Server_Ids:
    153. Master_Server_Id: 41
    154. Master_UUID: 197dfb61-1310-11ec-af29-000c29b5c42c
    155. Master_Info_File: /data/mysql/master.info
    156. SQL_Delay: 0
    157. SQL_Remaining_Delay: NULL
    158. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
    159. Master_Retry_Count: 86400
    160. Master_Bind:
    161. Last_IO_Error_Timestamp:
    162. Last_SQL_Error_Timestamp:
    163. Master_SSL_Crl:
    164. Master_SSL_Crlpath:
    165. Retrieved_Gtid_Set:
    166. Executed_Gtid_Set:
    167. Auto_Position: 0
    168. 1 row in set (0.00 sec)