详细步骤,请参考(), 阿铭只把简单步骤写一下。
根据阿铭提供的地址,假如你已经搭建好了一个mysql,跑的是3306端口,下面阿铭再搭建一个3307端口的mysql:
[root@localhost ~]# cd /usr/local/ [root@localhost local]# cp -r mysql mysql_2 [root@localhost local]# cd mysql_2
[root@localhost mysql_2]# cp /etc/my.cnf ./my.cnf [root@localhost mysql_2]# vim my.cnf
其中:
port = 3306
改为:
port = 3307
把:
socket = /tmp/mysql.sock
改为:
socket = /tmp/mysql_2.sock
在这一行的下面再加一行:
datadir = /data/mysql_2
保存后就可以初始化了
[root@localhost mysql_2]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql_2
最后一步是初始化数据库目录,如果出现两个 “OK” 并且生成/data/mysql_2目录才正确,否则请仔细查看错误信息,如果不能解决请到阿铭论坛()发帖咨询阿铭。拷贝配置文件到mysql_2下,并修改相关项目:
自己做启动脚本
cd /etc/init.d/
cp mysql mysqld2
vim mysql2
修改
basedir=/usr/local/mysql_2
datadir=/data/mysql_2
conf=$basedir/my.cnf 加上
# Try to find basedir in /etc/my.cnf
conf=$basedir/my.cnf
保存
/etc/init.d/mysql_2 start
mysql主从配置-2
登录
mysql -S /tmp/mysql.sock
创建库
create database db1
exit
将db1库中写去mysql库
mysqldump -S /tmp/mysql.sock mysql > 123.sql
mysql -S /tmp/mysql.sock db1 < 123.sql
vim /etc/my.cof
在[mysqld]部分查看是否有以下内容,如果没有则添加:
server-id=1log-bin=aming
除了这两行是必须的外,还有两个参数,你可以选择性的使用:
binlog-do-db=db1 #只正对db1做主从 binlog-ignore-db=mysql #除了mysql不坐主从
binlog-do-db=需要复制的数据库名,多个数据库名,使用逗号分隔。binlog-ignore-db=不需要复制的数据库库名,多个数据库名,使用逗号分隔。这两个参数其实用一个就可以啦。
如果修改过配置文件需要重启mysqld服务,否则不需要重启:
/ets/init.d/mysqld restart
进去mysql
mysql -S /tmp/mysql.sock
创建用户
grant replication slave on *.* to 'repl'@'127.0.0.1' identified by '123456';
然后
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+--------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------+----------+--------------+------------------+
| aming.000001 | 181 | | mysql |
+--------------+----------+--------------+------------------+
1 row in set (0.00 sec)
从新打开一个虚拟机为从
在从上
vim /usr/local/mysql_2/my.cnf
修改
server-id = 111
保存
/etc/init.d/mysql_2 restart
[root@mingming ~]# mysql -S /tmp/mysql_2.sock -e "create database db1"
[root@mingming ~]# mysql -S /tmp/mysql_2.sock db1 < 123.sql
mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='127.0.0.1',master_port=3306,master_user='repl',master_password='123456',master_log_file='aming.000001',master_log_pos=181;
Query OK, 0 rows affected (0.01 sec)
mysql> slave start;
Query OK, 0 rows affected (0.00 sec)
show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: aming.000001
Read_Master_Log_Pos: 181
Relay_Log_File: mingming-relay-bin.000003
Relay_Log_Pos: 247
Relay_Master_Log_File: aming.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: 181
Relay_Log_Space: 405
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:
1 row in set (0.00 sec)
ERROR:
No query specified
有两个YES就成功了
登录主
mysql> unlock tables;
mysql> use db1;
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_db1 |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
23 rows in set (0.00 sec)
主上删除一个表
mysql> drop table help_category;
Query OK, 0 rows affected (0.01 sec)
在从上查看是否被删除
mysql> use db1
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_db1 |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
22 rows in set (0.00 sec)
在住上创建一个表
不能在从上操作