日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

DBA(四):数据读写分离,MySQL多实例操作

發(fā)布時(shí)間:2025/3/21 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DBA(四):数据读写分离,MySQL多实例操作 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

數(shù)據(jù)讀寫分離

MySQL讀寫分離

概念:把客戶端查詢數(shù)據(jù)的請(qǐng)求和存儲(chǔ)數(shù)據(jù)的SQL命令,分別給不同的數(shù)據(jù)庫(kù)服務(wù)器處理

讀寫分離的原理

  • 由MySQL代理面向客戶端提供服務(wù)
  • 收到SQL的寫請(qǐng)求時(shí),交給master服務(wù)器處理
  • 收到SQL的讀請(qǐng)求時(shí),交給slave服務(wù)器處理

讀寫分離拓?fù)鋱D

部署maxscale服務(wù)

Maxscale代理軟件

  • 由MariaDB公司開(kāi)發(fā)
  • 下載地址
  • 軟件包在此提取碼:5x5c

構(gòu)建MySQL讀寫分離

  • 部署MySQL一主一從結(jié)構(gòu)
    • 主服務(wù)器 192.168.4.51
    • 從服務(wù)器 192.168.4.52
  • 部署MySQL代理服務(wù)器
    • 裝包、修改配置文件、啟動(dòng)服務(wù)
  • 測(cè)試配置
    • 客戶端連接代理服務(wù)訪問(wèn)數(shù)據(jù)
    1.配置MySQL主從同步:host52數(shù)據(jù)庫(kù)服務(wù)器作為host51的從服務(wù)器host51主機(jī):[root@host51 ~]# vim /etc/my.cnf [mysqld] server_id=51 #指定服務(wù)器id號(hào) log_bin=master51 #啟用binlog日志,并指定文件名前綴 [root@host53 mysql]# systemctl restart mysqld [root@host51 ~]# mysql -uroot -p123456 mysql> grant replication slave on *.* to repluser@"%" identified by "123qqq...A"; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> show master status; +-----------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-----------------+----------+--------------+------------------+-------------------+ | master51.000003 | 365 | | | | +-----------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)host52主機(jī):[root@host52 ~]# vim /etc/my.cnf[mysqld]server_id=52 [root@host54 ~]# systemctl restart mysqld [root@host52 ~]# mysql -uroot -p123456mysql> change master to-> master_host="192.168.4.51",-> master_user="repluser",-> master_password="123qqq...A",-> master_log_file="master51.000003",-> master_log_pos=365; Query OK, 0 rows affected, 2 warnings (0.08 sec)mysql> start slave; Query OK, 0 rows affected (0.01 sec)mysql> show slave status \G *************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.4.51Master_User: repluserMaster_Port: 3306Connect_Retry: 60Master_Log_File: master51.000003Read_Master_Log_Pos: 154Relay_Log_File: host52-relay-bin.000011Relay_Log_Pos: 365Relay_Master_Log_File: master51.000003Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 154Relay_Log_Space: 6474Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 51Master_UUID: 6d7b632c-4e3f-11ea-8a8d-000c29875030Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)此時(shí)做初步測(cè)試:首先在host51主機(jī)上查看授權(quán)用戶的權(quán)限 mysql> show grants for repluser@"%"; +--------------------------------------------------+ | Grants for repluser@% | +--------------------------------------------------+ | GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'%' | +--------------------------------------------------+ 1 row in set (0.00 sec)mysql> create database aaa; Query OK, 1 row affected (0.06 sec)mysql> create table aaa.b(name char(10)); Query OK, 0 rows affected (0.24 sec)mysql> insert into aaa.b values("zzz"); Query OK, 1 row affected (0.11 sec)mysql> select * from aaa.b; +------+ | name | +------+ | zzz | +------+ 1 row in set (0.00 sec)在host52主機(jī)上連接數(shù)據(jù)庫(kù)服務(wù)器,查看是否有剛剛寫入的數(shù)據(jù) mysql> select * from aaa.b; +------+ | name | +------+ | zzz | +------+ 1 row in set (0.00 sec)***** 環(huán)境要求:關(guān)閉防火墻,保證yum源可以正常使用,安裝提供服務(wù)的軟件 配置讀寫分離服務(wù)器192.168.4.57,具體操作如下:1.安裝軟件maxscale-2.1.2-1.rhel.7.x86_64.rpm [root@host57 ~]# rpm -ivh maxscale-2.1.2-1.rhel.7.x86_64.rpm #安裝maxscale 提供服務(wù)程序和管理命令 [root@host57 ~]# max [Tab][Tab] #查看maxscale的命令 maxadmin maxbinlogcheck maxpasswd maxavrocheck maxkeys maxscale 2.修改配置文件 [root@host57 ~]# ls /etc/maxscale.cnf /etc/maxscale.cnf[root@host57 ~]# cp /etc/maxscale.cnf /root[root@host57 ~]# vim /etc/maxscale.cnf9 [maxscale] #定義線程個(gè)數(shù)10 threads=auto #改為自動(dòng)模式...18 [server1] #定義數(shù)據(jù)庫(kù)服務(wù)器19 type=server20 address=192.168.4.51 #master主機(jī)IP地址21 port=330622 protocol=MySQLBackend23 24 [server2] #定義數(shù)據(jù)庫(kù)服務(wù)器225 type=server26 address=192.168.4.52 #slave主機(jī)IP地址27 port=330628 protocol=MySQLBackend35 [MySQL Monitor] #定義數(shù)據(jù)庫(kù)監(jiān)視的數(shù)據(jù)庫(kù)節(jié)點(diǎn)36 type=monitor37 module=mysqlmon38 servers=server1,server2 #監(jiān)控的數(shù)據(jù)庫(kù)列表,不能寫IP39 user=maxscalemon #監(jiān)控用戶40 passwd=123qqq...A #密碼41 monitor_interval=1000052 #[Read-Only Service] #第52行到58行注釋掉,取消定義只讀服務(wù)53 #type=service54 #router=readconnroute55 #servers=server156 #user=myuser57 #passwd=mypwd58 #router_options=slave63 [Read-Write Service] #定義讀寫分離數(shù)據(jù)庫(kù)的節(jié)點(diǎn)64 type=service65 router=readwritesplit66 servers=server1,server2 #主,從服務(wù)器主機(jī)名67 user=maxscalerouter #監(jiān)控路由用戶68 passwd=123qqq...A #密碼69 max_slave_connections=100%75 [MaxAdmin Service] #定義管理服務(wù)76 type=service77 router=cli85 #[Read-Only Listener] #取消定義只讀服務(wù)的端口,注釋掉86 #type=listener87 #service=Read-Only Service88 #protocol=MySQLClient89 #port=400891 [Read-Write Listener] #定義讀寫分離服務(wù)的端口號(hào)節(jié)點(diǎn)92 type=listener93 service=Read-Write Service94 protocol=MySQLClient95 port=400697 [MaxAdmin Listener] #定義管理服務(wù)的端口號(hào)節(jié)點(diǎn)98 type=listener99 service=MaxAdmin Service 100 protocol=maxscaled 101 socket=default 102 port=4016 #自己定義管理服務(wù)的端口號(hào),范圍0-65535,但建議不要設(shè)置在1024以內(nèi),1024以內(nèi)的端口都已經(jīng)有固定的服務(wù) [root@host57 ~]# sed -i '/#/d' /etc/maxscale.cnf #可以將配置文件里的注釋刪除3.添加授權(quán)用戶根據(jù)maxscale.cnf文件配置,在主、從服務(wù)器上添加對(duì)應(yīng)的授權(quán)用戶,因?yàn)閮膳_(tái)數(shù)據(jù)庫(kù)服務(wù)器是主從同步結(jié)構(gòu),只在主數(shù)據(jù)庫(kù)服務(wù)器添加用戶即可,從服務(wù)器會(huì)自動(dòng)同步mysql> grant replication slave ,replication client on *.* to maxscalemon@"%" identified by "123qqq...A"; #授權(quán)監(jiān)控用戶 Query OK, 0 rows affected, 1 warning (0.07 sec)mysql> grant select on mysql.* to maxscalerouter@"%" identified by "123qqq...A"; #授權(quán)路由用戶 Query OK, 0 rows affected, 1 warning (0.00 sec)4.查看授權(quán)用戶 mysql> select host , user from mysql.user where user in ("maxscalerouter","maxscalemon"); +------+----------------+ | host | user | +------+----------------+ | % | maxscalemon | | % | maxscalerouter | +------+----------------+ 2 rows in set (0.08 sec)在代理服務(wù)器57主機(jī)上,測(cè)試授權(quán)用戶 [root@host57 ~]#yum -y install mariadb //安裝提供mysql命令的軟件包 [root@host57 ~]# mysql -h 192.168.4.51 -umaxscalemon -p123qqq…A [root@host57 ~]# mysql -h 192.168.4.52 -umaxscalemon -p123qqq…A [root@host57 ~]# mysql -h 192.168.4.51 -umaxscalerouter -p123qqq…A [root@host57 ~]#mysql -h 192.168.4.52 -umaxscalerouter -p123qqq…A5.啟動(dòng)maxscale代理服務(wù) [root@host57 ~]# maxscale -f /etc/maxscale.cnf [root@host57 ~]# netstat -nutlp | grep maxscale #查看maxscale端口,4006為讀寫分離端口,4016為管理服務(wù)端口 tcp6 0 0 :::4006 :::* LISTEN 11249/maxscale tcp6 0 0 :::4016 :::* LISTEN 11249/maxscale [root@host57 ~]# ps -C maxscale #查看進(jìn)程PID TTY TIME CMD11249 ? 00:00:00 maxscale測(cè)試配置1.查看監(jiān)控信息(在主機(jī)57本機(jī)訪問(wèn)自己) [root@host57 ~]# maxadmin -uadmin -pmariadb -P4016 MaxScale> list servers #只能有一個(gè)空格!! Servers. -------------------+-----------------+-------+-------------+-------------------- Server | Address | Port | Connections | Status -------------------+-----------------+-------+-------------+-------------------- server1 | 192.168.4.51 | 3306 | 0 | Master, Running server2 | 192.168.4.52 | 3306 | 0 | Slave, Running -------------------+-----------------+-------+-------------+--------------------2.在主服務(wù)器上添加授權(quán)連接用戶 在主服務(wù)器上添加即可,從服務(wù)器會(huì)自動(dòng)同步 [root@host50 ~]# which mysql /usr/bin/mysql [root@host50 ~]# rpm -q mariadb mariadb-5.5.56-2.el7.x86_64[root@host50 ~]# mysql -h192.168.4.58 -P4006 -ujim -p123qqq...A ERROR 1045 (28000): Access denied for user 'jim'@'::ffff:192.168.4.50' (using password: YES) #客戶端訪問(wèn)服務(wù)器只能使用路由用戶訪問(wèn)在主服務(wù)器添加授權(quán)用戶給客戶端連接使用 客戶端使用授權(quán)用戶連接 mysql> create database db6; Query OK, 1 row affected (0.00 sec)mysql> create table db6.user(name char(15)); Query OK, 0 rows affected (0.06 sec)mysql> grant select ,insert on db6.* to jim@"%" identified by "123qqq...A"; Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> desc db6.user; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | name | char(15) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 1 row in set (0.00 sec)mysql> select user ,host from mysql.user; +----------------+--------------+ | user | host | +----------------+--------------+ | haha | % | | jim | % | | maxscalemon | % | | maxscalerouter | % | | repluser | % | | webadmin | % | | admin | 192.168.4.% | | root | 192.168.4.52 | | admin2 | localhost | | mysql.sys | localhost | | root | localhost | | tian | localhost | +----------------+--------------+ 12 rows in set (0.00 sec)在host52主機(jī)上面查看mysql> desc db6.user; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | name | char(15) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 1 row in set (0.00 sec)在host50客戶端訪問(wèn)代理服務(wù)器,插入數(shù)據(jù)可以成功查看 [root@host50 ~]# mysql -h192.168.4.58 -P4006 -ujim -p123qqq...A MySQL [(none)]> insert into db6.user values("bob"); Query OK, 1 row affected (0.01 sec)MySQL [(none)]> select * from db6.user; +------+ | name | +------+ | bob | +------+ 1 row in set (0.01 sec)在host51以及host52主機(jī)也可以看到插入的數(shù)據(jù) mysql> select * from db6.user; +------+ | name | +------+ | bob | +------+ 1 row in set (0.00 sec)

    MySQL多實(shí)例

    多實(shí)例概述

    • 什么是多實(shí)例?
    • 在一臺(tái)物理主機(jī)上運(yùn)行多個(gè)數(shù)據(jù)庫(kù)服務(wù)
    • 為什么要使用多實(shí)例?
    • 節(jié)約運(yùn)維成本
    • 提高硬件利用率

    配置多實(shí)例

    環(huán)境準(zhǔn)備:
    在192.168.4.57主機(jī)上配置,要提前安裝軟件包mysql-5.7.20可以支持配置多實(shí)例
    軟件包在此!提取碼:iptb

    [root@host57 ~]# ls mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz [root@host57 ~]# tar -zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz[root@host57 ~]# ls mysql-5.7.20-linux-glibc2.12-x86_64 bin COPYING docs include lib man README share support-files[root@host57 ~]# mv mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/mysql[root@host57 ~]# cd /usr/local/mysql[root@host57 mysql]# ls bin(SQL命令) COPYING docs(幫助文檔,二進(jìn)制) include(源碼調(diào)用程序文件) lib(源碼調(diào)用模塊) man(幫助文檔) README share(幫助文檔) support-files(配置文件模板)[root@host57 mysql]# PATH=/usr/local/mysql/bin:$PATH #將path變量賦新值[root@host57 mysql]# echo $PATH /usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin[root@host57 mysql]# mysql[Tab][Tab] mysql mysqld_multi mysql_secure_installation mysqladmin mysqld_safe mysqlshow mysqlbinlog mysqldump mysqlslap mysqlcheck mysqldumpslow mysql_ssl_rsa_setup mysql_client_test_embedded mysql_embedded mysqltest_embedded mysql_config mysqlimport mysql_tzinfo_to_sql mysql_config_editor mysql_install_db mysql_upgrade mysqld mysql_plugin mysqlxtest mysqld-debug mysqlpump [root@host57 mysql]# vim /etc/bashrc #在文檔結(jié)尾添加永久配置全局變量 export PATH=/usr/local/mysql/bin:$PATH[root@host57 ~]# yum -y install libaio #安裝依賴包[root@host57 ~]# useradd mysql #添加進(jìn)程所有者用戶,一定不要忘記創(chuàng)建!!!!創(chuàng)建并編寫主配置文件 /etc/my.cnf多實(shí)例服務(wù)的運(yùn)行配置 如果原本有下載mariadb或者安裝過(guò)mysql軟件就會(huì)產(chǎn)生/etc/my.cnf,要先刪除 [root@host57 ~]# rm -rf /etc/my.cnfsocket文件:當(dāng)自己訪問(wèn)自己時(shí),通過(guò)socket文件傳遞數(shù)據(jù),是訪問(wèn)mysql接口程序的文件,套接字文件,虛擬文件,服務(wù)啟動(dòng)時(shí)有該文件,服務(wù)關(guān)閉時(shí)文件即消失[root@host57 ~]# vim /etc/my.cnf [mysqld_multi] #啟用多實(shí)例 mysqld=/usr/local/mysql/bin/mysqld_safe #指定進(jìn)程文件路徑 mysqladmin=/usr/local/mysql/bin/mysqladminer=root #指定管理命令路徑 user=root #指定進(jìn)程用戶[mysqld1] #實(shí)例進(jìn)程名稱 datadir=/dir1 #數(shù)據(jù)庫(kù)目錄,要手動(dòng)創(chuàng)建 log-error=/dir1/mysqld1.error #錯(cuò)誤日志位置 socket=/dir1/mysqld1.sock #指定sock文件的路徑和名稱 pid-file=/dir1/mysqld1.pid #進(jìn)程pid號(hào)文件位置 port=3307 #端口號(hào)(自定義,不能是已經(jīng)存在的默認(rèn)的端口)[mysqld2] datadir=/dir2 log-error=/dir2/mysqld2.error socket=/dir2/mysqld2.sock pid-file=/dir2/mysqld2.pid port=3308[root@host57 ~]# mkdir /dir1 #創(chuàng)建數(shù)據(jù)庫(kù)目錄 [root@host57 ~]# mkdir /dir2 啟動(dòng)服務(wù)(首次啟動(dòng)服務(wù)會(huì)創(chuàng)建root登錄的初始密碼) [root@host57 ~]# ls /dir1[root@host57 ~]# mysqld_multi start 1 #開(kāi)啟第一個(gè)服務(wù) [root@host57 ~]# ss -nutlp | grep 3307 tcp LISTEN 0 80 :::3307 :::* users:(("mysqld",pid=11365,fd=20))[root@host57 ~]# ps -C mysqldPID TTY TIME CMD11365 pts/0 00:00:00 mysqld[root@host57 ~]# ls /dir1 #查看數(shù)據(jù)庫(kù)目錄文件列表 auto.cnf ib_logfile0 mysql mysqld1.sock sys ib_buffer_pool ib_logfile1 mysqld1.error mysqld1.sock.lock ibdata1 ibtmp1 mysqld1.pid performance_schema[root@host57 ~]# mysql -uroot -p',hi=N2b,8/qI' -S /dir1/mysqld1.sock mysql> alter user root@"localhost" identified by "123456"; #修改新密碼[root@host57 ~]# mysql -uroot -p123456 -S /dir1/mysqld1.sock mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)[root@host57 ~]# ls /dir2[root@host57 ~]# mysqld_multi start 2 #啟動(dòng)實(shí)例2[root@host57 ~]# ls /dir2 #查看數(shù)據(jù)庫(kù)目錄文件列表 auto.cnf ib_logfile0 mysql mysqld2.sock.lock ib_buffer_pool ib_logfile1 mysqld2.error performance_schema ibdata1 ibtmp1 mysqld2.sock sys[root@host57 ~]# ss -nutlp | grep 3308 tcp LISTEN 0 80 :::3308 :::* users:(("mysqld",pid=11734,fd=20))[root@host57 ~]# ps -C mysqldPID TTY TIME CMD11365 pts/0 00:00:01 mysqld11734 pts/0 00:00:00 mysqld[root@host57 ~]# mysql -uroot -p"dsvL3O<Nzo4%" -S /dir2/mysqld2.sock mysql> alter user root@"localhost" identified by "123456"; #修改密碼 Query OK, 0 rows affected (0.03 sec)[root@host57 ~]# mysql -uroot -p123456 -S /dir2/mysqld2.sock #新密碼登錄 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)############## 如果啟動(dòng)服務(wù)而且有端口號(hào)但無(wú)法進(jìn)入數(shù)據(jù)庫(kù),可以進(jìn)行如下操作: rm -rf /dir1/* killall -9 mysqld #如果沒(méi)有killall命令,請(qǐng)yum -y install psmisc 檢查配置文件/etc/my.cnf 再次重新啟動(dòng)服務(wù)mysqld_multi start 1 ################停止服務(wù)(如果停止不了服務(wù)就殺掉進(jìn)程) [root@host57 ~]# ss -nutlp | grep mysqld tcp LISTEN 0 80 :::3307 :::* users:((mysqld",pid=11365,fd=20)) tcp LISTEN 0 80 :::3308 :::* users:((mysqld",pid=11734,fd=20))[root@host57 ~]# mysqld_multi --user=root --password=123456 stop 1 #關(guān)閉實(shí)例1的服務(wù)[root@host57 ~]# ss -nutlp | grep mysqld tcp LISTEN 0 80 :::3308 :::* users:((mysqld",pid=11734,fd=20))[root@host57 ~]# mysqld_multi start 1 #再次啟動(dòng)[root@host57 ~]# mysql -uroot -p123456 -S /dir1/mysqld1.sock #此時(shí)再次啟動(dòng)時(shí)直接用修改后的密碼登錄即可 [root@host57 ~]# ss -nutlp | grep mysqld tcp LISTEN 0 80 :::3307 :::* users:((mysqld",pid=11945,fd=28)) tcp LISTEN 0 80 :::3308 :::* users:((mysqld",pid=11734,fd=20))添加實(shí)例3.自定義運(yùn)行參數(shù)[root@host57 ~]# vim /etc/my.cnf [mysqld_multi] mysqld=/usr/local/mysql/bin/mysqld_safe mysqladmin=/usr/local/mysql/bin/mysqladmin user=root[mysqld1] datadir=/dir1 log-error=/dir1/mysqld1.error socket=/dir1/mysqld1.sock pid-file=/dir1/mysqld1.pid port=3307[mysqld2] datadir=/dir2 log-error=/dir2/mysqld2.error socket=/dir2/mysqld2.sock pid-file=/dir2/mysqld2.pid port=3308[mysqld3] datadir=/dir3 log-error=/dir3/mysqld3.error socket=/dir3/mysqld3.sock pid-file=/dir3/mysqld3.pid port=3309[root@host57 ~]#mkdir /dir3 [root@host57 ~]# ls /dir3 [root@host57 ~]# mysqld_multi start 3 [root@host57 ~]# ls /dir3 auto.cnf ib_logfile0 mysql mysqld3.sock sys ib_buffer_pool ib_logfile1 mysqld3.error mysqld3.sock.lock ibdata1 ibtmp1 mysqld3.pid performance_schema [root@host57 ~]# ss -nutlp | grep mysqld tcp LISTEN 0 80 :::3307 :::* users:((mysqld",pid=11945,fd=28)) tcp LISTEN 0 80 :::3308 :::* users:((mysqld",pid=11734,fd=20)) tcp LISTEN 0 80 :::3309 :::* users:((mysqld",pid=12140,fd=20))[root@host57 ~]# mysql -uroot -p'Iorf=e<ef6aM' -S /dir3/mysqld3.sock mysql> alter user root@"localhost" identified by "123456"; [root@host57 ~]# mysql -uroot -p123456 -S /dir3/mysqld3.sock允許客戶端50可以連接57主機(jī)的實(shí)例1服務(wù) ,對(duì)db1庫(kù)有完全權(quán)限,且實(shí)例1可以記錄用戶執(zhí)行的SQL命令 [root@host57 ~]# vim /etc/my.cnf[mysqld1] server_id=1 log_bin=mysqld1 datadir=/dir1 log-error=/dir1/mysqld1.error socket=/dir1/mysqld1.sock pid-file=/dir1/mysqld1.pid port=3307[root@host57 ~]# mysqld_multi --user=root --password=123456 stop 1 [root@host57 ~]# mysqld_multi start 1 [root@host57 ~]# mysql -uroot -p123456 -S /dir1/mysqld1.sock mysql> show master status; +----------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +----------------+----------+--------------+------------------+-------------------+ | mysqld1.000001 | 154 | | | | +----------------+----------+--------------+------------------+-------------------+ 1 row in set (0.04 sec)mysql> grant all on db1.* to yaya@"192.168.4.50" identified by "123456"; Query OK, 0 rows affected, 1 warning (0.07 sec)mysql> select user ,host from mysql.user; +---------------+--------------+ | user | host | +---------------+--------------+ | yaya | 192.168.4.50 | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +---------------+--------------+ 4 rows in set (0.00 sec)mysql> show grants for yaya@"192.168.4.50" ; +----------------------------------------------------------+ | Grants for yaya@192.168.4.50 | +----------------------------------------------------------+ | GRANT USAGE ON *.* TO 'yaya'@'192.168.4.50' | | GRANT ALL PRIVILEGES ON `db1`.* TO 'yaya'@'192.168.4.50' | +----------------------------------------------------------+ 2 rows in set (0.00 sec)[root@host50 ~]# mysql -h192.168.4.57 -uyaya -p123456 -P3307 MySQL [(none)]> show grants; +----------------------------------------------------------+ | Grants for yaya@192.168.4.50 | +----------------------------------------------------------+ | GRANT USAGE ON *.* TO 'yaya'@'192.168.4.50' | | GRANT ALL PRIVILEGES ON `db1`.* TO 'yaya'@'192.168.4.50' | +----------------------------------------------------------+ 2 rows in set (0.00 sec)MySQL [(none)]> create database db1; Query OK, 1 row affected (0.02 sec)MySQL [(none)]> create table db1.user(name char(10)); Query OK, 0 rows affected (0.02 sec)MySQL [(none)]> insert into db1.user values("aaa"); Query OK, 1 row affected (0.04 sec)MySQL [(none)]> select * from db1.user; +------+ | name | +------+ | aaa | +------+ 1 row in set (0.00 sec)在host57主機(jī)查看 [root@host57 ~]# mysql -uroot -p123456 -S /dir1/mysqld1.sock mysql> select * from db1.user; +------+ | name | +------+ | aaa | +------+ 1 row in set (0.00 sec)

    總結(jié)

    以上是生活随笔為你收集整理的DBA(四):数据读写分离,MySQL多实例操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。