前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CentOS系统使用yum安装MariaDB数据库实现多实例

CentOS系统使用yum安装MariaDB数据库实现多实例

作者头像
咻一咻
发布2020-05-29 11:48:13
8080
发布2020-05-29 11:48:13
举报
文章被收录于专栏:咻一咻

上一篇实现二进制安装MariaDB 现在开始yum安装MariaDB数据库实现多个实例 废话不多说直接开始

1.安装和启动

代码语言:javascript
复制
安装:yum install mariadb-server
路径:rpm -ql mariadb-server
[root@centos7 ~]#ls /var/lib/mysql //默认没东西
[root@centos7 ~]#systemctl start mariadb //启动mariadb
[root@centos7 ~]#ls /var/lib/mysql   //生成数据文件  
aria_log.00000001  aria_log_control  ibdata1  ib_logfile0  ib_logfile1  mysql  mysql.sock  performance_schema  test

配置文件
[root@centos7 ~]#cat  /etc/my.cnf
[mysqld]
port=3306 (这里以3306为例 想用什么端口在这里修改)
datadir=/var/lib/mysql //数据库路径
socket=/var/lib/mysql/mysql.sock //用于本机连接
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d

2.创建目录和修改权限

代码语言:javascript
复制
[root@centos7 ~]#mkdir /mysqldb/{3306,3307,3308}/{etc,socket,pid,log,data} -pv //创建目录
[root@centos7 ~]#getent passwd mysql //因为是yum安装不需要创建账号
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
[root@centos7 ~]#chown -R mysql.mysql /mysqldb/ //修改权限
  1. 准备配置文件
代码语言:javascript
复制
[root@centos7 ~]#mysql -e 'show variables like "basedir"' //查看mysql安装目录
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| basedir       | /usr  |
+---------------+-------+
//生成三套实例
[root@centos7 ~]#mysql_install_db --datadir=/mysqldb/3306/data --user=mysql --basedir=/usr 
[root@centos7 ~]#mysql_install_db --datadir=/mysqldb/3307/data --user=mysql --basedir=/usr
[root@centos7 ~]#mysql_install_db --datadir=/mysqldb/3308/data --user=mysql --basedir=/usr
//创建配置文件 以3306为例(3307,3307文件修改下数字即可)
[root@centos7 ~]cp /etc/my.cnf /mysqldb/3306/etc/
[root@centos7 ~]#cat  /mysqldb/3306/etc/my.cnf 
[mysqld]
datadir=/mysqldb/3306/data
socket=/mysqldb/3306/socket/mysql.sock
skip_grant_tables //再启动服务忽略授权变口令(_和- 都行)添加这一行表不能修改了
symbolic-links=0
[mysqld_safe]
log-error=/mysqldb/3306/log/mariadb.log
pid-file=/mysqldb/3306/pid/mariadb.pid

[root@centos7 ~]#cat /usr/lib/systemd/system/mariadb.service  //yum安装启动的配置文件
这里我们需要自己写配置文件;以3306为例(3307,3307文件修改下数字即可)
[root@centos7 3306]#cat mysqld     
#!/bin/bash
port=3306
mysql_user="root"
mysql_pwd="centos"
cmd_path="/usr/bin"  //参考 which mysqld_safe 
mysql_basedir="/mysqldb"
mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"

