CentOS安装mysql
不要直接安装mysql,它只是个客户端
1.下载mysql的repo源
如果没有 wget 先yum install -y wget
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
2.安装mysql-community-release-el7-5.noarch.rpm包
非 root 用户 加上 sudo
rpm -ivh mysql-community-release-el7-5.noarch.rpm
3.安装mysql服务
yum install -y mysql-server
4.启动mysql服务
service mysqld start
5.设置开机启动mysql服务
chkconfig mysqld on
空密码进入mysql
mysql> show databases;
mysql> use mysql;
mysql> show tables;
mysql> show user;
mysql> select user, host, password from user;
+------+-------------------+----------+
| user | host | password |
+------+-------------------+----------+
| root | localhost | |
| root | vm\_0\_13\_redhat | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | vm\_0\_13\_redhat | |
+------+-------------------+----------+
6 rows in set (0.00 sec)
修改 mysql 密码
mysql> UPDATE mysql.user SET password=PASSWORD("123456") WHERE user="root";
设置登录权限,远程服务器可访问
mysql> delete from user;
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
mysql> flush privileges;
以后密码登录
mysql -uroot -p123456
Mac上docker部署mysql服务
获取mysql官方最新docker镜像
$ docker pull mysql/mysql-server:latest
网易加速源:docker pull hub.c.163.com/library/mysql:5.7
下载后查看镜像:docker images
创建并启动mysql服务容器
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql/mysql-server
参数说明
--name 给容器起别名,可选,不指定,则自动生成不规则的字符串
-p 3306:3306 将本机3306 端口映射到docker 的3306端口
-v ~/mysql/data:/var/lib/mysql 是将本机的/mysql/data目录映射到docker的/var/lib/mysql 文件夹
自动创建~/mysql/data,用于存放容器的mysql数据库文件
-e MYSQL_ROOT_PASSWORD=123456 初始化密码为123456
-d 确定唯一镜像,这里只有一个mysql也有必要要tag, 仓库名:标签
# 可以重新运行,重新起个--name
# 不用在mac上持久化
-v ~/mysql/data:/var/lib/mysql
-v ~/mysql/logs:/logs 将本机下的logs目录挂载到容器的/logs
-v ~/mysql/conf:/etc/mysql/conf.d
下次用docker启动
docker start mysql
连接mysql数据库
# 进入mysql容器的bash终端
docker exec -it mysql bash
bash-4.2# mysql -uroot -p123456
mysql>
Mac本地连接mysql
必须先设置mysql 密码
mysql> use mysql;
mysql> update user set host="%" where user="root";
mysql> grant all PRIVILEGES on *.* to root@'%' WITH GRANT OPTION;
mysql> flush privileges;
终端链接mysql,类似远程连接
# Mac上仅安装mysql client客户端
brew install mysql --client-only --universal
# 尝试连接
mysql -h127.0.0.1 -uroot -p123456
停止mysql服务:
docker stop mysql
mysql5.6以后的版本修改了加密算法,这里需要更新密码算法,操作步骤如下
mysql> ALTER user 'root'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.09 sec)
mysql>
mysql> ALTER user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
参考资料
MAC上用Navicat连接本机Docker的Mysql8.0 - Fatal Error - CSDN博客
Docker 安装 MySQL | 菜鸟教程
Mac下的docker安装以及docker中mysql的安装使用 | Batman's Wayne House
在 Docker 中使用 mysql | 破船之家
Centos7.2安装Mysql5.7(阿里云) - 简书