mysql多實例-主從複製安裝

安裝環境:Centos6.5html

mysql版本:mysql-5.5.32.tar.gzmysql

一:安裝前準備:c++

1.安裝一些依賴庫sql

yum install cmake gcc gcc-c++ ncurses-devel bison perl zilb-devel libio-devel perl-devel -y

2.建立mysql用戶與用戶組數據庫

[root@oracle ~]# groupadd mysql
[root@oracle ~]# useradd -g mysql mysql
[root@oracle ~]# id mysql
uid=501(mysql) gid=501(mysql) 組=501(mysql)

3.建立mysql數據存放目錄,設定安裝兩個實例3306,3307bash

[root@oracle ~]# mkdir -p /data/{3306,3307}/data
[root@oracle ~]# tree /data
/data
├── 3306
│   └── data
└── 3307
    └── data

4.編譯安裝服務器

tar zxvf mysql-5.5.32.tar.gz 
cd mysql
-5.5.32 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DDEFAULT-CHARSET=utf8 -DMYSQL_USER=mysql make make install

5.分別複製my.cnf配置文件和mysql啓動腳本到/data/3306和3307下oracle

[root@oracle support-files]# cp my-huge.cnf /data/3306/my.cnf
[root@oracle support-files]# cp my-huge.cnf /data/3307/my.cnf
[root@oracle support-files]# cp mysql.server /data/3306/mysql
[root@oracle support-files]# cp mysql.server /data/3307/mysql

6.修改/data目錄屬主socket

[root@oracle ~]# chown -R mysql:mysql /data

7.修改3306,3307實例的配置文件tcp

以3306實例爲例,3307修改相應位置內容
# The following options will be passed to all MySQL clients [client] #password
= your_password port = 3306 socket = /data/3306/mysql.sock # The MySQL server [mysqld] port = 3306 socket = /data/3306/mysql.sock skip-external-locking key_buffer_size = 384M max_allowed_packet = 1M table_open_cache = 512 sort_buffer_size = 2M read_buffer_size = 2M read_rnd_buffer_size = 8M myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size = 32M # Try number of CPU's*2 for thread_concurrency thread_concurrency = 8 datadir=/data/3306/data
.......略過一段..........

  log-bin=mysql-bin     #開啓二進制日誌 ,從服務器不須要開啓二進制日誌,3307實例註釋掉這行

  server-id = 1         #實例的server-id不能相同,能夠設置3307的server-id = 2 

my.cnf文件末尾添加 [mysqld_safe]段,定義每一個實例的錯誤日誌文件,pid進程位置等

   [mysqld_safe]

   log-error=/data/3306/3306.err
   pid-file=/data/3306/mysqld.pid

8,修改3306,3307實例的啓動腳本文件內容

1.找到basedir=和datadir=兩行,在後面加上mysql的安裝目錄路徑和數據存放路徑
basedir=/usr/local/mysql datadir=/data/3306/data

2.定位到283行:

$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 &

改成:

$bindir/mysqld_safe --datadir="$datadir/my.cnf" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 &

 

3307實例也修改對應位置

9.給3306,3307的啓動腳本執行權限

[root@oracle 3306]# chmod 755 mysql 
[root@oracle 3307]# chmod 755 mysql 

10.初始化數據庫

[root@oracle ~]# cd /usr/local/mysql/scripts/
[root@oracle scripts]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/3306/data
[root@oracle scripts]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/3307/data
到數據存放目錄查看一下

[root@oracle ~]# cd /data/3306/data/      #一樣查看3307
[root@oracle data]# ls
mysql performance_schema test
[root@oracle data]#

11.啓動mysql

啓動3306實例

[root@oracle ~]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/3306/my.cnf 2>&1 > /dev/null &
[1] 6083

查看一下
[root@oracle ~]# netstat -tunlp | grep mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 6372/mysqld
啓動3307實例

[root@oracle ~]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/3307/my.cnf 2>&1 > /dev/null &
[2] 6393
[root@oracle ~]# netstat -tnlp | grep mysqld
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 6670/mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 6372/mysqld
啓動成功。

再看一下/data目錄下

12.給數據庫設置密碼提示錯誤 -bash: mysqladmin: command not found

[root@oracle 3306]# mysqladmin
-bash: mysqladmin: command not found
[root@oracle 3306]# 

執行指令系統默認是到 /usr/sbin 下尋找調用指令的,將/usr/local/mysql/bin/ 下的幾個經常使用管理命令建立軟鏈接到/usr/sbin下,之後使用mysql指令就不須要用絕對路徑了

