首页 > 代码库 > 普通用户Mysql 5.6.13 主从
普通用户Mysql 5.6.13 主从
Master:192.168.209.19
Slave:192.168.209.20
mysql版本:mysql5.6.13
1. 以root身份创建普通用户,如mysql,并创建mysql安装目录:
# useradd mysql
# passwd mysql
# mkdir /mysql
# chown mysql:mysql /mysql
安装依赖包:
yum -y install gcc gcc-c++ autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel* make cmake
2. 下载MySQL5.6.13完整开发版, 并将其拷贝到/mysql目录下.
文件: mysql-5.6.13.tar.gz
3. 以mysql用户解包MySQL,并将MySQL解包后的目录更改为/mysql/:
# su - mysql
$ cd
$ mv mysql-5.6.13.tar.gz /mysql
$ tar xvzf mysql-5.6.13.tar.gz
4. 配置MySQL:
$ cd /mysql/5.6.13
$cmake -DCMAKE_INSTALL_PREFIX=/mysql/mysql-5.6
$make
$make install
$cd /mysql/mysql-5.6/scripts
$ ./mysql_install_db --user=mysql --basedir=/mysql/mysql-5.6 --datadir=/mysql/mysql-5.6/data
#cp /mysql/mysql-5.6/support-files/mysql.server /etc/init.d/mysql
#cp /mysql/mysql-5.6/support-files/my-default.cnf /etc/my.cnf
启动mysql
$/mysql/mysql-5.6/support-files/mysql.server start
error解决:
[ERROR] Fatal error: Can‘t open and lock privilege tables: Table ‘mysql.user‘ doesn‘t exist
初始化数据库就ok,即:./mysql_install_db --user=mysql --basedir=/mysql/mysql-5.6 --datadir=/mysql/mysql-5.6/data
5.配置主从:
5.1、主从服务器分别操作:
5.1.1、mysql版本一致
5.1.2、初始化表,启动mysql
5.1.3、设置mysql root密码, /mysql/mysql-5.6/bin/mysqladmin -u root password ‘123456’
5.2、修改master:
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin #启用二进制日志
server-id=20 #服务器ID标示,主从不一致
5.3、修改slave:
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin #启用二进制日志
server-id=21 #服务器ID标示,主从不一致
5.2 登录mysql master配置
#/mysql/mysql-5.6/bin/mysql -uroot -p123456
mysql>GRANT REPLICATION SLAVE ON *.* to ‘test‘@‘%‘ identified by ‘123456‘;
#一般不用root帐号,“%”表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如192.168.209.20,加强安全。
6、登录主服务器的mysql,查询master的状态
mysql>show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000007 | 320 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
注:获取file和position。
7、配置从服务器mysql Slave:
mysql>change master to master_host=‘192.168.209.19‘,master_user=‘test‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000007‘,master_log_pos=320; #320为主服务器的position,无单引号。
Mysql>start slave; //启动从服务器复制功能
8、检查slave状态:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.209.19
Master_User: mysync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 320
Relay_Log_File: cdn20-relay-bin.000002
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000007
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: 320
Relay_Log_Space: 456
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: 20
Master_UUID: 6a36aa69-1c81-11e4-8213-b8ca3af2484f
Master_Info_File: /mysql/mysql-5.6/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
Slave_IO及Slave_SQL进程YES状态,否则都是错误的状态(如:其中一个NO均属错误)。
至此,主从服务器配置完成。
9、主从服务器测试:
主服务器Mysql,建立数据库,并在这个库中建表插入一条数据:
mysql> create database test1;
Query OK, 1 row affected (0.00 sec)
mysql> use test1
Database changed
mysql> create table test1(id int(4),name char(8));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into test values(001,‘test1‘);
ERROR 1146 (42S02): Table ‘test1.test‘ doesn‘t exist
mysql> insert into test1 values(001,‘test1‘);
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| test1 |
+--------------------+
5 rows in set (0.00 sec)
从服务器Mysql查询:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| test1 |
+--------------------+
5 rows in set (0.01 sec)
至此,mysql主从配置完成,可以正常使用;后续会加入集群模式。