function_start_mysql()
{
    if [ ! -e "$mysql_sock" ];then
      printf "Starting MySQL...\n"
      ${cmd_path}/mysqld_safe --defaults-file=${mysql_basedir}/${port}/etc/my.cnf  &> /dev/null  &
      /******************************************************************
      破解口令 比上一行多 --skip_grant_tables
       ${cmd_path}/mysqld_safe --skip_grant_tables --defaults-file=${mysql_basedir}/${port}/etc/my.cnf  &> /dev/null  &
      //再启动服务忽略授权变口令(_和- 都行)添加这一行表不能修改了
      /******************************************************************
    else
      printf "MySQL is running...\n"
      exit
    fi
}


function_stop_mysql()
{
    if [ ! -e "$mysql_sock" ];then
       printf "MySQL is stopped...\n"
       exit
    else
       printf "Stoping MySQL...\n"
       ${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown
   fi
}


function_restart_mysql()
{
    printf "Restarting MySQL...\n"
    function_stop_mysql
    sleep 2
    function_start_mysql
}

case $1 in
start)
    function_start_mysql
;;
stop)
    function_stop_mysql
;;
restart)
    function_restart_mysql
;;
*)
    printf "Usage: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"
esac

第四步:设置权限并启动

代码语言:javascript
复制
[root@centos7 3306]#chmod 700 mysqld  设置权限 因为里面有密码 设置为700
[root@centos7 3306]#ll ../3308/mysqld           
-rwx------ 1 root root 1000 Jan 23 18:18 ../3308/mysqld
[root@centos7 3306]#ll ../3307/mysqld  
-rwx------ 1 root root 1000 Jan 23 18:17 ../3307/mysqld
[root@centos7 3306]#ll ../3306/mysqld  
-rwx------ 1 root root 1000 Jan 23 18:15 ../3306/mysqld

启动多实例
[root@centos7]#/mysqldb/3306/mysqld start
[root@centos7]#/mysqldb/3307/mysqld start
[root@centos7]#/mysqldb/3308/mysqld start

[root@centos7 3306]#ss -ntl //查看端口
LISTEN  0   50  *:3307  *:*                  
LISTEN  0    50    *:3308  *:*                  
LISTEN  0    50    *:3306  *:*                  

第五步:连接

代码语言:javascript
复制
[root@centos7 ~]#mysql //直接这样连接会出现错误 需要指定路径
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[root@centos7 ~]#mysql -S /mysqldb/3306/socket/mysql.sock //指定路径 这是没口令的连接
MariaDB [(none)]> show variables like "port"; //查看端口
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)
[root@centos7 ~]#mysql -S /mysqldb/3307/socket/mysql.sock -uroot -pcentos //这是有口令的连接 -u用户;-p密码

第六步:设置口令

代码语言:javascript
复制
命令:/mysqldb/3307/mysqld stop 关闭mysqldb/3307
第一种方法
[root@centos7]#mysqladmin -uroot -S /mysqldb/3308/socket//mysql.sock password 'centos' //添加口令
[root@centos7 3307]#/mysqldb/3308/mysqld stop 这里不输入口令停止不了
Stoping MySQL...
Enter password: 
/usr/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'
[root@centos7 3307]#/mysqldb/3308/mysqld stop 输入正确口令成功
Stoping MySQL...
Enter password: 

第二种方法
[root@centos7 ~]#mysql -S /mysqldb/3307/socket/mysql.sock
MariaDB [(none)]> select user,password,host from mysql.user; 查看表内容
+------+----------+-------------------+
| user | password | host              |
+------+----------+-------------------+
| root |          | localhost         |
| root |          | centos7.qifei.com |
| root |          | 127.0.0.1         |
| root |          | ::1               |
|      |          | localhost         |
|      |          | centos7.qifei.com |
+------+----------+-------------------+
insert(插入) update(修改)delet(删除)
设置密码
MariaDB [(none)]> update mysql.user set password=password("centos1") where user='root'; //注意这是改的所有
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4  Changed: 4  Warnings: 0

MariaDB [(none)]> select user,password,host from mysql.user;                          
+------+-------------------------------------------+-------------------+
| user | password                                  | host              |
+------+-------------------------------------------+-------------------+
| root | *07012D77331829FBC7415FCFE0041354CE238D41 | localhost         |
| root | *07012D77331829FBC7415FCFE0041354CE238D41 | centos7.qifei.com |
| root | *07012D77331829FBC7415FCFE0041354CE238D41 | 127.0.0.1         |
| root | *07012D77331829FBC7415FCFE0041354CE238D41 | ::1               |
|      |                                           | localhost         |
|      |                                           | centos7.qifei.com |
+------+-------------------------------------------+-------------------+

删除匿名账户
MariaDB [(none)]> drop user ''@localhost;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> drop user ''@centos7.qifei.com;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> drop user 'root'@centos7.qifei.com;       
Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> drop user 'root'@'::1';  
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select user,password,host from mysql.user;
MariaDB [(none)]> delete from mysql.user where host="centos7.qifei.com";   这样也可以删除
Query OK, 0 rows affected (0.00 sec)
+------+-------------------------------------------+-------------------+
| user | password                                  | host              |
+------+-------------------------------------------+-------------------+
| root | *07012D77331829FBC7415FCFE0041354CE238D41 | localhost         |
| root | *07012D77331829FBC7415FCFE0041354CE238D41 | 127.0.0.1         |
+------+-------------------------------------------+-------------------+

修改完刷新生效
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

命令

代码语言:javascript
复制
MariaDB [(none)]> show databases; 数据库列表
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/01/23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档