- 在刚入社会还是职场小白期间,总是喜欢不断接触各种新鲜的技术,给自己一种什么都会的感觉,但也是这种年轻的想法,反而显得自己很无知
- 以前在不懂什么问题之后,马上去baidu,然后找到一篇好的帖子收藏,并按部就班的学会作者的套路,还会把baidu的能力当作自己的学习能力😊baidu大法固然好,但其实也在扼杀自己学习了解新事物的能力,这样的弯路可能也是必经之路,不一概而论
- 总之在想一种高效又可以快速获取知识的办法,我们走的弯路也实际是自己投机取巧的路。不多扯...
先奉上大法宝
没错,今天是围绕官网的学习(不是baidu)
作为一名程序员,强烈建议围绕linux去学习,Windows的同学只能说下抱歉🙏
本文只介绍最常用的一直安装方式yum(对于支持dnf,可将以下命令替换为dnf),linux肯定有此工具
完全安装MySQL的步骤
1. 添加MySQL Yum存储库
可以参考官网存储库https://dev.mysql.com/downloads/repo/yum/
- 检查linux系统
[root@maxiaoyun ~]# uname -a
# 会发现系统基于EL7
# EL7是Red Hat 7.x,CentOS 7.x和CloudLinux 7.x的下载(ELx同理)
[root@maxiaoyun ~]# Linux maxiaoyun 3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
- 安装rpm
[root@maxiaoyun ~]# wget http://repo.mysql.com/mysql80-community-release-el7-2.noarch.rpm
[root@maxiaoyun ~]# ls
[root@maxiaoyun ~]# mysql80-community-release-el7-2.noarch.rpm
[root@maxiaoyun ~]# yum localinstall mysql80-community-release-el7-2.noarch.rpm
# 执行完之后会发现,/etc/yum.repos.d/ 目录下多了两个文件
[root@maxiaoyun yum.repos.d]# mysql-community-source.repo mysql-community.repo
- 查看repolist
[root@maxiaoyun ~]# yum repolist all | grep mysql
# mysql版本(enabled表示启用,咱们安装的为80版本,所以80相关为enabled状态)
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - disabled
mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 95
mysql-connectors-community-source MySQL Connectors Community - S disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 84
mysql-tools-community-source MySQL Tools Community - Source disabled
mysql-tools-preview/x86_64 MySQL Tools Preview disabled
mysql-tools-preview-source MySQL Tools Preview - Source disabled
mysql55-community/x86_64 MySQL 5.5 Community Server disabled
mysql55-community-source MySQL 5.5 Community Server - S disabled
mysql56-community/x86_64 MySQL 5.6 Community Server disabled
mysql56-community-source MySQL 5.6 Community Server - S disabled
mysql57-community/x86_64 MySQL 5.7 Community Server disabled
mysql57-community-source MySQL 5.7 Community Server - S disabled
mysql80-community/x86_64 MySQL 8.0 Community Server enabled: 82
mysql80-community-source MySQL 8.0 Community Server - S disabled
# 也可以手动启用、禁用
[root@maxiaoyun ~]# yum-config-manager --disable mysql57-community
[root@maxiaoyun ~]# yum-config-manager --enable mysql80-community
# dnf(作用一样)
[root@maxiaoyun ~]# dnf config-manager --disable mysql57-community
[root@maxiaoyun ~]# dnf config-manager --enable mysql80-community
- 也可以手动编辑/etc/yum.repos.d/mysql-community.repo
# Disable to use MySQL 5.7
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=0 //表示禁用
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
# Enable to use MySQL 8.0
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
- 检查repo
[root@maxiaoyun ~]# yum repolist enabled | grep mysql
# Enabled repo
mysql-connectors-community/x86_64 MySQL Connectors Community 95
mysql-tools-community/x86_64 MySQL Tools Community 84
mysql80-community/x86_64 MySQL 8.0 Community Server 82
2. 安装MySQL
[root@maxiaoyun ~]# yum install mysql-community-server
3. 启动MySQL服务器
[root@maxiaoyun ~]# service mysqld start
[root@maxiaoyun ~]# service mysqld status
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2019-04-17 09:47:18 CST; 1h 12min ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 31119 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 31192 (mysqld)
Status: "SERVER_OPERATING"
Tasks: 38
Memory: 490.9M
CGroup: /system.slice/mysqld.service
└─31192 /usr/sbin/mysqld
4月 17 09:46:57 maxiaoyun systemd[1]: Starting MySQL Server...
4月 17 09:47:18 maxiaoyun systemd[1]: Started MySQL Server.
如果遇到启动失败,一般为权限问题
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
一般情况为修改/var/lib/mysql
# 修改权限
[root@maxiaoyun ~]# chmod -R 777 /var/lib/mysql
# 删除目录下的文件
[root@maxiaoyun ~]# rm -rf /var/lib/mysql/*
# 重新启动
[root@maxiaoyun ~]# service mysqld restart
也可以确认一下文件位置,在my.cnf中查找,如果找不到my.cnf,可以使用下面方法
# 第一步
[root@maxiaoyun ~]# which mysqld
/usr/sbin/mysqld
# 第二步
[root@maxiaoyun ~]# /usr/sbin/mysqld --verbose --help |grep -A 1 'Default options'
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf // 其中/etc/my.cnf就为mysql配置文件
# 第三步
[root@maxiaoyun ~]# cat /etc/my.cnf
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# 修改其权限
[root@maxiaoyun ~]# chmod -R 777 /var/lib/mysql
4. Mysql连接
mysql8.0以后,初始化的root密码需要在日志中查看,mysql8.0以前root密码为空,可以直接登录
- 8.0查看初始密码方法
[root@maxiaoyun ~]# grep 'temporary password' /var/log/mysqld.log
# 展示如下,最后处就是密码
2019-04-17T01:47:13.751056Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: zRidoX(Y#8Aq
修改密码
# 先登录
[root@maxiaoyun ~]# mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';