開發人員學Linux(6):CentOS7編譯安裝MySQL5.7.18多實例及主從複製
1.前言
上一篇講述了如何在CentOS7下編譯安裝Nginx-1.12.0並如何配置反向代理,本篇將講述如何編譯安裝MySQL5.7.18並配置多實例和主從複製。
2.準備
2.1下載MySQL5.7.18源碼
注意最新版本的MySQL需要Boost才能編譯安裝,在MySQL提供的下載中有不帶boost的源碼,還有帶boost的源碼,如果下載不帶boost的源碼還需要再去下載boost源碼,為省事起見,建議下載帶boost的源碼,下載地址:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.18.tar.gz (可直接使用wget下載)
註:可以打開網頁來下載,網頁地址為:https://dev.mysql.com/downloads/mysql/,當前最新版本為5.7.18,在「Select Operating System:」處選擇「Source Code」,然後在圖中選擇「Generic Linux (Architecture Independent), Compressed TAR Archive Includes Boost Headers 5.7.18 58.8M (mysql-boost-5.7.18.tar.gz)」,如下圖所示:
2.2安裝epel
EPEL的全稱叫 Extra Packages for Enterprise linux 。EPEL是由 Fedora 社區打造,為 RHEL 及衍生髮行版如 CentOS、Scientific Linux 等提供高質量軟體包的項目。裝上了 EPEL之後,就相當於添加了一個第三方源。
在編譯MySQL過程中需要一些依賴插件,安裝epel後可直接通過yum install 來安裝。
執行命令:
yum install epel-release –y
2.3安裝cmake
不同於nginx使用make來編譯和安裝,MySQL使用CMake來編譯和安裝,安裝命令:
yum install cmake –y
為將來方便還可以把相關的也一併安裝了:
yum install autoconf automake libtool -y
2.4安裝可能依賴庫
yum install krb5 krb5-devel libidn libidn-devel openssl openssl-devel -ynyum install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devdel -ynyum install zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel -ynyum install ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel -ynyum install perl-Data_dumper python-devel -yn
3.編譯及安裝
3.1解壓文件
假定mysql-boost-5.7.18.tar.gz文件位於/root/目錄下(不管是通過wget直接下載的還是通過下載工具上傳的),首先解壓文件:
tar zxvf /root/mysql-boost-5.7.18.tar.gz
cd /root/mysql-5.7.18
3.2配置、編譯及安裝
首先進行參數配置,命令如下:
cmake n-DWITH_BOOST=boost n-DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.18 n-DMYSQL_DATADIR=/usr/local/mysql-5.7.18/data n-DWITH_MYISAM_STORAGE_ENGINE=1 n-DWITH_INNOBASE_STORAGE_ENGINE=1 n-DWITH_FEDERATED_STORAGE_ENGINE=1 n-DWITH_BLACKHOLE_STORAGE_ENGINE=1 n-DWITH_EXAMPLE_STORAGE_ENGINE=1 n-DWITH_PARTITION_STORAGE_ENGINE=1 n-DWITH_READLINE=1 n-DENABLED_LOCAL_INFILE=1 n-DDEFAULT_CHARSET=utf8 n-DDEFAULT_COLLATION=utf8_general_ci n-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii n-DWITH_ZLIB=bundled n-DWITH_READLINE=1 n-DWITH_EMBEDDED_SERVER=1 n-DWITH_DEBUG=0 n-DWITH_SSL=systemn
然後編譯和安裝:
make && make install
這裡要說明幾點:
(1)當命令或參數過長時,為了便於直觀顯示,可以在其後加上」」,當然」」與正常命令之間需要有空格;
(2) -DWITH_BOOST=boost 指定boost類庫的位置;
(3) -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.18 指定安裝位置;
在上述配置過程中可能會有出錯信息,根據出錯信息調整響應參數或者安裝缺失的類庫,直到提示可以安裝為止。
然後執行make進行編譯,編譯過程中沒有錯誤的再執行make install安裝,由於源代碼體積有點大,編譯耗費的時間比較長,在本人2G內存、4核的虛擬機上編譯時間超過半小時以上。
安裝成功之後,在/usr/local/mysql-5.7.18下就是安裝後的文件目錄。
註:以上編譯是針對多實例的,如果是單實例,則可採用以下配置:
cmake n-DWITH_BOOST=boost n-DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.18 n-DMYSQL_DATADIR=/usr/local/mysql-5.7.18/data n-DMYSQL_TCP_PORT=3306 n-DWITH_MYISAM_STORAGE_ENGINE=1 n-DWITH_INNOBASE_STORAGE_ENGINE=1 n-DWITH_FEDERATED_STORAGE_ENGINE=1 n-DWITH_BLACKHOLE_STORAGE_ENGINE=1 n-DWITH_EXAMPLE_STORAGE_ENGINE=1 n-DWITH_PARTITION_STORAGE_ENGINE=1 n-DWITH_READLINE=1 n-DENABLED_LOCAL_INFILE=1 n-DDEFAULT_CHARSET=utf8 n-DDEFAULT_COLLATION=utf8_general_ci n-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii n-DWITH_ZLIB=bundled n-DWITH_READLINE=1 n-DWITH_EMBEDDED_SERVER=1 n-DWITH_DEBUG=0 n-DWITH_SSL=systemn
4.資料庫初始化
4.1創建多資料庫實例配置文件my.cnf文件
my.cnf文件是MySQL的配置文件,在MySQL啟動過程中會讀取裡面的配置信息。
在本篇中不滿足於單實例的配置,本篇將利用3306和3307兩個埠創建兩個實例,因此需要在/etc/目錄下創建my.cnf文件。
首先創建文件夾:
mkdir –p /usr/local/mysql-5.7.18/data/3306/data
mkdir –p /usr/local/mysql-5.7.18/data/3307/data
/usr/local/mysql-5.7.18/data/3306文件夾用於存放監聽3306埠實例的相關配置信息、執行腳本和數據,/usr/local/mysql-5.7.18/data/3307文件夾用於存放監聽3307埠實例的相關配置信息、執行腳本和數據。
因為mysql資料庫將以nginx這個用戶來運行,因此首先需要創建這個賬戶:
useradd mysql -s /sbin/nologin –m
/etc/my.cnf的內容:
[mysqld_multi] nmysqld = /usr/local/mysql-5.7.18/bin/mysqld_safe nmysqladmin = /usr/local/mysql-5.7.18/bin/mysqladmin nuser = mysql npassword = !QAZ2wsx3edcnn[mysqld3306]nserver-id = 1nlog-bin= /usr/local/mysql-5.7.18/data/3306/mysql-binnuser = mysqlnpid-file = /usr/local/mysql-5.7.18/data/3306/mysql.pidnsocket = /usr/local/mysql-5.7.18/data/3306/mysql.socknlog-error = /usr/local/mysql-5.7.18/data/3306/mysqld.lognport = 3306nbasedir = /usr/local/mysql-5.7.18ndatadir = /usr/local/mysql-5.7.18/data/3306/datantmpdir = /tmpnopen_files_limit=1024nexternal-locking = falsencharacter-set-server=utf8n#default-storage-engine=MyISAMndefault-storage-engine=INNODBnexplicit_defaults_for_timestamp=truennbind-address = 0.0.0.0nmax_allowed_packet = 8Mnthread_stack = 192Knthread_cache_size = 8nnmax_connections = 800nmax_connect_errors = 300nnquery_cache_limit = 1Mnquery_cache_size = 2Mnjoin_buffer_size=1Mnsort_buffer_size=1Mnnlong_query_time = 1nrelay-log = /usr/local/mysql-5.7.18/data/3306/relay-binnrelay-log-info-file =/usr/local/mysql-5.7.18/data/3306/relay-log.infonbinlog_cache_size = 1Mnmax_binlog_cache_size = 1Mnmax_binlog_size = 2Mnkey_buffer_size=16Mnread_buffer_size = 1Mnread_rnd_buffer_size = 1Mnbulk_insert_buffer_size = 1Mnlower_case_table_names = 1nskip-name-resolvenslave-skip-errors =1032,1062nreplicate-ignore-db = mysqlninnodb_buffer_pool_size = 32Mninnodb_data_file_path = ibdata1:12M:autoextendninnodb_thread_concurrency = 8ninnodb_flush_log_at_trx_commit = 2ninnodb_log_buffer_size = 2Mninnodb_log_file_size = 4Mninnodb_max_dirty_pages_pct = 90ninnodb_lock_wait_timeout = 120ninnodb_file_per_table = 0nnn[mysqld3307]nserver-id = 2nuser = mysqlnpid-file = /usr/local/mysql-5.7.18/data/3307/mysql.pidnsocket = /usr/local/mysql-5.7.18/data/3307/mysql.socknlog-error = /usr/local/mysql-5.7.18/data/3307/mysqld.lognport = 3307nbasedir = /usr/local/mysql-5.7.18ndatadir = /usr/local/mysql-5.7.18/data/3307/datantmpdir = /tmpnopen_files_limit=1024nexternal-locking = falsencharacter-set-server=utf8n#default-storage-engine=MyISAMndefault-storage-engine=INNODBnexplicit_defaults_for_timestamp=truennbind-address = 0.0.0.0nmax_allowed_packet = 8Mnthread_stack = 192Knthread_cache_size = 8nnmax_connections = 800nmax_connect_errors = 300nnquery_cache_limit = 1Mnquery_cache_size = 2Mnjoin_buffer_size=1Mnsort_buffer_size=1Mnnlong_query_time = 1nrelay-log = /usr/local/mysql-5.7.18/data/3307/relay-binnrelay-log-info-file =/usr/local/mysql-5.7.18/data/3307/relay-log.infonbinlog_cache_size = 1Mnmax_binlog_cache_size = 1Mnmax_binlog_size = 2Mnkey_buffer_size=16Mnread_buffer_size = 1Mnread_rnd_buffer_size = 1Mnbulk_insert_buffer_size = 1Mnlower_case_table_names = 1nskip-name-resolvenslave-skip-errors =1032,1062nreplicate-ignore-db = mysqlninnodb_buffer_pool_size = 32Mninnodb_data_file_path = ibdata1:12M:autoextendninnodb_thread_concurrency = 8ninnodb_flush_log_at_trx_commit = 2ninnodb_log_buffer_size = 2Mninnodb_log_file_size = 4Mninnodb_max_dirty_pages_pct = 90ninnodb_lock_wait_timeout = 120ninnodb_file_per_table = 0n
註:在上述文件中配置了兩個MySQL實例,並且在3306及3307配置中都提到了mysqld.log這個文件,這個文件是用來記錄MySQL啟動過程的信息的,不過悲催的是如果這個文件不存在,那麼是會啟動出錯的,可又沒有地方可以查看錯誤,因此需要提前創建好文件並設置許可權。
touch /usr/local/mysql-5.7.18/data/3306/mysqld.log
touch /usr/local/mysql-5.7.18/data/3307/mysqld.log
chmod 766 /usr/local/mysql-5.7.18/data/3306/mysqld.log
chmod 766 /usr/local/mysql-5.7.18/data/3307/mysqld.log
註:如果嫌每次輸入完整的文件路徑麻煩可以切換到程序的安裝目錄,甚至還可以創建軟鏈接,所謂的軟鏈接有點類似於Windows下的快捷方式,如ln –s /usr/local/mysql-5.7.18 /usr/local/mysql就是創建了mysql-5.7.18的軟鏈接,甚至如果你喜歡,還可以在用戶根目錄下創建軟鏈接,如:ln –s /usr/local/mysql-5.7.18 /usr/local/mysql。
通過命令查看,如下圖所示是在SSH客戶端中的效果:
上面mysql,nginx及tomcat都是創建的軟鏈接(文件屬性的第一個字元是」l」表明是軟鏈接),其對應的真實路徑分別為當前路徑下的mysql-5.7.18、nginx-1.12.0及apache-tomcat-8.5.15目錄。
註:針對單實例的my.cnf文件:
[client]nport = 3306nsocket = /usr/local/mysql-5.7.18/mysql.socknn[mysqld_safe]nlog-error = /usr/local/mysql-5.7.18/mysql.errnpid-file = /usr/local/mysql-5.7.18/mysql.pidnn[mysqld]n#n# * Basic Settingsn#nserver-id = 1nlog-bin= /usr/local/mysql-5.7.18/mysql-binnuser = mysqlnpid-file = /usr/local/mysql-5.7.18/mysql.pidnsocket = /usr/local/mysql-5.7.18/mysql.socknport = 3306nbasedir = /usr/local/mysql-5.7.18ndatadir = /usr/local/mysql-5.7.18/datantmpdir = /tmpnopen_files_limit=1024nexternal-locking = falsencharacter-set-server=utf8n#default-storage-engine=MyISAMndefault-storage-engine=INNODBnnbind-address = 0.0.0.0nmax_allowed_packet = 8Mnthread_stack = 192Knthread_cache_size = 8nnmax_connections = 800nmax_connect_errors = 300n#table_cache = 64n#thread_concurrency = 10nnquery_cache_limit = 1Mnquery_cache_size = 2Mnjoin_buffer_size=1Mnsort_buffer_size=1Mnnlong_query_time = 1nrelay-log = /usr/local/mysql-5.7.18/relay-binnrelay-log-info-file =/usr/local/mysql-5.7.18/relay-log.infonbinlog_cache_size = 1Mnmax_binlog_cache_size = 1Mnmax_binlog_size = 2Mnkey_buffer_size=16Mnread_buffer_size = 1Mnread_rnd_buffer_size = 1Mnbulk_insert_buffer_size = 1Mnlower_case_table_names = 1nskip-name-resolvenslave-skip-errors =1032,1062nreplicate-ignore-db = mysqln#innodb_additional_mem_pool_size = 4Mninnodb_buffer_pool_size = 32Mninnodb_data_file_path = ibdata1:12M:autoextendn#innodb_file_io_threads = 4ninnodb_thread_concurrency = 8ninnodb_flush_log_at_trx_commit = 2ninnodb_log_buffer_size = 2Mninnodb_log_file_size = 4Mn#innodb_log_files_in_groups = 3ninnodb_max_dirty_pages_pct = 90ninnodb_lock_wait_timeout = 120ninnodb_file_per_table = 0n[mysqldump]nquicknquote-namesnmax_allowed_packet = 16Mn
4.2創建MySQL的啟動文件
在這裡使用MySQL提供的用於多實例啟動的文件,這個文件位於/usr/local/mysql-5.7.18/support-files目錄下,文件名為mysqld_multi.server,首先將其複製到/etc/init.d/目錄,命令如下:
cp /usr/local/src/mysql-5.1.60/support-files/mysqld_multi.server /etc/init.d/mysqld_multi.server
然後修改/etc/init.d/mysqld_multi.server文件如下:
#!/bin/shn#n# A simple startup script for mysqld_multi by Tim Smith and Jani Tolonen.n# This script assumes that my.cnf file exists either in /etc/my.cnf orn# /root/.my.cnf and has groups [mysqld_multi] and [mysqldN]. See then# mysqld_multi documentation for detailed instructions.n#n# This script can be used as /etc/init.d/mysql.servern#n# Comments to support chkconfig on RedHat Linuxn# chkconfig: 2345 64 36n# description: A very fast and reliable SQL database engine.n#n# Version 1.0n#nnbasedir=/usr/local/mysql-5.7.18nbindir=/usr/local/mysql-5.7.18/binnnif test -x $bindir/mysqld_multinthenn mysqld_multi="$bindir/mysqld_multi";nelsen echo "Cant execute $bindir/mysqld_multi from dir $basedir";n exit;nfinncase "$1" inn start )n "$mysqld_multi" start $2n ;;n stop )n "$mysqld_multi" stop $2n ;;n report )n "$mysqld_multi" report $2n ;;n restart )n "$mysqld_multi" stop $2n "$mysqld_multi" start $2n ;;n *)n echo "Usage: $0 {start|stop|report|restart}" >&2n ;;nesacn
注意:
上述啟動文件中關閉實例沒有採用kill進程的辦法,而是使用mysqladmin shutdown的方法,這個方法需要root級別用戶的賬號和密碼,因此需要控制這個文件的查看和編輯許可權。
需要給兩個文件設置執行許可權。
chmod 755 /usr/local/mysql-5.7.18/data/3306/mysql
chmod 755 /usr/local/mysql-5.7.18/data/3307/mysql
此外,因為MySQL的兩個實例均以MySQL用戶運行,為保證有足夠的許可權,設置/usr/local/mysql-5.7.18/文件夾的用戶和用戶組屬性:
chown –R mysql:mysql /usr/local/mysql-5.7.18
註:單實例版的MySQL啟動文件(文件全路徑為/lib/systemd/system/mysql.service):
[Unit]nDescription=MySQL Server 5.7.18nAfter=network.targetnn[Service]nExecStart=/usr/local/mysql-5.7.18/bin/mysqld_safe --defaults-file=/usr/local/mysql-5.7.18/my.cnf --datadir=/usr/local/mysql-5.7.18 --socket=/usr/local/mysql-5.7.18/mysqld.sock User=mysql Group=mysqlnWorkingDirectory=/usr/local/mysql-5.7.18nn[Install]nWantedBy=multi-user.targetn
4.3執行初始化
網上有很多例子講解MySQL初始化都是利用MySQL安裝目錄下的bin目錄中的mysql_install_db來執行,但是這個官方不建議的,官方建議使用mysqld來初始化。
下面的腳本可以執行但不建議使用(且本人發現執行後不知道root用戶默認密碼):
/usr/local/mysql-5.7.18/bin/mysql_install_db n--basedir=/usr/local/mysql-5.7.18 n--datadir=/usr/local/mysql-5.7.18/data/3306/data n--user=mysqlnn/usr/local/mysql-5.7.18/bin/mysql_install_db n--basedir=/usr/local/mysql-5.7.18 n--datadir=/usr/local/mysql-5.7.18/data/3307/data n--user=mysqln
下面的腳本是官方建議的:
/usr/local/mysql-5.7.18/bin/mysqld --initialize n--basedir=/usr/local/mysql-5.7.18 n--datadir=/usr/local/mysql-5.7.18/data/3306/data n--user=mysqlnn/usr/local/mysql-5.7.18/bin/mysqld --initialize n--basedir=/usr/local/mysql-5.7.18 n--datadir=/usr/local/mysql-5.7.18/data/3307/data n--user=mysqln
註:單實例版的初始化命令:
/usr/local/mysql-5.7.18/bin/mysqld --initialize n--basedir=/usr/local/mysql-5.7.18 n--datadir=/usr/local/mysql-5.7.18/data n--user=mysqln
上面的腳本如果執行成功,最後一句里有root用戶的初始密碼,在本人執行上述兩條命令時得到結果如下:
其中關鍵的兩句(每個實例初始化都有類似一句):
2017-06-09T13:45:09.590690Z 1 [Note] A temporary password is generated for root@localhost: Gdl<nv>R7&+wn2017-06-09T13:46:48.721250Z 1 [Note] A temporary password is generated for root@localhost: Z>B#5IfIFAfjn
上述密碼是隨機的,請記住一會登錄系統後更改臨時密碼。
4.4啟動MySQL實例和登錄
4.4.1啟動實例
可以通過mysqld_multi來啟動實例,這個文件位於/usr/local/mysql-5.7.18/bin目錄下,可以將這個路徑添加到/etc/profile和/etc/bashrc文件中的PATH變數中。
在前面的my.cnf文件中我們添加了兩個實例,名稱分別為mysqld3306和mysqld3307,並且針對多實例的配置文件my.cnf在/etc目錄,因此啟動3306對應實例的命令如下:
/usr/local/mysql-5.7.18/bin/mysqld_multi --defaults-extra-file=/etc/my.cnf start 3306
如果要同時啟動3306和3307兩個監聽實例,則執行如下命令:
/usr/local/mysql-5.7.18/bin/mysqld_multi --defaults-extra-file=/etc/my.cnf start 3306,3307
可以通過如下命令查看各實例的啟動狀態:
/usr/local/mysql-5.7.18/bin/mysqld_multi --defaults-extra-file=/etc/my.cnf report
以下是在本機上執行上面的命令返回的結果:
[root@it ~]# /usr/local/mysql-5.7.18/bin/mysqld_multi --defaults-extra-file=/etc/my.cnf reportnReporting MySQL serversnMySQL server from group: mysqld3306 is runningnMySQL server from group: mysqld3307 is runningn
這時也可以通過netstat查看實例是否啟動成功,如果啟動成功相應的埠就會處於監聽狀態。
[root@localhost ~]# netstat -lntp | grep 330
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 4480/mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3817/mysqld
從執行結果來看,3306和3307兩個埠對應的MySQL實例均已啟動。
當然,你也可以通過上述命令來重啟或者停止MySQL實例,如停止:
/usr/local/mysql-5.7.18/bin/mysqld_multi --defaults-extra-file=/etc/my.cnf stop 3306,3307
如果啟動不成功,可以通過相應的錯誤日誌來排除錯誤,如下就是用tail來查看錯誤日誌:
[root@localhost ~]# tail /usr/local/mysql-5.7.18/data/3306/mysqld.logn2017-06-10T14:41:51.366238Z 0 [Warning] db entry sys mysql.sys@localhost ignored in --skip-name-resolve mode.n2017-06-10T14:41:51.366625Z 0 [Warning] proxies_priv entry @ root@localhost ignored in --skip-name-resolve mode.n2017-06-10T14:41:51.371787Z 0 [Warning] tables_priv entry sys_config mysql.sys@localhost ignored in --skip-name-resolve mode.n2017-06-10T14:41:51.416687Z 0 [Note] Event Scheduler: Loaded 0 eventsn2017-06-10T14:41:51.416855Z 0 [Note] /usr/local/mysql-5.7.18/bin/mysqld: ready for connections.nVersion: 5.7.18-log socket: /usr/local/mysql-5.7.18/data/3306/mysql.sock port: 3306 Source distributionn2017-06-10T14:41:51.416862Z 0 [Note] Executing SELECT * FROM INFORMATION_SCHEMA.TABLES; to get a list of tables using the deprecated partition engine. You may use the startup option --disable-partition-engine-check to skip this check.n2017-06-10T14:41:51.416864Z 0 [Note] Beginning of list of non-natively partitioned tablesn2017-06-10T14:41:51.473660Z 0 [Note] End of list of non-natively partitioned tablesn2017-06-10T14:42:07.539386Z 3 [Note] Start binlog_dump to master_thread_id(3) slave_server(2), pos(mysql-bin.000001, 2654)n
4.4.2登錄實例
如果是單實例可以通過mysql -u user -p來登錄,但這裡有了兩個實例,所以登錄方式有些區別。
登錄到3306埠對應的實例:
/usr/local/mysql-5.7.18/bin/mysql -u root -p -S /usr/local/mysql-5.7.18/data/3306/mysql.sock
登錄到3307埠對應的實例:
/usr/local/mysql-5.7.18/bin/mysql -u root -p -S /usr/local/mysql-5.7.18/data/3307/mysql.sock
以下是登錄3306實例的過程:
[root@localhost ~]# cd /usr/local/mysql-5.7.18n[root@localhost mysql-5.7.18]# ./bin/mysql -u root -p -S ./data/3306/mysql.socknEnter password:nWelcome to the MySQL monitor. Commands end with ; or g.nYour MySQL connection id is 4nServer version: 5.7.18n
在此過程中需要輸入root的密碼,root的密碼在初始化階段時系統隨機生成了,可見「執行初始化」一節中描述:
2017-06-09T13:45:09.590690Z 1 [Note] A temporary password is generated for root@localhost: Gdl<nv>R7&+w
2017-06-09T13:46:48.721250Z 1 [Note] A temporary password is generated for root@localhost: Z>B#5IfIFAfj
其中Gdl<nv>R7&+w和Z>B#5IfIFAfj分別是3306和3307實例對應的隨機密碼,初次登錄過程中需要輸入。
登錄成功後,如果再執行其它語句時會得到如下提示:
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
因為我們沒有改系統為root賬戶生成的隨機密碼,在此之前是不能進行任何操作的,可以通過如下命令更改:
mysql> SET PASSWORD = PASSWORD(your_password);
Query OK, 0 rows affected (0.03 sec)
在實際執行過程中請將your_password更改為自己的密碼。改完之後記得改3306和3307兩個啟動文件中的用戶密碼哦,否則stop命令不會生效的。
至此,MySQL資料庫已完成初始化和初步配置。在實際過程中由於root用戶的許可權過大、且默認情況下只能在伺服器上登錄,為了安全一般會創建其它較小許可權的用戶,在這裡為了演示方便省卻了這個操作。
5.設置MySQL主從同步
主從同步,也稱master-slave,是開發過程中常見的提高程序性能的辦法。通常情況下在主資料庫伺服器寫入數據,查詢數據的時候是在從伺服器,主從伺服器之間通過同步機制來保持一致,雖然主從資料庫的一致性是存在一定時間差的,但因為這個時間差很小所以在一些對時間不是非常敏感的業務場景下被認為是實時一致的。在大多數主流資料庫中都是可以通過配置來實現主從同步、讀寫分離的。
這裡就以剛剛配置起來的兩個數據實例來演示如何實現主從同步的,步驟有如下:
(1)主資料庫實例設置server-id和開啟bin-log;
(2)主資料庫實例創建用於同步的賬號;
(3)從資料庫實例設置server-id;
(4)從資料庫實例配置同步參數;
(5)從資料庫實例啟動同步開關。
5.1主資料庫實例設置server-id和開啟bin-log
其實在上面的配置文件里已經配置了server-id和bin-log,就在/etc/my.cnf文件的[mysqld3306]節點處:
[mysqld3306]
server-id = 1
log-bin= /usr/local/mysql-5.7.18/data/3306/mysql-bin
另外還需要注意的是在這個my.cnf配置文件中還有一句:
replicate-ignore-db = mysql
它的意思是在進行主從同步時忽略mysql這個庫,因為mysql庫主要存放賬號及授權信息的,不同資料庫實例賬號和授權信息不同的可能性極大,因此沒有必要同步這個庫。
通過如下命令可以快速查看是否已經正確配置:
[root@localhost ~]# egrep "server-id|log-bin" /usr/local/mysql-5.7.18/data/3306/my.cnfnserver-id = 1nlog-bin= /usr/local/mysql-5.7.18/data/3306/mysql-binnserver-id = 2n
在資料庫中也可以查看:
mysql> show variables like "server_id";n+---------------+-------+n| Variable_name | Value |n+---------------+-------+n| server_id | 1 |n+---------------+-------+n1 row in set (0.00 sec)nnmysql> show variables like "log_bin";n+---------------+-------+n| Variable_name | Value |n+---------------+-------+n| log_bin | ON |n+---------------+-------+n1 row in set (0.00 sec)n
同時,這裡也查看一下主資料庫的狀態:
mysql> show master status G;n*************************** 1. row ***************************n File: mysql-bin.000003n Position: 154n Binlog_Do_DB:n Binlog_Ignore_DB:nExecuted_Gtid_Set:n1 row in set (0.00 sec)nnERROR:nNo query specifiedn
這裡:File和Position的值是兩個很重要的參數,這是從資料庫下次同步時的起始位置。
5.2主資料庫實例創建用於同步的賬號
在主資料庫創建一個replication賬號用於從主庫同步數據到從庫,創建replication賬號的語句如下:
grant replication slave on *.* to replication@% identified by your_password;flush privileges;
在實際執行時請將your_password改為自己的密碼,執行這個SQL語句後就會在mysql庫中的user表中創建一個名為replication的用戶,可以通過SQL語句查看:
mysql> show grants for "replication"@"%";n+-----------------------------------------------------+n| Grants for replication@% |n+-----------------------------------------------------+n| GRANT REPLICATION SLAVE ON *.* TO replication@% |n+-----------------------------------------------------+n1 row in set (0.00 sec)n
5.3從資料庫實例設置server-id
同樣,其實在上面的配置文件里已經配置了server-id和bin-log,就在/etc/my.cnf文件的[mysqld3307]節點處:
[mysqld3307]
server-id = 2
注意:在一個主從關係群中server-id是唯一的,另外在由於從資料庫不向其它庫同步數據,因此沒有開啟bin-log。這裡也通過egrep查看一下:
[root@localhost ~]# egrep "server-id|log-bin" /etc/my.cnf
server-id = 1
log-bin= /usr/local/mysql-5.7.18/data/3306/mysql-bin
server-id = 2
5.4從資料庫實例配置同步參數
登錄3307埠對應的實例:
cd /usr/local/mysql-5.7.18
./bin/mysql -u root -p -S ./data/3307/mysql.sock
然後執行以下語句:
CHANGE MASTER TO nMASTER_HOST=127.0.0.1, nMASTER_PORT=3306, nMASTER_USER=replication, nMASTER_PASSWORD=your_password, nMASTER_LOG_FILE=mysql-bin.000003, nMASTER_LOG_POS=154;n
說明:
MASTER_HOST為主伺服器IP或主機名;
MASTER_PORT為主伺服器埠;
MASTER_USER為主伺服器上用於同步的資料庫賬戶名;
MASTER_PASSWORD為主伺服器上用於同步的資料庫賬戶對應的密碼;
MASTER_LOG_FILE為當前bin-log日誌文件名;
MASTER_LOG_POS為當前偏移量;
其中MASTER_LOG_FILE和MASTER_LOG_POS可以在主資料庫上執行」 show master status G;」SQL語句來獲得,見本文「主資料庫實例設置server-id和開啟bin-log」一節。
5.5從資料庫實例啟動同步開關
登錄從資料庫執行」start slave」即可。
在本篇中由於連個資料庫都是剛剛初始化的,所以數據都是一致的。在實際情況中,需要先將兩個資料庫實例中的除mysql庫之外的數據一致後才可啟動同步,否則兩個庫中的就會不一致。
這裡我們來檢查一下效果:
在主庫中創建一個資料庫,如下:
mysql> create database zhoufoxcn;nQuery OK, 1 row affected (0.00 sec)nnmysql> show databases;n+--------------------+n| Database |n+--------------------+n| information_schema |n| mysql |n| performance_schema |n| sys |n| zhoufoxcn |n+--------------------+n5 rows in set (0.01 sec)n
然後在從庫中查看資料庫的情況:
mysql> show databases;n+--------------------+n| Database |n+--------------------+n| information_schema |n| mysql |n| performance_schema |n| sys |n| zhoufoxcn |n+--------------------+n5 rows in set (0.00 sec)n
可見主庫的數據被自動同步到從庫了,這時再在主庫中刪除zhoufoxcn這個庫,可以看到從庫中也自動刪掉了。
這時在主庫查看狀態:
mysql> show master status G;n*************************** 1. row ***************************n File: mysql-bin.000003n Position: 488n Binlog_Do_DB:n Binlog_Ignore_DB:nExecuted_Gtid_Set:n1 row in set (0.00 sec)nnERROR:nNo query specifiedn
在從庫查看狀態:
mysql> show slave status G;n*************************** 1. row ***************************n Slave_IO_State: Waiting for master to send eventn Master_Host: 127.0.0.1n Master_User: replicationn Master_Port: 3306n Connect_Retry: 60n Master_Log_File: mysql-bin.000003n Read_Master_Log_Pos: 488n Relay_Log_File: relay-bin.000008n Relay_Log_Pos: 701n Relay_Master_Log_File: mysql-bin.000003n Slave_IO_Running: Yesn Slave_SQL_Running: Yesn Replicate_Do_DB:n Replicate_Ignore_DB: mysqln Replicate_Do_Table:n Replicate_Ignore_Table:n Replicate_Wild_Do_Table:n Replicate_Wild_Ignore_Table:n Last_Errno: 0n Last_Error:n Skip_Counter: 0n Exec_Master_Log_Pos: 488n Relay_Log_Space: 1115n Until_Condition: Nonen Until_Log_File:n Until_Log_Pos: 0n Master_SSL_Allowed: Non Master_SSL_CA_File:n Master_SSL_CA_Path:n Master_SSL_Cert:n Master_SSL_Cipher:n Master_SSL_Key:n Seconds_Behind_Master: 0nMaster_SSL_Verify_Server_Cert: Non Last_IO_Errno: 0n Last_IO_Error:n Last_SQL_Errno: 0n Last_SQL_Error:n Replicate_Ignore_Server_Ids:n Master_Server_Id: 1n Master_UUID: da584607-4d19-11e7-8903-080027f376f5n Master_Info_File: /usr/local/mysql-5.7.18/data/3307/data/master.infon SQL_Delay: 0n SQL_Remaining_Delay: NULLn Slave_SQL_Running_State: Slave has read all relay log; waiting for more updatesn Master_Retry_Count: 86400n Master_Bind:n Last_IO_Error_Timestamp:n Last_SQL_Error_Timestamp:n Master_SSL_Crl:n Master_SSL_Crlpath:n Retrieved_Gtid_Set:n Executed_Gtid_Set:n Auto_Position: 0n Replicate_Rewrite_DB:n Channel_Name:n Master_TLS_Version:n1 row in set (0.00 sec)nnERROR:nNo query specifiedn
可以看出通過配置,兩台資料庫能夠自動同步數據了。
6.總結
本篇主要講述了如何從MySQL官方網站下載源代碼通過編譯和安裝,然後配置成多個實例運行,在最後還講解了如何配置MySQL資料庫主從同步。在實際情況中,可能有人在用MariaDB了,這是原MySQL開發人員在Oracle收購了Sun之後(Sun收購了MySQL),擔心Oracle不再繼續開源MySQL而開發的一套開源數據系統,它們有很多相似性,在本篇沒有涉及。另外,本篇沒有涉及的還有通過mysqld_multi.server來設置將MySQL隨系統啟動。
推薦閱讀:
※想學習 Linux,裝個虛擬機,裝哪個發行版好?
※linux下隱藏自己的文件和進程是如何做到的?
※發現自己有系統和軟體升級強迫症怎麼辦?
※在使用coroutine+asio多線程框架的時候,如何維護連接池復用連接?