理論知識
mysql 同步原理
mysql同步方案
讀寫分離方案
mysql linux 免安裝配置
安裝前必須刪除原來的安裝
需要檢查 以下文件是否存在 ,如果存儲則要刪除之
/etc/my.cnf
/etc/init.d/mysqld
mysql 依賴的庫
- shell> yum search libaio # search for info
- shell> yum install libaio # install library
創建mysql 與用戶組,-s /bin/false 表示該用戶不能登錄
- shell> groupadd mysql
- shell> useradd -r -g mysql -s /bin/false mysql
解壓安裝包至指定目錄
- shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
- shell> ln -s full-path-to-mysql-VERSION-OS mysql
- shell> cd mysql
爲 mysql 用戶添加權限
- shell> chown -R mysql ./
- shell> chgrp -R mysql ./
- #創建data目錄並添加權限
- shell> mkdir -p /data/mysql
- shell> chown -R mysql:mysql /data/mysql
拷貝配置文件
- shell> cp ${basedir}/support-files/my-medium.cnf /etc/my.cnf
- #修改配置
- [mysqld]
- basedir=/home/cbt/svr/mysql
- datadir=/data/mysql
- character-set-server=utf8
- shell> cp support-files/mysql.server /etc/init.d/mysql
初始化 mysql 庫
- shell> ./scripts/mysql_install_db --user=mysql --basedir=/home/cbt/svr/mysql --datadir=/data/mysql
#添加環境變量
- shell> vi /etc/profile
- PATH=/home/cbt/svr/mysql/bin:$PATH
- export PATH
- #讓剛纔的修改生效
- shell> source /etc/profile
啓動及其它配置 #啓動數據庫
- service mysql start
- #開機啓動
- chkconfig mysqld on
- #初始化mysql的一些設置
- mysql_secure_installation
- #回車
- Enter current password for root (enter for none):
- #y,設置mysql的root密碼
- Set root password?[Y/n] y
- #以下都yes
- Remove anonymous users?[Y/n] y
- Disallow root login remotely?[Y/n] y
- Remove test database and access to it?[Y/n] y
- Reload privilege tables now?[Y/n] y
- ThanksforusingMySQL!
允許遠程登陸
- mysql> use mysql;
- mysql> select host,user,password from user;
- mysql> update user set password=password('123456') where user='root';
- mysql> update user set host='%' where user='root' and host='localhost';
- mysql> flush privileges;
安裝時的一些錯誤
-bash: ./scripts/mysql_install_db: /usr/bin/perl: bad interpreter: 沒有那個文件或目錄
解決: yum -y install perl perl-devel
Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
解決:yum -y install libaio-devel
一主多從配置
master my.cnf 配置
- #需要同步的二進制數據庫名;
- binlog-do-db=tuling
- #不同步的二進制數據庫名,如果不設置可以將其註釋掉;
- binlog-ignore-db=information_schema
- binlog-ignore-db=mysql
- binlog-ignore-db=personalsite
- binlog-ignore-db=test
- #以下參數可選----------
- #binlog 格式
- binlog-format=ROW
- log-bin=mysql-master-bin
- #slave更新時是否記錄到日誌中;
- log-slave-updates=true
- #開啓半同步,需要另外安裝插來支持
- rpl_semi_sync_master_enabled=ON
創建用戶用於主從同步的數據庫
- grant replication slave,super,reload on *.* to slave1@192.168.17.201 identified by '123456';
查看主節點狀態
- mysql> show master status
- #在主庫上查看已連接的slave主機
- mysql> show slave hosts;
- #查看所有binlog日誌
- mysql> show binary logs;
- #查看所有binlog 事件
- mysql> show binlog events in 'mysql-bin.000003' from 145 \G;
slave my.cnf 配置-----
- server-id = 2
- log-bin=mysql-slave-bin
- replicate-do-db=tuling
- replicate-ignore-db=information_schema
- replicate-ignore-db=mysql
- replicate-ignore-db=personalsite
- replicate-ignore-db=test
slave 節點修改master 配置
- mysql>change master to master_host='192.168.0.101', master_user='slave1', master_password='123456' ,MASTER_AUTO_POSITION = 2887;
Slave 相關操作
- #啓動slave
- mysql>start slave;
- mysql>stop slave;
- #查看 slave 狀態
- show slave status\G;
- #跳過指定數量錯誤
- SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;
- #查看 relaylog 事件
- show relaylog events in 'localhost-relay-bin.000019'
讀寫分離實現 360 Atlas
安裝與卸載Atlas
atlas下載
鏈接:https://pan.baidu.com/s/1zXCYzayszOBCAxZPnLOMVw 提取碼:2s8b
- #安裝
- shell> rpm -i Atlas-2.2.1.el6.x86_64.rpm
- #卸載
- shell> rpm -e Atlas-2.2.1.el6.x86_64.rpm
安裝目錄在 /usr/local/mysql-proxy/ 當中
Atlass配置
- #Atlas後端連接的MySQL主庫的IP和端口,可設置多項,用逗號分隔
- proxy-backend-addresses = 127.0.0.1:3306
- #Atlas後端連接的MySQL從庫的IP和端口,@後面的數字代表權重,用來作負載均衡,若省略則默認爲1,可設置多項,用逗號分隔
- #proxy-read-only-backend-addresses = 127.0.0.1:3305@1
- #用戶名與其對應的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程序encrypt加密。用戶密碼所有主庫和從庫都必須一至
- pwds = root:/iZxz+0GRoA=
- #Atlas監聽的工作接口IP和端口
- proxy-address = 0.0.0.0:1234
啓動與關閉Atlas
- ./mysql-proxyd test start
- ./mysql-proxyd test stop
以代理方式進行管理員登錄
- mysql -h127.0.0.1 -P2345 -uroot -proot