更多博文請關注:沒有傘的孩子必須努力奔跑 (www.xuchanggang.cn)
mysql
在咱們的生產環境中,不多一臺db server就一個mysql實例,從節約成本和充分利用資源的角度出發,咱們每每會在主從環境下,對從機的db server 安裝多個mysql實例,用來同步主db server的相應實例sql
mysqld_multi 的 option usage: myysqld_multi [optinos] {start|stop|report} [GNR[,GNR]..] option: start|stop |reportGNR 指的是[msyqld/N]中的 N,GNR 中不能有空格和 tab
[root@client100 mysql]# mkdir -p /home/mysql/data_3307 # 修改數據目錄的屬主 [root@client100 mysql]# chown mysql. -R /home/mysql/data_3307
[root@client100 mysql]# vim /etc/my.cnf [mysqld_multi] # 指定相關命令的路徑 mysqld = /usr/local/mysql/bin/mysqld_safe mysqladmin = /usr/local/mysql/bin/mysqladmin # 指定用戶,用於啓動和關閉mysql服務[這裏儘可能使用root用戶,默認root用戶擁有因此權限,記得在兩個實例中root密碼一致] user = root password = kongzhong # 指定錯誤日誌路徑 log = /usr/local/mysql/multi.log [mysqld3306] basedir =/usr/local/mysql datadir =/home/mysql/data socket = /usr/local/mysql/mysqld3306.sock pid-file = /home/mysql/data/mysql3306.pid general_log = 1 general_log_file = /home/mysql/data/mysql3306.log port =3306 server_id =100 user = mysql join_buffer_size = 128M sort_buffer_size = 2M read_rnd_buffer_size = 2M default_storage_engine = MYISAM join_buffer_size = 128M max_allowed_packet= 1M net_buffer_length= 8K skip-external-locking skip-host-cache skip-name-resolve # InnoDB innodb_buffer_pool_size = 128M innodb_log_file_size = 48M innodb_file_per_table = 1 innodb_flush_method = O_DIRECT # MyISAM key_buffer_size = 48M character-set-server=utf8 collation-server=utf8_general_ci # LOG log_error = /home/mysql/data/mysql-error.log long_query_time = 1 slow-query-log slow_query_log_file = /home/mysql/data/mysql-slow.log # Others explicit_defaults_for_timestamp=true max_connections = 500 open_files_limit = 65535 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [mysqld3307] basedir =/usr/local/mysql datadir =/home/mysql/data_3307 socket = /usr/local/mysql/mysqld3307.sock pid-file = /home/mysql/data_3307/mysql3307.pid general_log = 1 general_log_file = /home/mysql/data_3307/mysql3307.log port =3307 server_id =101 user = mysql join_buffer_size = 128M sort_buffer_size = 2M read_rnd_buffer_size = 2M default_storage_engine = MYISAM join_buffer_size = 128M max_allowed_packet= 1M net_buffer_length= 8K skip-external-locking skip-host-cache skip-name-resolve # InnoDB innodb_buffer_pool_size = 128M innodb_log_file_size = 48M innodb_file_per_table = 1 innodb_flush_method = O_DIRECT # MyISAM key_buffer_size = 48M character-set-server=utf8 collation-server=utf8_general_ci # LOG log_error = /home/mysql/data_3307/mysql-error.log long_query_time = 1 slow-query-log slow_query_log_file = /home/mysql/data_3307/mysql-slow.log # Others explicit_defaults_for_timestamp=true max_connections = 500 open_files_limit = 65535 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# 進入數據庫安裝目錄 [root@client100 mysql]# cd /usr/local/mysql/scripts/ # 執行初始化腳本,指定數據目錄/用戶 [root@client100 scripts]# ./mysql_install_db --basedir=/usr/local/mysql --datadir=/home/mysql/data_3307 --user=mysql
# 啓動相應實例 [root@client100 ~]# mysqld_multi start 3306 [root@client100 ~]# mysqld_multi start 3307 # 使用以下命令檢測 mysql 相應實例進程是否啓動 [root@client100 ~]# ps -ef |grep mysql # 使用netstat 檢測mysql兩個實例端口是否啓動[看到兩個端口的監聽都出現,就說明搭建正常] [root@client100 ~]# netstat -ntlp |grep mysql # 使用以下兩條命令,可登錄到對應實例數據庫 [root@client100 ~]# mysql -uroot -S /usr/local/mysql/mysqld3306.sock -p [root@client100 ~]# mysql -uroot -S /usr/local/mysql/mysqld3307.sock -p # 登錄後,可查看參數,查看數據存放目錄,以確認是否登錄本身要登錄的實例 mysql> show variables like '%datadir%' # 關閉實例 [root@client100 ~]# mysqld_multi stop 3306 [root@client100 ~]# mysqld_multi stop 3307 # 若是執行以上命令,不能關閉的話,請使用如下方式關閉 [root@client100 ~]# mysqladmin -uroot -p -S /usr/local/mysql/mysqld3307.sock shutdown [root@client100 ~]# mysqladmin -uroot -p -S /usr/local/mysql/mysqld3306.sock shutdown ## 有的朋友可能在使用 mysqld_multi stop 關閉時,始終有實例關閉不了,這裏就是/etc/my.cnf中user/password的做用 ## 須要作以下操做: ## 若是你指定的用戶不是root用戶,那最好擁有mysql的全部目錄權限,否則會報權限不夠,因此建議用mysql或root用戶 ## 若是是root用戶,由於root擁有全部權限,因此只要設置兩個實例的密碼都是同樣的就行 ## 而對於mysql,則編譯安裝時,建立用戶是就要容許此用戶登陸,即不要加nologin限制 ## 並登陸到實例數據庫後,授予shutdown權限,設置密碼 mysql> GRANT SHUTDOWN ON *.* TO 'mysql'@'localhost' IDENTIFIED BY 'kongzhong'; mysql> flush privileges ## 如今再用 mysqld_multi stop 關閉,就可正常關閉[仍是那句,建議用root]