[root@oracle 3306]# ln -s /usr/local/mysql/bin/mysqladmin /usr/sbin/
[root@oracle 3306]# ln -s /usr/local/mysql/bin/mysqld_safe /usr/sbin/
[root@oracle 3306]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/

設置密碼

[root@oracle 3306]# mysqladmin -u root password root123 -s /data/3306/mysql.scok
[root@oracle 3306]# mysqladmin -u root password root123 -s /data/3307/mysql.scok

登陸數據庫出現錯誤,登陸被拒絕。

[root@oracle 3306]# mysql -uroot -p -S /data/3306/mysql.sock
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
登陸被拒絕...這個讓我鬱悶了一會...
先跳過權限表登陸,再修改密碼

  mysqld_safe --defaults-file=/data/3306/my.cnf --skip-grant-table &

  再登陸

  mysql -uroot -S /data/3306/mysql.sock

[root@oracle 3306]# mysql -uroot -S /data/3306/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.32-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select user,password,host from mysql.user;
+------+----------+-----------+
| user | password | host |
+------+----------+-----------+
| root | | localhost |
| root | | oracle |
| root | | 127.0.0.1 |
| root | | ::1 |
| | | localhost |
| | | oracle |
+------+----------+-----------+
6 rows in set (0.03 sec)

(鬱悶:前面設置的密碼這裏查詢怎麼是爲空呢。。。。)

先刪除user名稱爲空的用戶

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> delete from user where user='';
Query OK, 2 rows affected (0.03 sec)

修改密碼

mysql> update user set password=PASSWORD('root123456') where user = 'root';
Query OK, 4 rows affected (0.04 sec)
Rows matched: 4 Changed: 4 Warnings: 0

退出後重啓一下服務,登陸成功(幾乎每次剛安裝好後登陸都會遇到這個問題。。。)

這裏總結一下多實例的啓動,關閉,登陸的指令:
啓動指令:mysqld_safe --defaults-file=/data/3306/my.cnf 2>&1 > /dev/null &
關閉指令:mysqladmin -S /data/3306/mysql.sock shutdown (有密碼要指定-u , -p選項)
登陸指令:mysql -uroot -p -S /data/3306/mysql.sock
指定實例登陸跳過權限表:
跳過權限:mysqld_safe --defaults-file=/data/3306/my.cnf --skip-grant-tables
跳過權限登陸:mysql -uroot -S /data/3306/mysql.sock

//////////////////////////////////////***************************//////////////////////////////////////////////////////////////////////////

同理,3307實例也遇到了登陸問題,使用一樣的方式解決便可。下面開始進行主從複製配置。

主庫端的配置:

1.主庫中添加一個用於主從之間通訊的用戶並受權

mysql> grant file on *.* to 'backup'@'192.168.%.%' identified by 'root123456';

  mysql> grant replication slave on *.* to 'backup'@'192.168.%.%' identified by 'root123456';

2.檢查一下主庫二進制是否開啓了

mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |
+---------------+-------+
1 row in set (0.01 sec)
再查看當前二進制記錄信息

  mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 410 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

從庫端配置

在從庫執行master change to 配置主從庫鏈接參數(忘記指令能夠在mysql中執行help master change to查看)
mysql> use mysql;

mysql> change master to \

-> master_host='192.168.214.133'\
-> master_user='backup'\
-> master_password='root123456'\
-> master_log_file='mysql-bin.000004'\
-> master_log_pos=410;

啓動從庫slave

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

查看slave是否是啓動正常了

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.214.133
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 410
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000004
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: 410
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)

接下來,在主庫中建立一個數據庫,測試主從複製功能。

mysql> show tables;
+----------------+
| Tables_in_demo |
+----------------+
| test           |
+----------------+
1 row in set (0.00 sec)

mysql> desc test;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | varchar(20) | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into test values ('344','testname');
Query OK, 1 row affected (0.36 sec)

mysql> select * from test;
+------+----------+
| id   | name     |
+------+----------+
| 344  | testname |
+------+----------+
1 row in set (0.44 sec)

mysql

再看從庫同步的信息

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| demo               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> select * from demo.test;
+------+----------+
| id   | name     |
+------+----------+
| 344  | testname |
+------+----------+
1 row in set (0.01 sec)

能夠看到主從庫信息同步也正常。

 在有些狀況下,從庫不須要同步主庫的全部數據庫,有時只須要同步主庫裏的指定的數據庫,那麼在從庫的my.cnf中添加 replicate-do-db=dbname 指定要同步的庫名稱便可。

這個問題能夠參考 http://blog.csdn.net/goustzhu/article/details/9339621 文章。

或者 http://bbs.chinaunix.net/thread-1187327-1-1.html

相關文章
相關標籤/搜索