mac下安装多个mysql实例
多实例安装
首先上mysql官网https://dev.mysql.com/downloads/mysql/下载最新版的mysql,由于要安装多个mysql实例,这里我选择压缩包版本的mysql手动安装。
下载macOS 10.14 (x86, 64-bit), TAR这个版本后,运行tar -xvf命令进行解压,这里我解压到~/software/mysql-bin/mysql
如果需要的话,可以将mysql目录设置软链接到/usr/local/mysql,类似于这样 ln -s ~/software/mysql-bin/mysql /usr/local/mysql,这一步需要root权限
接下来,可以根据需要创建的mysql实例个数来创建mysql数据目录,这里我要创建两个实例,所以我新建了两个目录~/software/mysql-cluster/master和~/software/mysql-cluster/slave
然后将mysql执行目录配置到环境变量中
执行以下两条初始化命令,加上--initialize-insecure参数则生成的root用户没有密码,否则mysql初始化时随机生成一个密码并输入到日志文件中
mysqld --datadir=~/software/mysql-cluster/mysql-master/data --initialize --initialize-insecure
mysqld --datadir=~/software/mysql-cluster/mysql-slave/data --initialize --initialize-insecure
由于要启动多个实例,需要用到mysqld_multi命令,多个数据库实例公用一个配置文件,如下所示
[mysqld_multi] #mysqld = /usr/local/mysql/bin/mysqld_safe mysqladmin = /usr/local/mysql/bin/mysqladmin user = root password = root [mysqld3307] server-id=3307 port=3307 log-bin=mysql-bin log-error=~/software/mysql-cluster/mysql-master/mysqld.log tmpdir=~/software/mysql-cluster/mysql-master slow_query_log=on slow_query_log_file =~/software/mysql-cluster/mysql-master/mysql-slow.log long_query_time=1 socket=~/software/mysql-cluster/mysql-master/mysql_3307.sock pid-file=~/software/mysql-cluster/mysql-master/mysql.pid basedir=~/software/mysql-cluster/mysql-master datadir=~/software/mysql-cluster/mysql-master/data [mysqld3308] server-id=3308 port=3308 log-bin=mysql-bin log-error=~/software/mysql-cluster/mysql-slave/mysqld.log tmpdir=~/software/mysql-cluster/mysql-slave slow_query_log=on slow_query_log_file =~/software/mysql-cluster/mysql-slave/mysql-slow.log long_query_time=1 socket=~/software/mysql-cluster/mysql-slave/mysql_3308.sock pid-file=~/software/mysql-cluster/mysql-slave/mysql.pid basedir=~/software/mysql-cluster/mysql-slave datadir=~/software/mysql-cluster/mysql-slave/data read_only=1 [mysqld] character_set_server=utf8
接下来,运行mysqld_multi --default-files=cluster.conf start即可启动所有mysql实例
配置主从复制
连接上主mysql,创建用于复制的用户并赋予复制权限:
mysql> create user 'copy'@'%' identified by 'copy'; Query OK, 0 rows affected (0.07 sec) mysql> grant replication slave on *.* to 'copy'@'%'; Query OK, 0 rows affected (0.02 sec)
执行show master status命令
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 1166 | | | |
+------------------+----------+--------------+------------------+-------------------+
连接从mysql,设置master信息并启动slave:
mysql> change master to master_host='127.0.0.1', master_port=3307,master_user='copy',master_password='copy',master_log_file='mysql-bin.000002',master_log_pos=1166; Query OK, 0 rows affected, 1 warning (0.10 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec)
执行show slave status命令,查看slave状态,发现Slave_IO_State是Waiting for master to send event,Slave_IO_Running和Slave_SQL_Running状态是YES就说明设置成功
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 127.0.0.1 Master_User: copy Master_Port: 3307 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 1166 Relay_Log_File: 192-relay-bin.000002 Relay_Log_Pos: 322 Relay_Master_Log_File: mysql-bin.000002 Slave_IO_Running: Yes Slave_SQL_Running: Yes