1. 程式人生 > >CentOS7下yum安裝mysql配置多例項

CentOS7下yum安裝mysql配置多例項

最近想試試Mysql的主從複製功能“Mysql Replication”,但是苦於沒多臺電腦,於是考慮在Linux上配置多個Mysql例項做為測試環境。

環境:虛擬機器上的CentOS7

首先得在CentOS上安裝MySQL,下載原始碼編譯太麻煩,於是考慮用yum直接安裝。由於在CentOS7上已經沒有了MySQL的yum源,於是我們這次用MariaDB替換Mysql,畢竟兩者的差距不大,在使用上基本上是互通的。

yum安裝MariaDB:

yum install mariadb mariadb-server

安裝完成後要進行手動開啟MySQL服務並初始化:
service mariadb start
[[email protected] ~]# mysql_secure_installation


NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.


Enter current password for root (enter for none): 
OK, successfully used password, moving on...


Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.


Set root password? [Y/n] Y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!




By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.


Remove anonymous users? [Y/n] Y
 ... Success!


Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.


Disallow root login remotely? [Y/n] n
 ... skipping.


By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.


Remove test database and access to it? [Y/n] n
 ... skipping.


Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.


Reload privilege tables now? [Y/n] Y
 ... Success!


Cleaning up...


All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.


Thanks for using MariaDB!

