一、安装
1.1 yum 安装
[root@linux-node1 ~]# yum install mariadb mariadb-server -y
1.2 启动并初始化
[root@linux-node1 ~]# service mariadb start
[root@linux-node1 ~]# 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!
1.3 检查登录情况
[root@linux-node1 ~]# mysql -uroot -p123456
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)]>
二、创建多实例
2.1 创建目录
[root@linux-node1 ~]# mkdir /home/multiMysql
[root@linux-node1 ~]# mkdir /home/multiMysql/{etc,socket,bin,datadir}
在/home目录下创建multiMysql文件夹,并在里面创建etc,socket,bin,datadir这四个文件夹备用。
现在我们在datadir中创建3个文件夹以放置三个实例的数据文件:3307,3308,3309
[root@linux-node1 ~]# mkdir /home/multiMysql/datadir/{3307,3308,3309}
然后用mysql_install_db来生成即将使用的多个实例的数据文件,首先需要对/home/multiMysql进行递归授权防止之后的操作出现权限不够的情况:
[root@linux-node1 ~]# chmod -R 777 /home/multiMysql
2.2 初始化实例
$ 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.01 sec)
--user是指mysql实例将使用的在linux系统中的用户,最好命名为mysql,yum安装后一般都有这个用户,如果没有可以自主创建:
2.3 创建多实例配置文件
2.3.1 创建共用配置文件
$ 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=row
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=\\u@\\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
2.3.2 创建三个实例的配置文件:
#3307
#vim 3307.cnf
[client]
port = 3307
socket = /home/multiMysql/socket/mysql3307.sock
[mysqld]
datadir=/home/multiMysql/datadir/3307
port = 3307
server_id =1
socket = /home/multiMysql/socket/mysql3307.sock
!includedir /home/multiMysql/etc/my.cnf.d
#3308
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
#3309
#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
2.4 启动实例
$ /usr/bin/mysqld_safe --defaults-file=/home/multiMysql/etc/3307.cnf &
$ /usr/bin/mysqld_safe --defaults-file=/home/multiMysql/etc/3308.cnf &
$ /usr/bin/mysqld_safe --defaults-file=/home/multiMysql/etc/3309.cnf &
2.5 登录查看
[root@linux-node1 etc]# 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)]>
MariaDB [(none)]> \q
Bye
[root@linux-node1 etc]# 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)]> \q
Bye
[root@linux-node1 etc]# 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)]>
2.6 创建密码
MariaDB [(none)]> update mysql.user set password=password('123456') where User="root" and Host="localhost";
MariaDB [(none)]> flush privileges;
2.7 停止实例
$ mysqladmin -uroot -p123456 -S /home/multiMysql/socket/mysql3307.sock shutdown
$ mysqladmin -uroot -p123456 -S /home/multiMysql/socket/mysql3308.sock shutdown
$ mysqladmin -uroot -p123456 -S /home/multiMysql/socket/mysql3309.sock shutdown
三、启动脚本
#3307
#vim /home/multiMysql/bin/mysql3307
#!/bin/bash
mysql_port=3307
mysql_username="root"
mysql_password="123456"
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
#3308
#vim /home/multiMysql/bin/mysql3308
#!/bin/bash
mysql_port=3308
mysql_username="root"
mysql_password="123456"
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
#3309
#vim /home/multiMysql/bin/mysql3309
#!/bin/bash
mysql_port=3309
mysql_username="root"
mysql_password="123456"
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
######################################################################
$ /usr/bin/mysqld_safe --defaults-file=/home/multiMysql/etc/3307.cnf &
$ /usr/bin/mysqld_safe --defaults-file=/home/multiMysql/etc/3308.cnf &
$ /usr/bin/mysqld_safe --defaults-file=/home/multiMysql/etc/3309.cnf &
遇到问题
数据库初始化后,必须用mysqld_safe进行后台启动,然后通过端口号进行登录,修改数据库的秘码以及权限调整。开始我直接用写好的脚本进行启动的,密码都是自己设定的肯定成功不了,看报错在百度也解决不了问题,浪费一天时间,看杨哥博客仔细看才发现出的问题,不够细心。感谢杨哥的博客非常认真!!!
网友评论