在Linux系統中安裝多個MySQL實例是比較常見的。下面就讓我們來詳細了解一下如何安裝兩個MySQL實例。
步驟一:安裝MySQL
$sudo apt-get install mysql-server
步驟二:創建第一個MySQL實例
$sudo mkdir /usr/local/mysql $sudo tar -xzvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz $sudo mv mysql-5.7.23-linux-glibc2.12-x86_64/* /usr/local/mysql/
步驟三:創建第一個MySQL實例的配置文件my.cnf
$sudo cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf $sudo vim /etc/my.cnf # 修改如下幾項: [mysqld] port=3306 datadir=/usr/local/mysql/data socket=/tmp/mysql.sock log-error=/usr/local/mysql/data/mysql.err pid-file=/usr/local/mysql/data/mysql.pid
步驟四:初始化第一個MySQL實例的數據目錄
$sudo mkdir /usr/local/mysql/data $sudo chmod -R 777 /usr/local/mysql/data $cd /usr/local/mysql/bin $sudo ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
步驟五:啟動第一個MySQL實例
$sudo ./mysqld_safe --user=mysql &
步驟六:登錄第一個MySQL實例并創建數據庫
$mysql -uroot -p Enter password: ***** mysql>create database test1; mysql>grant all privileges on test1.* to 'root'@'%' identified by 'password' with grant option; mysql>flush privileges; mysql>exit
步驟七:創建第二個MySQL實例
$sudo mkdir /usr/local/mysql2 $sudo tar -xzvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz $sudo mv mysql-5.7.23-linux-glibc2.12-x86_64/* /usr/local/mysql2/
步驟八:創建第二個MySQL實例的配置文件my.cnf
$sudo cp /usr/local/mysql/support-files/my-default.cnf /etc/my2.cnf $sudo vim /etc/my2.cnf # 修改如下幾項: [mysqld] port=3307 datadir=/usr/local/mysql2/data socket=/tmp/mysql2.sock log-error=/usr/local/mysql2/data/mysql.err pid-file=/usr/local/mysql2/data/mysql.pid
步驟九:初始化第二個MySQL實例的數據目錄
$sudo mkdir /usr/local/mysql2/data $sudo chmod -R 777 /usr/local/mysql2/data $cd /usr/local/mysql2/bin $sudo ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql2 --datadir=/usr/local/mysql2/data
步驟十:啟動第二個MySQL實例
$sudo ./mysqld_safe --defaults-file=/etc/my2.cnf --user=mysql &
步驟十一:登錄第二個MySQL實例并創建數據庫
$mysql -uroot -p -h 127.0.0.1 -P 3307 Enter password: ***** mysql>create database test2; mysql>grant all privileges on test2.* to 'root'@'%' identified by 'password' with grant option; mysql>flush privileges; mysql>exit
安裝兩個MySQL實例就完成了!