然後測試是否能夠正常登入:
[[email protected] ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

說明已經能正常登入了,我們可以檢視MySQL的使用者許可權了:
MariaDB [(none)]> select user,host,password from mysql.user \G;
*************************** 1. row ***************************
    user: root
    host: localhost
password: *FC3D158275832C2A64C4A6ECD9154A703179BF8F
*************************** 2. row ***************************
    user: root
    host: 127.0.0.1
password: *FC3D158275832C2A64C4A6ECD9154A703179BF8F
*************************** 3. row ***************************
    user: root
    host: ::1
password: *FC3D158275832C2A64C4A6ECD9154A703179BF8F
*************************** 4. row ***************************
    user: root
    host: %
password: *FC3D158275832C2A64C4A6ECD9154A703179BF8F
4 rows in set (0.01 sec)

在這裡我們可以看到有四個登入賬號,user表示登入名,host表示登入主機限制,password為雜湊後的登入密碼,其中%表示任意,如host的%表示可以任意主機登入,在之後會寫到怎樣修改這些登入資料。

現在我們退出MySQL登入,準備做MySQL上的多例項。

[[email protected] home]# mkdir /home/multiMysql
[[email protected] home]# mkdir /home/multiMysql/{etc,socket,bin,datadir}
在/home目錄下建立multiMysql資料夾,並在裡面建立etc,socket,bin,datadir這四個資料夾備用。
現在我們在datadir中建立3個資料夾以放置三個例項的資料檔案:3307,3308,3309
[[email protected] multiMysql]# mkdir /home/multiMysql/datadir/{3307,3308,3309}
然後用mysql_install_db來生成即將使用的多個例項的資料檔案,首先需要對/home/multiMysql進行遞迴授權防止之後的操作出現許可權不夠的情況:
chmod -R 777 /home/multiMysql
mysql_install_db --basedir=/usr --datadir=/home/multiMysql/datadir/3307 --user=mysql
mysql_install_db --basedir=/usr --datadir=/home/multiMysql/datadir/3308 --user=mysql
mysql_install_db --basedir=/usr --datadir=/home/multiMysql/datadir/3309 --user=mysql
其中的引數--basedir是指mysql的二進位制檔案目錄(誤?),--datadir是指即將安裝到的資料庫檔案目錄,如果不知道--basedir該怎麼填,可以登入進mysql後查詢:
MariaDB [(none)]> show variables like '%basedir%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| basedir     | /usr |
+---------------+-------+
1 row in set (0.00 sec)

--user是指mysql例項將使用的在linux系統中的使用者,最好命名為mysql,yum安裝後一般都有這個使用者,如果沒有可以自主建立:

groupadd mysql
adduser -g mysql mysql
現在來檢視三份資料檔案有沒有生成,例如檢視3308的:
[[email protected] multiMysql]# ls /home/multiMysql/datadir/3308/
aria_log.00000001  aria_log_control  mysql  performance_schema  test
如果裡面有檔案代表生成成功。

接下來我們來做多例項的配置:

先建立一個公用配置檔案:

mkdir /home/multiMysql/etc/my.cnf.d/
vim /home/multiMysql/etc/my.cnf.d/my.cnf
[mysqld]
skip-name-resolve
lower_case_table_names=1
innodb_file_per_table=1
back_log = 50
max_connections = 300
max_connect_errors = 1000
table_open_cache = 2048
max_allowed_packet = 16M
binlog_cache_size = 2M
max_heap_table_size = 64M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 64
thread_concurrency = 8
query_cache_size = 64M
query_cache_limit = 2M
ft_min_word_len = 4
default-storage-engine = innodb
thread_stack = 192K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 64M
log-bin=mysql-bin
binlog_format=mixed
slow_query_log
long_query_time = 1
server-id = 1
key_buffer_size = 8M
read_buffer_size = 2M
read_rnd_buffer_size = 2M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 200M
innodb_data_file_path = ibdata1:10M:autoextend
innodb_file_io_threads = 8
innodb_thread_concurrency = 16
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 60
innodb_lock_wait_timeout = 120
[mysqldump]
quick
max_allowed_packet = 256M
[mysql]
no-auto-rehash
prompt=\\[email protected]\\d \\R:\\m>
[myisamchk]
key_buffer_size = 512M
sort_buffer_size = 512M
read_buffer = 8M
write_buffer = 8M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open-files-limit = 8192
然後分別建立三個例項的配置檔案:
[[email protected] etc]# vim 3307.cnf 
[client]
port = 3307
socket = /home/multiMysql/socket/mysql3307.sock
[mysqld]
datadir=/home/multiMysql/datadir/3307
port = 3307
socket = /home/multiMysql/socket/mysql3307.sock

!includedir /home/multiMysql/etc/my.cnf.d
然後把3307.cnf複製兩份:
[[email protected] etc]# cp 3307.cnf 3308.cnf
[[email protected] etc]# cp 3307.cnf 3309.cnf
然後分別編輯複製的兩份配置檔案,把埠和socket進行修改:
[[email protected] etc]# vim 3308.cnf 
[client]
port = 3308
socket = /home/multiMysql/socket/mysql3308.sock
[mysqld]
datadir=/home/multiMysql/datadir/3308
port = 3308
socket = /home/multiMysql/socket/mysql3308.sock

!includedir /home/multiMysql/etc/my.cnf.d
[[email protected] etc]# vim 3309.cnf 
[client]
port = 3309
socket = /home/multiMysql/socket/mysql3309.sock
[mysqld]
datadir=/home/multiMysql/datadir/3309
port = 3309
socket = /home/multiMysql/socket/mysql3309.sock

!includedir /home/multiMysql/etc/my.cnf.d
在配置檔案中,port是例項的埠,socket是例項執行時的sock檔案,datadir是之前我們生成的資料庫檔案位置。

然後我們來編輯三個啟動指令碼:

[[email protected] bin]# vim /home/multiMysql/bin/mysql3307
#!/bin/bash
mysql_port=3307
mysql_username="root"
mysql_password=""
function_start_mysql()
{
printf "Starting MySQL...\n"
mysqld_safe --defaults-file=/home/multiMysql/etc/${mysql_port}.cnf 2>&1 > /dev/null &
}
function_stop_mysql()
{
printf "Stoping MySQL...\n"
mysqladmin -u ${mysql_username} -p${mysql_password} -S /home/multiMysql/socket/mysql${mysql_port}.sock shutdown
}
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
function_start_mysql
}
function_kill_mysql()
{
kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}')
kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}')
}
case $1 in
start)
function_start_mysql;;
stop)
function_stop_mysql;;
kill)
function_kill_mysql;;
restart)
function_stop_mysql
function_start_mysql;;
*)
echo 

