mysql8.0.22安装部署主从复制

1.mysql官方下载Linux版本,我这里下载的是: mysql-8.0.22-linux-glibc2.12-x86_64.tar.xz(注:不同压缩包,存在解压命令的不同)

2.将压缩包扔到Linux的 /usr/local文件夹中
3.使用解压命令解压: tar xvf /usr/local/mysql-8.0.22-linux-glibc2.12-x86_64.tar.xz
4.重命名解压后得到的文件夹, 名称变更为 mysql (mysql一些默认配置里Linux访问路径为:/usr/local/mysql, 直接重命名后,后续一些地方则不需要更改)

6.进入被重命名过的文件夹中,再创建一个文件夹,取名:data
(1) cd /usr/local/mysql
(2) mkdir data

7.创建 /etc/my.cnf . 看其他博友说的,好像是8.0这个版本通过二进制文件解压安装的话是不存在的. 这里手动创建一个

(1) vi /etc/my.cnf

(2)复制以下内容进去:
[mysqld]
port=3306
basedir=/usr/local/mysql
datadir=/data01/data
max_connections=10000
max_connect_errors=10
character-set-server=UTF8MB4
default-storage-engine=INNODB
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION









文字说明:
(需要修改的地方:
basedir: 使用给定目录作为根目录(安装目录)
datadir: 从给定目录读取数据库文件,存储mysql数据文件夹
重要的两个:
lower_case_table_names: 值为1,忽略大小写,即查询时候不分大小写(一旦设置并启动后,再更改比较烦,慎重)
sql_mode: (sql中select后面的字段必须出现在group by后面,或者被聚合函数包裹,不然会抛出上面的错误,允许使用 GROUP BY 函数)





(3)按键盘左上角 ESC键 -> :wq (保存文件)

8.进行授权
(1) chown -R mysql.mysql /usr/local/mysql
(2) chmod 755 /usr/local/mysql
(3) chmod 644 /etc/my.cnf


  1. 生成mysql基础数据
    (1) cd /usr/local/mysql/bin

    (2) 生成mysql数据库基础数据 ./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/data01/data --lower_case_table_names=1 --initialize

    文字说明:
    --user=mysql : 指定运行mysqld进程的用户名。可更改,主要为了后续添加多个mysql管理员时方便
    --basedir=/usr/local/mysql: 安装目录
    --datadir=/usr/local/mysql/mysql-files: 数据存放目录
    --lower_case_table_names=1: 不区分大小写(这个根据自己需求来,可以先搜索看看)



注: 该命令运行后, 会产生一个临时登录密码, 在返回结果的最后一行(可搜索看看)

10.添加mysql服务到系统,并完成授权
(1) cd /usr/local/mysql
(2) cp -a ./support-files/mysql.server /etc/init.d/mysql
(3) chmod +x /etc/init.d/mysql
(4) chkconfig --add mysql



  1. mysql启动: service mysql start
    mysql状态查看: service mysql status

  2. mysql登录
    (1) mysql -uroot -p
    (2) 需要输入的密码是 第9步中运行生成基础数据后, 返回结果中给出的临时密码

  3. 修改密码
    (1) ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mysql'; (xxxx - 这里需要自己设置密码)
    (2) flush privileges; (密码生效)

  4. 允许远程连接 use mysql
    (1) update user set host='%' where user='root'; (root表示想要被连接的数据库的用户名,其中“%”表示允许所有机器能访问root用户.如果失败的话,有可能是因为数据库的用户名不是root,这种情况下,只需要将root改为数据库用户名就可以了。)
    (2) flush privileges;

  5. 配置环境变量 .bash_profile
    MYSQL_HOME=/usr/local/mysql
    PATH=$PATH:$MYSQL_HOME/bin
    export PATH MYSQL_HOME


16.修改 server_id系统变量

主库
set global server_id=1151;
从库1
set global server_id=1152;
从库2
set global server_id=1153;




  1. 查看主库二进制日志信息

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000003 | 438 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)





