一、準備工做mysql
groupadd mysql linux
useradd -r -g mysql mysql c++
mkdir /usr/local/mysql/ #建立mysql安裝目錄 sql
mkdir /data/mysql #建立數據存放目錄 數據庫
chown mysql:mysql -R /data/mysql bash
yum install -y gcc gcc-c++ libtool autoconf automake imake libxml2-devel expat-devel ncurses-devel cmake bison 服務器
wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.43.tar.gzide
二、解壓源碼包,編譯安裝學習
tar -zxvf mysql-5.5.43.tar.gz 測試
cd mysql-5.5.43 #進入解壓後mysql目錄
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/data/mysql/ -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306
make && make install #編譯經過過,安裝
三、初始化MySQL
cp support-files/my-medium.cnf /etc/my.cnf #複製配置文件
cp support-files/mysql.server /etc/init.d/mysqld #複製啓動腳本
chmod 755 /etc/init.d/mysqld
cd /usr/local/mysql #進入安裝目錄
chown -R mysql.mysql . # 受權
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql #初始化數據庫
/etc/init.d/mysqld start #啓動數據庫
四、MySQL配置
chkconfig --add mysqld #添加系統服務
chkconfig mysqld on #添加開機啓動
export PATH=$PATH:/usr/local/mysql/bin #添加環境變量
echo 'PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
service mysqld start/stop
mysql主從配置
192.168.2.96 salve
192.168.2.97 master
一、首先配置主服務器
[root@localhost ~]# service mysqld stop [root@localhost ~]# vi /etc/my.cnf ##在[mysqld]段內添加以下內容(其實默認就是須要檢查一下)
log-bin=mysql-bin ###########啓用二進制日誌#############
server-id=1 ###########服務器惟一ID###########
[root@localhost ~]# service mysqld start Starting MySQL.. [肯定] [root@localhost ~]# mysqladmin -uroot -p password "123456" #默認root用戶密碼爲空,修改密碼爲123456 這裏修改密碼也能夠直接登陸進去 在從新受權 Enter password: [root@localhost ~]# mysql -uroot -p ##登錄mysql終端,受權給從服務器,
mysql> grant replication slave on *.* to 'root'@'192.168.2.96' identified by '123456'; Query OK, 0 rows affected (0.01 sec) mysql> flush privileges; ###刷新 Query OK, 0 rows affected (0.00 sec) mysql> show master status; ##查詢主數據庫狀態 +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000004 | 471 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) ###這裏須要記錄 File 以及 Position 的值,在操做從服務器時會用到,以當時終端的爲準###
二、配置從服務器
[root@localhost ~]# service mysqld stop ##中止mysql數據庫 Shutting down MySQL. [肯定] [root@localhost ~]# vi /etc/my.cnf ##在[mysqld]段內添加以下內容 [mysqld] log-bin=mysql-bin ###########啓用二進制日誌############# server-id=2 ###########服務器惟一ID########### [root@localhost ~]# service mysqld start ##啓動數據庫 Starting MySQL.. [肯定] [root@localhost ~]# mysqladmin -uroot -p password "123456" #默認root用戶密碼爲空,修改密碼爲123456 Enter password: [root@localhost ~]# mysql -uroot -p ###登錄mysql終端,
###執行同步SQL語句###
mysql> change master to
-> master_host='192.168.2.97',
-> master_user='root',
-> master_password='123456',
-> master_log_file='mysql-bin.000004',
-> master_log_pos=471;
注意:這裏完成後最後刷新權限 不然下面查看主從同步狀態時顯示:Slave_IO_Running: Connecting
mysql> flush privileges; ###刷新權限 Query OK, 0 rows affected (0.00 sec)
###啓動Slave 同步進程####
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
####主從同步檢查####
mysql> show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
表明mysql主從搭建成功,測試主從服務器是否可以同步,就本身簡單的測試一下便可 這裏就不作演示了。
mysql主主配置
接着上面的繼續簡單操做幾步便可完成主主配置(其操做步驟和搭建主從同樣)
在從服務器上操做以下 (此次從服務器實際上是當作主)
mysql> grant replication slave on *.* to 'root'@'192.168.2.97' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000004 | 700 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
192.168.2.97服務器以下操做:
mysql> change master to master_host='192.168.2.96', master_user='root', master_password='123456', master_log_file='mysql-bin.000004', master_log_pos=700; Query OK, 0 rows affected (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> start slave; 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.2.96 Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 700 Relay_Log_File: linux-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: 700 Relay_Log_Space: 409 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: 2 1 row in set (0.00 sec)
OK!到這裏mysql主主搭建完成
因爲博主比較懶 mysql環境搭建就寫在一個博文裏面了 測試的話就本身簡單測試下便可。有錯誤的地方請指出來 一塊學習進步。