当前位置:   article > 正文

mysql主从搭建_mysql8.0.37安装及配置超详细教程

mysql8.0.37安装及配置超详细教程

1、下载mysql安装包(都要)

https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.37-linux-glibc2.12-x86_64.tar.xz

mysql-8.0.37-linux-glibc2.12-x86_64.tar.xz

2、执行安装(都要)

  1. tar xvJf mysql-8.0.37-linux-glibc2.12-x86_64.tar.xz
  2. cp -r mysql-8.0.37-linux-glibc2.12-x86_64 /usr/local/
  3. cd /usr/local/
  4. mv mysql-8.0.37-linux-glibc2.12-x86_64 mysql
  5. groupadd mysql
  6. useradd -g mysql -s /sbin/nologin mysql
  7. chown -R mysql.mysql /usr/local/mysql
  8. mkdir /data
  9. cd /data
  10. mkdir -p /data/{mysqldata,mysqllog,mysqltmp}
  11. chown -R mysql.mysql /data/{mysqldata,mysqllog,mysqltmp}

3、在/data目录,创建my.cnf文件,写入以下内容:(都要,除server-id以外,其他都先不动,server-id需要每台机器不一样)

  1. [mysqld]
  2. datadir=/data/mysqldata/
  3. secure-file-priv=/data/mysqltmp
  4. tmpdir=/data/mysqltmp
  5. log-error=/data/mysqllog/mysqld.log
  6. pid-file=/usr/local/mysql/mysqld.pid
  7. socket=/usr/local/mysql/mysql.sock
  8. #read_only = 1
  9. log_timestamps = SYSTEM
  10. slow_query_log=1
  11. long_query_time=5
  12. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  13. explicit_defaults_for_timestamp
  14. skip-name-resolve
  15. skip-character-set-client-handshake
  16. skip-host-cache
  17. skip-external-locking
  18. skip-slave-start
  19. innodb_buffer_pool_size = 10240M
  20. innodb_sort_buffer_size = 2048M
  21. innodb_read_io_threads = 4
  22. innodb_write_io_threads = 4
  23. innodb_thread_concurrency = 16
  24. innodb_flush_log_at_trx_commit = 2
  25. innodb_log_buffer_size = 64M
  26. innodb_log_file_size = 128M
  27. innodb_log_files_in_group = 4
  28. #innodb_lock_wait_timeout = 200
  29. #innodb_flush_method=O_DIRECT
  30. innodb_adaptive_flushing=ON
  31. innodb_max_dirty_pages_pct=80
  32. innodb_io_capacity=1000
  33. lower_case_table_names=1
  34. wait_timeout=28800
  35. interactive_timeout=28800
  36. character-set-server=utf8
  37. collation-server=utf8_general_ci
  38. log-bin-trust-function-creators=1
  39. max_connections=1024
  40. max_allowed_packet=512M
  41. back_log=500
  42. event_scheduler=on
  43. innodb_temp_data_file_path = ibtmp1:12M:autoextend:max:10G
  44. # Disabling symbolic-links is recommended to prevent assorted security risks
  45. symbolic-links=0
  46. server-id=1
  47. gtid_mode=ON
  48. enforce_gtid_consistency=TRUE
  49. log-bin=mysql-bin-A
  50. binlog-ignore-db=mysql
  51. binlog-ignore-db=information_schema
  52. binlog-ignore-db=performance_schema
  53. binlog-ignore-db=sys
  54. #log-slave-updates
  55. sync_binlog=1
  56. auto_increment_offset=1
  57. auto_increment_increment=1
  58. expire_logs_days=4
  59. binlog_format=row
  60. slave_parallel_type=LOGICAL_CLOCK
  61. slave_parallel_workers=4
  62. master_info_repository = TABLE
  63. relay_log_info_repository = TABLE
  64. #federated
  65. #slave-skip-errors=1061,1062,1305,1032
  66. [mysqldump]
  67. quick
  68. max_allowed_packet = 1G
  69. [mysql]
  70. no-auto-rehash
  71. socket = /usr/local/mysql/mysql.sock
  72. [myisamchk]
  73. key_buffer_size = 20M
  74. sort_buffer_size = 20M
  75. read_buffer = 2M
  76. write_buffer = 2M
  77. [mysqlhotcopy]
  78. interactive-timeout
  79. mkdir -p /data/{mysqldata,mysqllog,mysqltmp}
  80. chown -R mysql.mysql /data/{mysqldata,mysqllog,mysqltmp}

4、初始化mysql,启动mysql(都要)

  1. # 初始化 如果报错可能是系统和mysql版本不兼容,我这里是centos7最小安装
  2. /usr/local/mysql/bin/mysqld --defaults-file=/data/my.cnf --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysqldata
  3. # 启动
  4. /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/my.cnf --pid-file=/usr/local/mysql/mysqld.pid --datadir=/data/mysqldata &

5、查看密码(都要)

执行如下命令查看日志,找到图中类似位置,为root密码

more /data/mysqllog/mysqld.log

6、登录mysql(都要)

执行如下命令,并输入密码即可

/usr/local/mysql/bin/mysql -uroot -p -S /usr/local/mysql/mysql.sock

7、修改管理员密码(都要)

alter user 'root'@'localhost' identified by '你的管理员密码';

以上是mysql正常搭建步骤,下面开始主从配置

1、在主库上创建一个有权限的用户,用于从库登录主库进行复制

  1. # 在主库上:
  2. create user 'repl'@'192.168.137.%' identified with mysql_native_password by 'Repl@001';
  3. grant replication slave on *.* to 'repl'@'192.168.137.%';
  4. flush privileges;
  5. # 查看主库日志名称和读写位置
  6. show master status;
  7. +--------------------+----------+--------------+-------------------------------------------------+------------------------------------------+
  8. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  9. +--------------------+----------+--------------+-------------------------------------------------+------------------------------------------+
  10. | mysql-bin-A.000002 | 2053 | | mysql,information_schema,performance_schema,sys | 34bfd5a9-0dae-11ef-ad2d-00155d24fe02:1-8 |
  11. +--------------------+----------+--------------+-------------------------------------------------+------------------------------------------+

2、配置从库的用户信息

  1. # 配置主库地址,账号,日志名,位置
  2. change master to master_host='192.168.137.8',master_user='repl',master_password='Repl@001',master_log_file='mysql-bin-A.000002',master_log_pos=2053,get_master_public_key=1;
  3. # 启动主从同步
  4. start slave;
  5. # 查看主从同步状态,两个yes,文件名和主库一致,然后数据位置一样即可
  6. show slave status;

完成

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号