lihongjie0209 / myblog

4 stars 0 forks source link

MySQL: 手动安装主从 #211

Open lihongjie0209 opened 3 years ago

lihongjie0209 commented 3 years ago

下载二进制安装包

wget https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.30-linux-glibc2.12-x86_64.tar

创建Mysql用户


shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql

解压安装包

tar xvf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz

ln -s mysql-5.7.30-linux-glibc2.12-x86_64 mysql

创建软连接到bin目录


ls /opt/mysql/bin/ | xargs -I{} ln -s /opt/mysql/bin/{} /usr/bin/{}

创建数据目录

[root@localhost opt]# mkdir -p /data/mysql
[root@localhost opt]# chown -R  mysql:mysql /data/mysql/
[root@localhost support-files]# chown -R  mysql:mysql /opt/mysql/

初始化Mysql

在my.conf中添加

basedir=/opt/mysql/
datadir=/data/mysql/

然后执行

mysqld  --initialize-insecure --user=mysql

启动mysql

cp /opt/mysql/support-files/mysql.server /etc/init.d/mysqld

systemctl enable mysqld
systemctl start mysqld
systemctl status mysqld.service
lihongjie0209 commented 3 years ago

启动binlog 和 serverid

[mysqld]
log-bin=mysql-bin
server-id=1

重启之后的效果:

mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 154
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

ERROR: 
No query specified
lihongjie0209 commented 3 years ago

导入测试数据库

下载 https://github.com/datacharmer/test_db 到服务器

  116  unzip test_db-master.zip -d test_db
  117  cd test_db/test_db-master/

  120  mysql -u root -pmysql -P3306 -t < employees.sql 
  121  mysql -u root -pmysql -P3306 -h 127.0.0.1 -t < employees.sql 

查看master 状态:

mysql> show master status \G
*************************** 1. row ***************************
             File: mysql-bin.000002
         Position: 66374800
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)