MySQL(Mariadb)总结10 - CentOS7 二进制安装以及多实例主从实战

  1. 二进制安装
tar xf mysql-5.5.61-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
ln -s /usr/local/mysql-5.5.61-linux-glibc2.12-x86_64 /usr/local/mysql

chown -R mysql.mysql /usr/local/mysql
mkdir -p /data/{3307,3308}/data
chown -R mysql.mysql /data/
  1. 添加环境变量
echo 'export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/scripts' >> /etc/profile
  1. 初始化数据库
mysql_install_db --datadir=/data/3307/data --basedir=/usr/local/mysql --user=mysql
mysql_install_db --datadir=/data/3308/data --basedir=/usr/local/mysql --user=mysql

如果是设置mysql 5.7的多实例,则初始化时使用如下命令替换上面的mysql_install_db

mysqld --initialize-insecure -datadir=/data/3307/data --basedir=/usr/local/mysql --user=mysql
mysqld --initialize-insecure -datadir=/data/3307/data --basedir=/usr/local/mysql --user=mysql
  1. 生成配置my.conf的配置文件,注意master和slave的server-id不能重复
# cat /data/3307/my.cnf  | grep -Ev "^#|^$"
[client]
port        = 3307
socket      = /data/3307/data/mysql.sock
[mysqld]
datadir         = /data/3307/data
port        = 3307
socket      = /data/3307/data/mysql.sock
skip-external-locking
key_buffer_size = 16K
max_allowed_packet = 1M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 128K
server-id   = 1
log-bin=mysql-bin
binlog_format=mixed
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
log-error=/data/3307/data/mysqld.log
pid-file=/data/3307/data/mysqld.pid

# cat /data/3308/my.cnf  | grep -Ev "^#|^$"
[client]
port        = 3308
socket      = /data/3308/data/mysql.sock
[mysqld]
datadir         = /data/3308/data
port        = 3308
socket      = /data/3308/data/mysql.sock
skip-external-locking
key_buffer_size = 16K
max_allowed_packet = 1M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 128K
server-id   = 3
log-bin=mysql-bin
binlog_format=mixed
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
log-error=/data/3308/data/mysqld.log
pid-file=/data/3308/data/mysqld.pid
  1. 生成多个实例的系统启动文件:
# cat /usr/lib/systemd/system/mysqld-3307.service  | grep -Ev "^#|^$"
[Unit]
Description=MySQL Community Server
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
Alias=mysql.service
[Service]
User=mysql
Group=mysql
PermissionsStartOnly=true
ExecStart=/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/3307/my.cnf
TimeoutSec=600
Restart=always
PrivateTmp=false

# cat /usr/lib/systemd/system/mysqld-3308.service  | grep -Ev "^#|^$"
[Unit]
Description=MySQL Community Server
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
Alias=mysql.service
[Service]
User=mysql
Group=mysql
PermissionsStartOnly=true
ExecStart=/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/3308/my.cnf
TimeoutSec=600
Restart=always
PrivateTmp=false
  1. 启动服务
systemctl start mysqld-3307 mysqld-3308
  1. 登录到3307设置为master
mysql -uroot -p -h 127.0.0.1 -P 3307

mysql> grant replication slave on *.* to 'fbo'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      248 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
  1. 登录到3308设置为slave
    Slave_IO_RunningSlave_SQL_Running判断是否成功;
mysql> change master to master_host='192.168.75.101',master_port=3307,master_user='fbo',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=248;
Query OK, 0 rows affected (0.00 sec)

mysql> slave start
    -> ;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.75.101
                  Master_User: fbo
                  Master_Port: 3307
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 248
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000001
             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: 248
              Relay_Log_Space: 410
              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: 1
1 row in set (0.00 sec)

推荐阅读更多精彩内容