[[email protected] bin]# vim /home/multiMysql/bin/mysql3308
#!/bin/bash
mysql_port=3308
mysql_username="root"
mysql_password=""
function_start_mysql()
{
printf "Starting MySQL...\n"
mysqld_safe --defaults-file=/home/multiMysql/etc/${mysql_port}.cnf 2>&1 > /dev/null &
}
function_stop_mysql()
{
printf "Stoping MySQL...\n"
mysqladmin -u ${mysql_username} -p${mysql_password} -S /home/multiMysql/socket/mysql${mysql_port}.sock shutdown
}
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
function_start_mysql
}
function_kill_mysql()
{
kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}')
kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}')
}
case $1 in
start)
function_start_mysql;;
stop)
function_stop_mysql;;
kill)
function_kill_mysql;;
restart)
function_stop_mysql
function_start_mysql;;
*)
echo "Usage: /data/dbdata_${mysql_port}/mysqld {start|stop|restart|kill}";;
esac
[[email protected] bin]# vim /home/multiMysql/bin/mysql3309
#!/bin/bash
mysql_port=3309
mysql_username="root"
mysql_password=""
function_start_mysql()
{
printf "Starting MySQL...\n"
mysqld_safe --defaults-file=/home/multiMysql/etc/${mysql_port}.cnf 2>&1 > /dev/null &
}
function_stop_mysql()
{
printf "Stoping MySQL...\n"
mysqladmin -u ${mysql_username} -p${mysql_password} -S /home/multiMysql/socket/mysql${mysql_port}.sock shutdown
}
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
function_start_mysql
}
function_kill_mysql()
{
kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}')
kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}')
}
case $1 in
start)
function_start_mysql;;
stop)
function_stop_mysql;;
kill)
function_kill_mysql;;
restart)
function_stop_mysql
function_start_mysql;;
*)
echo "Usage: /data/dbdata_${mysql_port}/mysqld {start|stop|restart|kill}";;
esac

因為是yum安裝,所以mysqld_safe和mysqladmin可以不用加路徑直接執行,另外mysql_port是指這個bash簡要開啟的例項的埠,mysql_username和mysql_userpassword為我們即將在例項中配置的可關閉mysql程序的mysql使用者名稱和密碼。

現在給三個bash檔案許可權來執行,並嘗試開啟三個例項:

[[email protected] bin]# chmod 777 /home/multiMysql/bin/mysql3307
[[email protected] bin]# chmod 777 /home/multiMysql/bin/mysql3308
[[email protected] bin]# chmod 777 /home/multiMysql/bin/mysql3309

先關閉yum安裝的預設mysql例項程序:

