Linux 或 Mac 下安装、配置 mariadb 或 mysql
Linux 下的安装配置,以下用 ubuntu 举例说明:
一、安装
- Linux
sudo apt-get install mariadb
- Mac
brew install mariadb
二、管理服务
- Linux
systemcl
或 service
- 启动
systemctl start mysqld
- 停止
systemctl stop mysqld
- 重启
systemctl restart mysqld
- Mac
homebrew
- 启动
brew services start mariadb
- 停止
brew services stop mariadb
- 重启
brew services restart mariadb
三、初始化配置
如果要用可视化界面远程连接 mysql,需新建一个用户并 grant 授权
首次安装后使用匿名用户登录,设置 root 密码
- 登录
mariadb
或
mysql
- 设置 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;
四、开启命令行自动补全和语法高亮
-
安装
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
- 使用
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;