前言
本人是学习JAVA快一年的小白,学习数据库时为了安装MariaDB折腾了整整一下午才成功,希望这篇总结可以帮到你
1. 安装Homebrew
1.1 What’s mac Homebrew ?
Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷
1.2 安装和更新
1.2.1 默认的url安装
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)
1.2.1.1 安装过程中的问题
因为中国有防火墙,所以访问外网被阻拦,所以无法获取github的安装包,从现象上看安装进程一直停止在下面的界面:
1.2.1.2 解决方案
使用镜像解决,就是访问一个镜像服务器 我在这里使用的是中科大的镜像。
1.2.3 中科镜像安装
1.2.3.1 安装URL
/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)
1.2.3.2 查看文件确认是否安装成功
-
第一个标志: Installation successfull 安装成功
-
第二个标志,homebrew-core 下有下面的文件
1.2.3.3 用命令检查是否安装成功
lxy@longcaicaideMacBook-Pro homebrew % brew help
1.2.4 检查Homebrew是否完全安装成功
brew doctor
lxy@longcaicaideMacBook-Pro homebrew-core % brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!
Warning: Suspicious https://github.com/Homebrew/brew git origin remote found.
The current git origin is:
https://mirrors.ustc.edu.cn/brew.git
With a non-standard origin, Homebrew won't update properly.
You can solve this by setting the origin remote:
git -C "/usr/local/Homebrew" remote set-url origin https://github.com/Homebrew/brew
Warning: Suspicious https://github.com/Homebrew/homebrew-core git origin remote found.
The current git origin is:
git://mirrors.ustc.edu.cn/homebrew-core.git
With a non-standard origin, Homebrew won't update properly.
You can solve this by setting the origin remote:
git -C "/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core" remote set-url origin https://github.com/Homebrew/homebrew-core
lxy@longcaicaideMacBook-Pro homebrew-core %
lxy@longcaicaideMacBook-Pro homebrew-core % git -C
no directory given for -C
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
1.2.5 更新Homebrew
lxy@longcaicaideMacBook-Pro homebrew-core % brew update
Already up-to-date.
2,安装和使用MariaDB
2.1 什么是MariaDB(3+3)
MariaDB由MySQL的原始开发人员制作,也是一种开源软件,著名的用户包括*,WordPress和谷歌,并且MariaDB Server是世界上最受欢迎的数据库服务器之一。
MariaDB将数据转换为各种应用程序中的结构化信息
- 是MySQL中的增强型替代品
- 它快速,可扩展且强大
- 具有丰富的存储引擎生态系统,插件和许多其他工具,可以适用各种例子。
MariaDB是作为开源软件开发的,作为关系数据库
- 它提供了一个用于访问数据的SQL接口
- MariaDB的最新版本还包括GIS和JSON功能
- MariaDB是MySQL的一个分支,因此MariaDB的数据库MySQL结构和索引相同,并且可以从MySQL切换到MariaDB而无需更改应用程序
2.2 确认MariaDB版本
lxy@longcaicaideMacBook-Pro homebrew-core % brew info mariadb
2.3 安装MariaDB
brew install mariadb
==> mariadb
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.
MySQL is configured to only allow connections from localhost by default
To have launchd start mariadb now and restart at login:
brew services start mariadb
Or, if you don't want/need a background service you can just run:
mysql.server start
lxy@longcaicaideMacBook-Pro homebrew-core %
2.3.1 安装命令不能执行在root用户下
sh-3.2# brew install mariadb
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.
sh-3.2# quit
sh: quit: command not found
sh-3.2# exit
exit
2.4 运行数据库安装程序
lxy@longcaicaideMacBook-Pro homebrew-core % mysql_install_db
mysql.user table already exists!
Run mysql_upgrade, not mysql_install_db
lxy@longcaicaideMacBook-Pro homebrew-core %
2.4.1 Mysql升级失败处理
lxy@longcaicaideMacBook-Pro homebrew-core % mysql_upgrade
Version check failed. Got the following error when calling the 'mysql' command line client
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
FATAL ERROR: Upgrade failed
问题:忘记root密码
lxy@longcaicaideMacBook-Pro homebrew-core % whoami
lxy
lxy@longcaicaideMacBook-Pro homebrew-core % su root
Password:
su: Sorry
解决方法:通过当前用户lxy执行sudo su命令,可是暂时登陆成功更改root密码
lxy@longcaicaideMacBook-Pro homebrew-core % sudo su(
sudo su : 这个命令,也是登录到了root,但是并没有切换root的环境变量,比如PATH。)
Password:
sh-3.2# passwd root
Changing password for root.
New password:
Retype new password:
sh-3.2# exit
exit
lxy@longcaicaideMacBook-Pro homebrew-core % su root
Password:
sh-3.2#
2.5 安装MariaDB成功
lxy@longcaicaideMacBook-Pro ~ % brew install mariadb
Warning: mariadb 10.5.8 is already installed and up-to-date
To reinstall 10.5.8, run `brew reinstall mariadb`
2.6 启动MariaDB
lxy@longcaicaideMacBook-Pro ~ % mysql.server start
Starting MariaDB
.201212 17:08:17 mysqld_safe Logging to '/usr/local/var/mysql/longcaicaideMacBook-Pro.local.err'.
201212 17:08:17 mysqld_safe Starting mariadbd daemon with databases from /usr/local/var/mysql
SUCCESS!
2.6.1 数据库重启
lxyu@longcaicaideMacBook-Pro ~ % mysql.server restart
2.6.2 数据库停止
lxy@longcaicaideMacBook-Pro ~ % mysql.server stop
2.6.3 查看进程
lxy@longcaicaideMacBook-Pro ~ % ps -axu lxy|grep mysql
501 14054 ?? 0:00.02 /bin/sh /usr/local/Cellar/mariadb/10.5.8/bin/mysqld_safe --datadir=/usr/local/var/mysql --pid-file=/usr/local/var/mysql/longcaicaideMacBook-Pro.local.pid
501 14121 ?? 0:00.94 /usr/local/Cellar/mariadb/10.5.8/bin/mariadbd --basedir=/usr/local/Cellar/mariadb/10.5.8 --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/Cellar/mariadb/10.5.8/lib/plugin --log-error=/usr/local/var/mysql/longcaicaideMacBook-Pro.local.err --pid-file=/usr/local/var/mysql/longcaicaideMacBook-Pro.local.pid
501 15454 ttys001 0:00.00 grep mysql
2.7 查看MariaDB版本
MariaDB [(none)]> select @@version;
+----------------+
| @@version |
+----------------+
| 10.5.8-MariaDB |
+----------------+
1 row in set (0.000 sec)
2.7.1 解决root密码无法登陆问题
问题现象
lxy@longcaicaideMacBook-Pro ~ % mysql -uroot - p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
解决方案:切换到root
lxy@longcaicaideMacBook-Pro ~ % su root
MariaDB [(none)]> select user,host,plugin from mysql.user;
+-------------+-------------------------------+-----------------------+
| User | Host | plugin |
+-------------+-------------------------------+-----------------------+
| mariadb.sys | localhost | mysql_native_password |
| root | localhost | mysql_native_password |
| lxy
| localhost | mysql_native_password |
| | localhost | |
| | longcaicaidemacbook-pro.local | |
+-------------+-------------------------------+-----------------------+
5 rows in set (0.001 sec)
MariaDB [(none)]> flush privileges; --- 刷新权限
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> set password for 'root'@'localhost' = password('root'); -- 通过系统root用户改数据库root用户的密码
Query OK, 0 rows affected (0.011 sec)
2.8 MariaDB基本命令
-- 显示数据库列表
show databases;
-- 切换到名为mysql的数据库,显示该库中的数据表
use mysql;
show tables;
-- 显示数据表table的结构
desc table;
-- 建数据库A与删数据库A
create database `database_A`;
drop database `database_A`;
-- 建表:
use database_A;
create table table_A(字段列表);
drop table table_A;
-- 显示表中的记录:
select * from table_A;
-- 清空表中记录:
delete from table_A;
3,数据库操作
3.1 查看Mysql进程
sh-3.2# ps -axu |grep mysql
0 103 ?? 0:00.03 /bin/sh /usr/local/mysql/bin/mysqld_safe --user=root
0 416 ?? 0:45.79 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/error.log --pid-file=longcaicaideMacBook-Pro.local.pid --port=3306
0 11361 ttys001 0:00.00 grep mysql
sh-3.2#
3.2 结束Mysql进程
前提是需要知道进程的PID号
将PID替换为相应的进程号
sudo kill PID号
举个例子:
sudo kill 2091
3.2.1 Mysql重新启动的问题
sh-3.2# kill -9 11676 (PID号为11676,-9是立即杀死,杀死后立即恢复启动了)
sh-3.2# ps -axu lxy|grep mysql
0 11503 ?? 0:00.04 /bin/sh /usr/local/mysql/bin/mysqld_safe --user=root
0 11714 ?? 0:02.99 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/error.log --pid-file=longcaicaideMacBook-Pro.local.pid --port=3306
0 11719 ttys001 0:00.00 grep mysql
sh-3.2#
3.3 停止Mysql进程
sh-3.2# sudo /usr/local/mysql/support-files/mysql.server stop
Shutting down MySQL
... SUCCESS!
sh-3.2#
3.3.1 关闭Mysql进程依然存在的方法
sh-3.2# cd /Library/LaunchDaemons/ ---- 输入 /Library/LaunchDaemons/ 查看你的.plist文件 的 文件位置
sh-3.2# ls
com.mysql.mysql.plist com.oray.sunlogin.plist
com.oracle.java.Helper-Tool.plist com.teamviewer.Helper.plist
com.oray.sunlogin.helper.plist com.teamviewer.teamviewer_service.plist
sh-3.2# sudo launchctl unload -w /Library/LaunchDaemons/com.mysql.mysql.plist
/Library/LaunchDaemons/com.mysql.mysql.plist: Operation now in progress
sh-3.2# ps -axu lxy|grep mysql
0 12873 ttys001 0:00.00 grep mysql
sh-3.2#
3.4 如何卸载Mysql
//打开终端窗口
//使用mysqldump备份你的数据库
//停止数据库服务器
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
edit /etc/hostconfig and remove the line MYSQLCOM=-YES-
rm -rf ~/Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm -rf /private/var/db/receipts/*mysql*