sudo service mariadb stop
啟動三個例項:
[[email protected] bin]# /home/multiMysql/bin/mysql3307 start
[[email protected] bin]# /home/multiMysql/bin/mysql3308 start
[[email protected] bin]# /home/multiMysql/bin/mysql3309 start
檢視是否有三個mysql程序:
[[email protected] bin]# ps -ef | grep mysql
root      47013      1  0 19:57 pts/0    00:00:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/home/multiMysql/etc/3307.cnf
mysql     47680  47013  2 19:57 pts/0    00:00:04 /usr/libexec/mysqld --defaults-file=/home/multiMysql/etc/3307.cnf --basedir=/usr --datadir=/home/multiMysql/datadir/3307 --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/home/multiMysql/datadir/3307/localhost.localdomain.err --open-files-limit=8192 --pid-file=localhost.localdomain.pid --socket=/home/multiMysql/socket/mysql3307.sock --port=3307
root      50504      1  0 20:00 pts/0    00:00:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/home/multiMysql/etc/3308.cnf
mysql     51183  50504  9 20:00 pts/0    00:00:03 /usr/libexec/mysqld --defaults-file=/home/multiMysql/etc/3308.cnf --basedir=/usr --datadir=/home/multiMysql/datadir/3308 --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/home/multiMysql/datadir/3308/localhost.localdomain.err --open-files-limit=8192 --pid-file=localhost.localdomain.pid --socket=/home/multiMysql/socket/mysql3308.sock --port=3308
root      51224      1  0 20:00 pts/0    00:00:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/home/multiMysql/etc/3309.cnf
mysql     51952  51224  2 20:00 pts/0    00:00:00 /usr/libexec/mysqld --defaults-file=/home/multiMysql/etc/3309.cnf --basedir=/usr --datadir=/home/multiMysql/datadir/3309 --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/home/multiMysql/datadir/3309/localhost.localdomain.err --open-files-limit=8192 --pid-file=localhost.localdomain.pid --socket=/home/multiMysql/socket/mysql3309.sock --port=3309
root      52445   3644  0 20:01 pts/0    00:00:00 grep --color=auto mysql
可以看到三個例項已經啟動,我們來嘗試連線三個例項的sock:
[[email protected] bin]# mysql -u root -S /home/multiMysql/socket/mysql3307.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> quit
Bye
[[email protected] bin]# mysql -u root -S /home/multiMysql/socket/mysql3308.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> quit
Bye
[[email protected] bin]# mysql -u root -S /home/multiMysql/socket/mysql3309.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> quit
Bye
可見三個例項已經啟動,可以進每個例項後檢視當前例項的埠以確認是否成功開啟例項:

例如連線3307的sock後執行sql:

MariaDB [(none)]> show variables like '%port%';
+-------------------------------------+-------+
| Variable_name                       | Value |
+-------------------------------------+-------+
| extra_port                          | 0     |
| innodb_import_table_from_xtrabackup | 0     |
| innodb_support_xa                   | ON    |
| large_files_support                 | ON    |
| port                                | 3307  |
| progress_report_time                | 5     |
| report_host                         |       |
| report_password                     |       |
| report_port                         | 3307  |
| report_user                         |       |
+-------------------------------------+-------+
10 rows in set (0.00 sec)
可見確實已經成功打開了。

下一步嘗試遠端連線mysql例項:

查得虛擬機器橋接的ip地址為:192.168.1.156



提示不允許遠端連線,可知是許可權不夠。在虛擬機器中分別sock連線三個資料庫並建立使用者許可權:

MariaDB [(none)]> grant all privileges on *.* to 'root'@'%' identified by 'poklau123' with grant option;
MariaDB [(none)]> flush privileges;
解釋一下這兩句sql的意思,grant是許可權授予,all privileges表示授予所有許可權,on表示授予的操作物件,*.*表示所有資料庫的所有表,to表示授予的物件‘root’@'%'表示任意主機的root登入使用者,identified by 'poklau123'表示設定密碼為poklau123。flush privileges表示重新整理使用者許可權,不重新整理的話許可權是不會立刻生效的。

現在重新嘗試連線:


連線成功,三個例項都授予許可權後三個例項都能分別連線了。

關閉例項:

[[email protected] bin]# /home/multiMysql/bin/mysql3307 stop
[[email protected] bin]# /home/multiMysql/bin/mysql3308 stop
[[email protected] bin]# /home/multiMysql/bin/mysql3309 stop

然後檢視程序是否成功關閉:
[[email protected] bin]# ps -ef | grep mysql
root      74999   3644  0 20:27 pts/0    00:00:00 grep --color=auto mysql
可見三個例項已經成功關閉,如果未能成功關閉,說明是bash檔案中使用者名稱密碼對應mysql例項裡的使用者密碼許可權不夠或錯誤,調整許可權即可。至此,三個mysql例項建立到此結束。