mysql>

  1. 在主库上建立复制专属账户

--主库
create USER 'repl'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mysql';
[root@mysql01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 8.0.22 MySQL Community Server - GPL





Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> grant replication slave,replication client on . to 'repl'@'%';
ERROR 1410 (42000): You are not allowed to create a user with GRANT
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> update user set host='%' where user='repl';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0


mysql> grant replication slave,replication client on . to 'repl'@'%';
ERROR 1410 (42000): You are not allowed to create a user with GRANT
mysql> grant replication slave,replication client on . to 'repl'@'%';
Query OK, 0 rows affected (0.00 sec)


mysql>

19.在从库上创建主库的信息

change master to
master_host='10.10.10.151',
master_port=3306,
master_user='repl',
master_password='mysql',
master_log_file='binlog.000003',
master_log_pos=438;





20 在从库启动复制查看复制进程

start slave;
show slave status\G
mysql> show slave status\G
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 10.10.10.151
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000003
Read_Master_Log_Pos: 1860
Relay_Log_File: mysql03-relay-bin.000002
Relay_Log_Pos: 1743
Relay_Master_Log_File: binlog.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1860
Relay_Log_Space: 1954
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1151
Master_UUID: aa703403-34ba-11eb-a6f8-000c295ce2e2
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
mysql> select * from mysql.user where user='repl'\G
1. row
Host: %
User: repl
Select_priv: N
Insert_priv: N
Update_priv: N
Delete_priv: N
Create_priv: N
Drop_priv: N
Reload_priv: N
Shutdown_priv: N
Process_priv: N
File_priv: N
Grant_priv: N
References_priv: N
Index_priv: N
Alter_priv: N
Show_db_priv: N
Super_priv: N
Create_tmp_table_priv: N
Lock_tables_priv: N
Execute_priv: N
Repl_slave_priv: Y
Repl_client_priv: Y
Create_view_priv: N
Show_view_priv: N
Create_routine_priv: N
Alter_routine_priv: N
Create_user_priv: N
Event_priv: N
Trigger_priv: N
Create_tablespace_priv: N
ssl_type:
ssl_cipher: 0x
x509_issuer: 0x
x509_subject: 0x
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin: mysql_native_password
authentication_string: *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA
password_expired: N
password_last_changed: 2020-12-03 01:07:52
password_lifetime: NULL
account_locked: N
Create_role_priv: N
Drop_role_priv: N
Password_reuse_history: NULL
Password_reuse_time: NULL
Password_require_current: NULL
User_attributes: NULL
1 row in set (0.01 sec)





















































































































mysql>

报错处理从库上出现故障 unblock with 'mysqladmin flush-hosts'报错

mysql> show slave status\G
1. row
Slave_IO_State: Connecting to master
Master_Host: 10.10.10.151
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000003
Read_Master_Log_Pos: 438
Relay_Log_File: mysql02-relay-bin.000002
Relay_Log_Pos: 4
Relay_Master_Log_File: binlog.000003
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 125
Relay_Log_Space: 156
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1129
Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60 retries: 5 message: Host '10.10.10.152' is blocked because of many connection errors;
unblock with 'mysqladmin flush-hosts'
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_UUID:
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 201203 01:51:49
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)






























































出现这个报错:
unblock with 'mysqladmin flush-hosts'

解决方法1:修改max_connect_errors的值
(1)进入Mysql数据库查看max_connect_errors:

show variables like '%max_connect_errors%';
(2)修改max_connect_errors的值:
set global max_connect_errors = 100;
(3)查看是否修改成功
show variables like '%max_connect_errors%';



解决方法2:使用mysqladmin flush-hosts 命令清理一下hosts文件
(1)在查找到的目录下使用命令修改:mysqladmin -u xxx -p flush-hosts
或者

flush hosts;

解决方法3:重启mysqld
也可以在重启之前,在配置文件中将该参数调大。

vi /etc/my.cnf

max_connect_errors = 100

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章