您好,登錄后才能下訂單哦!
MySQL多實例的配置
通過上文 CentOS 6.8 編譯安裝MySQL5.5.32 ,我們完成了編譯安裝,接下配置多實例
本圖借鑒徐亮偉"思維簡圖"
5,添加多實例目錄
[root@db01 application]# mkdir -p /data/{3306,3307}/data [root@db01 application]# tree /data/ /data/ ├── 3306 │ └── data └── 3307 └── data,
6,上傳多實例配置文件
[root@db01 /]# cd / [root@db01 /]# rz -y data.zip [root@db01 /]# unzip data.zip [root@db01 /]# tree data data ├── 3306 ##3306實例目錄 │ ├── data │ ├── my.cnf │ └── mysql └── 3307 ├── data ├── my.cnf └── mysql
##多實例 文件對比 配置文件中 只有字符串 3306與3307,與serverID 的不同
7,給Mysql啟動腳本增加執行權限
find /data -type f -name "mysql"|xargs chmod 700 chown -R mysql.mysql /data
8,多實例初始化,作用就是加載mysql自帶的庫與表##
cd /application/mysql/scripts ./mysql_install_db --defaults-file=/data/3306/my.cnf --basedir=/application/mysql/ --datadir=/data/3306/data --user=mysql ./mysql_install_db --defaults-file=/data/3307/my.cnf --basedir=/application/mysql/ --datadir=/data/3307/data --user=mysql ##提示:--basedir=/application/mysql/為MySQL的安裝路徑,--datadir為數據文件目錄。 另,注意mysql_install_db和MySQL5.1的路徑不同,MySQL5.1不在MySQL bin路徑下了。
編譯過程,注意要有2個OK 才算成功
[root@db02 scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql WARNING: The host 'db02' could not be looked up with resolveip. This probably means that your libc libraries are not 100 % compatible with this binary MySQL version. The MySQL daemon, mysqld, should work normally with the exception that host name resolving will not work. This means that you should use IP addresses instead of hostnames when specifying MySQL privileges ! Installing MySQL system tables... OK #<==兩個OK是初始化成功的標志。 Filling help tables... OK #<==兩個OK是初始化成功的標志。 To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands: /application/mysql/bin/mysqladmin -u root password 'new-password' /application/mysql/bin/mysqladmin -u root -h db02 password 'new-password' Alternatively you can run: /application/mysql/bin/mysql_secure_installation which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers. See the manual for more instructions. You can start the MySQL daemon with: cd /application/mysql ; /application/mysql/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd /application/mysql/mysql-test ; perl mysql-test-run.pl Please report any problems with the /application/mysql/scripts/mysqlbug script! ###請注意如下幾行英文的說明及報錯 New default config file was createdas /application/mysql//my.cnf and will be used by default by theserver when you start it. You may edit this file to changeserver settings #從上文說明中可以指導mysql的默認配置文件已經變到了/application/mysql//my.cnf WARNING: Default config file/etc/my.cnf exists on the system This file will be read by defaultby the MySQL server If you do not want to use this,either remove it, or use the --defaults-file argument tomysqld_safe when starting the server #從上文說明中可以看到數據庫啟動時會讀取/etc/my.cnf,因此有可能會導致無法啟動,避免的方法就是使用mysqld_safe啟動服務時采用--defaults-file參數指定配置文件,前文已將/etc/my.cnf改名了,所以,就不需要指定參數了,這里是一個坑,讀者要注意。 此步驟必須要初始化成功,否則,后面會出現登錄不了數據庫等各種問題。 [root@oldboy ~]# mysql ERROR1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 解決辦法:重新初始化數據庫即可,此問題一般都是數據庫初始化問題,或者數據庫文件損壞,以及目錄權限問題
9,MySQL 初始化原理
初始化數據庫的就是加載MySQL自帶的庫與表 例如:生成的test及mysql庫表等。
初始化數據庫后,查看數據目錄,生成哪些文件
[root@db02 scripts]# ls -l /data/3306/data/ total 143372 -rw-rw---- 1 mysql mysql 134217728 Apr 29 03:32 ibdata1 -rw-rw---- 1 mysql mysql 4194304 Apr 29 03:32 ib_logfile0 -rw-rw---- 1 mysql mysql 4194304 Apr 29 03:32 ib_logfile1 -rw-rw---- 1 mysql mysql 4194304 Apr 29 03:32 ib_logfile2 drwx------ 2 mysql root 4096 Apr 29 03:31 mysql ##用于存放管理mysql數據 drwx------ 2 mysql mysql 4096 Apr 29 03:31 performance_schema #內部性能庫 drwx------ 2 mysql root 4096 Apr 29 03:31 test ##測試庫
10,啟動MySQL 命令 多實例(3306/3307)
[root@db02 scripts]# /data/3306/mysql start ##啟動腳本見百度云連接 Starting MySQL... [root@db02 scripts]# /data/3307/mysql start Starting MySQL... [root@db02 scripts]# ps -ef|grep 330 root 13426 1 0 11:32 pts/1 00:00:00 /bin/sh /application/mysql/bin/mysqld_safe --defaults-file=/data/3306/my.cnf mysql 14150 13426 0 11:32 pts/1 00:00:05 /application/mysql-5.5.32/bin/mysqld --defaults-file=/data/3306/my.cnf --basedir=/application/mysql --datadir=/data/3306/data --plugin-dir=/application/mysql/lib/plugin --user=mysql --log-error=/data/3306/mysql_oldboy3306.err --open-files-limit=1024 --pid-file=/data/3306/mysqld.pid --socket=/data/3306/mysql.sock --port=3306 root 17840 1 0 12:57 pts/1 00:00:00 /bin/sh /application/mysql/bin/mysqld_safe --defaults-file=/data/3307/my.cnf mysql 18540 17840 15 12:57 pts/1 00:00:01 /application/mysql-5.5.32/bin/mysqld --defaults-file=/data/3307/my.cnf --basedir=/application/mysql --datadir=/data/3307/data --plugin-dir=/application/mysql/lib/plugin --user=mysql --log-error=/data/3307/mysql_oldboy3307.err --open-files-limit=1024 --pid-file=/data/3307/mysqld.pid --socket=/data/3307/mysql.sock --port=3307 root 18558 1286 0 12:57 pts/1 00:00:00 grep --color=auto 330 ##如果啟動失敗,請檢查日志,路徑是在 每個實例下都有個err文件,例如下面的3306 [root@db02 scripts]# vi /data/3306/mysql_oldboy3306.err #MySQL5.6.36版本會出現如下錯誤: [root@db02 scripts]# /data/3306/mysql start Starting MySQL... 170508 12:56:08 mysqld_safe error: log-error set to '/data/3306/oldboy_3306.err', however file don't exists. Create writable for user 'mysql'. #解決方法: touch /data/3306/oldboy_3306.err chown mysql.mysql /data/3306/oldboy_3306.err /data/3306/mysql start ##3307實例也是如此操作,主要是缺少錯誤日志文件導致無法啟動
11,連接MySQL數據庫( 以socket區分,多實例)
[root@db02 scripts]# mysql -uroot -p -S /data/3306/mysql.sock #-S指定要啟動實例的socket-bash: mysql: command not found #報錯是因為沒有設定環境變量 解決方法的兩種思路: (建議使用第二種) 1)將mysql 路徑添加到環境變量 echo "PATH=$PATH:/application/mysql/bin/">>/etc/profile source /etc/profilep 2)將mysql 需要執行的文件或腳本,拷貝到第一個已經有環境變量權限的目錄下,去執行。 cp /application/mysql/bin/* /usr/local/sbin/ 再次連接,登錄3306 [root@db02 ~]# mysql -uroot -p -S /data/3306/mysql.sock [root@db02 ~]# mysql -uroot -p -S /data/3307/mysql.sock Enter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.5.32-log Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
12,添加開機自啟動 (多實例 multi instances)
[root@db02 ~]# echo "# mysql multi instances" >>/etc/rc.local #添加注釋,及實例啟動項 [root@db02 ~]# echo "/data/3306/mysql start " >>/etc/rc.local [root@db02 ~]# echo "/data/3307/mysql start " >>/etc/rc.loca
13,新增一個3308實例
mkdir -p /data/3308/data \cp /data/3306/my.cnf /data/3308/ \cp /data/3306/mysql /data/3308/ sed -i 's/3306/3308/g' /data/3308/my.cnf sed -i 's/server-id = 6/server-id = 8/g' /data/3308/my.cnf sed -i 's/3306/3308/g' /data/3308/mysql chown -R mysql:mysql /data/3308 chmod 700 /data/3308/mysql cd /application/mysql/scripts ./mysql_install_db --defaults-file=/data/3308/my.cnf --datadir=/data/3308/data --basedir=/application/mysql --user=mysql chown -R mysql:mysql /data/3308 egrep "server-id|log-bin" /data/3308/my.cnf touch /data/3308/oldboy_3308.err chown mysql /data/3308/oldboy_3308.err /data/3308/mysql start #mysql啟動慢,如果此段命令放到腳本中支持,需要sleep 3-5秒 netstat -lnt|grep 3308
測試3308實例
[root@db02 scripts]# mysqld_safe /data/3308/mysql.sock #目前已啟動3個實例 [root@db02 scripts]# netstat -lnt|grep 330 tcp 0 0 :::3306 :::* LISTEN tcp 0 0 :::3307 :::* LISTEN tcp 0 0 :::3308 :::* LISTEN
14,Mysql多實例啟動故障排錯說明
如果MySQL多實例有服務沒有被啟動,排除方法如下:
如果發現沒有顯示MySQL對應的實例端口,請稍微等待幾秒在檢查,MySQL服務的啟動稍慢
如果還是不行,請查看MySQL實例對應的錯誤日志,路徑在my.cnf配置文件中最下面定義:
例如:3306實例的錯誤日志為:/data/3306/*.err
細看所有執行命令返回的屏幕輸出,不要忽略關鍵的輸出內容。
輔助查看系統日志/var/log/messages
如果是MySQL關聯了其他服務,同時查看相關服務日志
仔細閱讀,重新查看操作的步驟是否正確,書寫命令及字符串是否正確。
多看日志,多多總結,終究會稱為高手的。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。