Linux 或 Mac 下安装、配置 mariadb 或 mysql

1 篇文章 0 订阅

在这里插入图片描述

Linux 或 Mac 下安装、配置 mariadb 或 mysql

Linux 下的安装配置,以下用 ubuntu 举例说明:

一、安装

  • Linux
sudo apt-get install mariadb
  • Mac
brew install mariadb

二、管理服务

  1. Linux

systemclservice

  • 启动
systemctl start mysqld
  • 停止
systemctl stop mysqld
  • 重启
systemctl restart mysqld
  1. Mac

homebrew

  • 启动
brew services start mariadb
  • 停止
brew services stop mariadb
  • 重启
brew services restart mariadb

三、初始化配置

如果要用可视化界面远程连接 mysql,需新建一个用户并 grant 授权

首次安装后使用匿名用户登录,设置 root 密码

  1. 登录
mariadb

mysql
  1. 设置 root 用户密码
SET Password FOR root@localhost = password('123');
FLUSH PRIVILEGES;

首次安装后初始化配置

mysql_secure_installation

查询数据库登录用户

SELECT user,authentication_string,plugin,host,password FROM mysql.user;

新创建一个登录用户

create user admin@localhost identified by 'admin_password';

授权用户可远程连接、登录

格式: grant all privileges on {database}.{table} to {username}@{host} identified by {password} [with grant option];

grant all privileges on *.* to admin@'%' identified by 'admin';
flush privileges;
  • 第一个 * 表示所有数据库,也可指定数据库名
  • 第二个 * 表示所有数据表,也可指定数据表名
  • % 表示所有远程主机 ip,也可指定主机 ip

带授权权限的权限配置

grant all privileges on *.* to admin@'%' identified by 'admin' with grant option;

授权本地用户连接

grant all privileges on *.* to admin@localhost identified by 'admin';
flush privileges;

四、开启命令行自动补全和语法高亮

  1. 安装 MyCLI

    MyCLI 是一个易于使用的命令行客户端,可用于受欢迎的数据库管理系统 MySQL、MariaDB 和 Percona,支持自动补全和语法高亮

  • Mac 系统
sudo brew install mycli
  • Debian 系列 linux 发行版(Ubuntu)
sudo apt-get install mycli
  • 红帽系列 linux 发行版 (RHEL、CentOS)
sudo yum install mycli
  • 安装 pip 后
sudo pip install mycli
  1. 使用
mycli -uadmin -h localhost;

五、Q&A

1. issue: ERROR 1396 (HY000): Operation CREATE USER failed for ‘jack’@‘localhost’

无法创建用户,原因是该用户被删除后存在缓存,需要刷新权限

drop user admin@localhost;
flush privileges;
create user admin@localhost identified by 'admin_password';

2. 内置用户数据库被误操作,导致登录用户不能正常访问数据库

删除原来内置的数据库文件,重新安装 mariadb,
Removed /opt/homebrew/var/mysql/ folder and reinstalled mariadb

3. ERROR 1356 (HY000): View ‘mysql.user’ references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them

mysql.user 是个 View。那就换成其他方式吧。

SET Password FOR root@localhost = password('123');
FLUSH PRIVILEGES;

参考资料

  1. homebrew 安装
  2. 程序员 Homebrew 使用指北
  3. 在 MySQL 中启用自动补全
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值