CentOS 7安装MySQL 8.0

tengxunyun cera

这MySQL的默认端口是3306。

Linux上面安装的方式主要分为如下三种:

YUM
二进制包
源码包
源码包的安装过于复杂,但是可以灵活配置,这里我们就不介绍了,主要介绍yum和二进制包的形式。

系统环境
$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
$ uname -a
Linux host.localdomain 3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
YUM
添加YUM源
$ rpm -ivh https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
mysql repo的下载地址:https://dev.mysql.com/downloads/repo/yum/

安装
$ yum install mysql-community-server -y
启动并设置开机自启动
$ systemctl enable –now mysqld
查看启动状态
$ systemctl status mysqld
● mysqld.service – MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 一 2018-12-17 08:47:04 EST; 15min ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 11986 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 12055 (mysqld)
Status: “SERVER_OPERATING”
CGroup: /system.slice/mysqld.service
└─12055 /usr/sbin/mysqld

12月 17 08:46:55 host.localdomain systemd[1]: Starting MySQL Server…
12月 17 08:47:04 host.localdomain systemd[1]: Started MySQL Server.
登录
MySQL在第一次启动的时候,会随机生成一个密码放在/var/log/mysqld.log日志文件中,我们可以通过grep命令获取到root的密码

$ grep ‘temporary password’ /var/log/mysqld.log
2018-06-12T09:34:07.463168Z 1 [Note] A temporary password is generated for [email protected]: WiwB.e2c3udA
其中WiwB.e2c3udA就是root用户的密码,下面我们登录MySQL

$ mysql -u root -h 127.0.0.1 -p
Enter password: # 输入密码
mysql>
查看数据库

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
这里会提示我们必须修改root密码才可以正常使用,我们把密码修改为D*gYmF5*

# 修改当前用户的密码
mysql> set password=password(‘D*gYmF5*’);
Query OK, 0 rows affected, 1 warning (0.00 sec)
# 刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.06 sec)
然后退出重新登录MySQL

mysql> exit
Bye
$ mysql -u root -h 127.0.0.1 -p
Enter password: # 输入刚才设置的密码
#
查看有多少库
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| sys |
+——————–+
4 rows in set (0.01 sec)
# 查看有多少用户
mysql> select user,host from mysql.user;
+—————+———–+
| user | host |
+—————+———–+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+—————+———–+
3 rows in set (0.00 sec)
二进制包
在安装之前,我们需要安装依赖库,因为MySQL需要

$ yum install libaio libaio-devel numactl numactl-devel -y
最好也把开发工具包安装上

$ yum groupinstall “Development Tools” -y
卸载mariadb
$ yum remove mariadb mariadb-libs -y
创建MySQL用户和组
$ groupadd mysql
$ useradd -r -g mysql -s /bin/false mysql
下载MySQL包
$ cd /opt/
$ wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.13-linux-glibc2.12-x86_64.tar
获取不同版本的下载地址:https://dev.mysql.com/downloads/mysql/

解压并创建软链
$ tar -xvf mysql-8.0.13-linux-glibc2.12-x86_64.tar
$ tar xvf mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz
$ ln -s /opt/mysql-8.0.13-linux-glibc2.12-x86_64 /usr/local/mysql
创建数据文件存放目录
$ mkdir /usr/local/mysql/data
授权
$ chown -R mysql.mysql /usr/local/mysql
$ ls -ld /usr/local/mysql
lrwxrwxrwx 1 mysql mysql 40 12月 17 09:31 /usr/local/mysql -> /opt/mysql-8.0.13-linux-glibc2.12-x86_64
添加环境变量
$ echo ‘export PATH=$PATH:/usr/local/mysql/bin’ >> ~/.bash_profile
$ source ~/.bash_profile
验证是否添加成功

$ mysql –version
mysql Ver 8.0.13 for linux-glibc2.12 on x86_64 (MySQL Community Server – GPL)
初始化数据库
$ mysqld –initialize –user=mysql –basedir=/usr/local/mysql –datadir=/usr/local/mysql/data
2018-12-17T14:32:55.725037Z 0 [System] [MY-013169] [Server] /opt/mysql-8.0.13-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.13) initializing of server in progress as process 23954
2018-12-17T14:32:59.438919Z 5 [Note] [MY-010454] [Server] A temporary password is generated for [email protected]: fTVb-*WAq1Oo
2018-12-17T14:33:02.335171Z 0 [System] [MY-013170] [Server] /opt/mysql-8.0.13-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.13) initializing of server has completed
记住倒数第二条的信息,告诉你了root的密码是fTVb-*WAq1Oo

生成SSL
$ mysql_ssl_rsa_setup –basedir=/usr/local/mysql –datadir=/usr/local/mysql/data/
创建my.cnf参数文件
$ vim /etc/my.cnf
[mysqld]
port = 3306
user = mysql
socket = /usr/local/mysql/data/mysqld.sock
pid-file = /usr/local/mysql/data/mysqld.pid
basedir = /usr/local/mysql/
datadir = /usr/local/mysql/data/
设置启动项
$ vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql

Type=forking

PIDFile=/usr/local/mysql/data/mysqld.pid

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Execute pre and post scripts as root
PermissionsStartOnly=true

# Start main service
ExecStart=/usr/local/mysql/bin/mysqld –daemonize –pid-file=/usr/local/mysql/data/mysqld.pid $MYSQLD_OPTS

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 5000

Restart=on-failure

RestartPreventExitStatus=1

PrivateTmp=false
重新加载systemd脚本

$ systemctl daemon-reload
启动并添加开机自启动

$ systemctl enable –now –now mysqld
查看运行状态

$ systemctl status mysqld
● mysqld.service – MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 一 2018-12-17 09:35:24 EST; 4s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 24042 ExecStart=/usr/local/mysql/bin/mysqld –daemonize –pid-file=/usr/local/mysql/data/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Main PID: 24044 (mysqld)
CGroup: /system.slice/mysqld.service
└─24044 /usr/local/mysql/bin/mysqld –daemonize –pid-file=/usr/local/mysql/data/mysqld.pid

12月 17 09:35:22 host.localdomain systemd[1]: Starting MySQL Server…
12月 17 09:35:23 host.localdomain mysqld[24042]: 2018-12-17T14:35:22.445672Z 0 [Warning] [MY-010139] [Server] Changed limits: max_open_files: 5000 (requested 8161)
12月 17 09:35:23 host.localdomain mysqld[24042]: 2018-12-17T14:35:22.445893Z 0 [Warning] [MY-010142] [Server] Changed limits: table_open_cache: 2419 (requested 4000)
12月 17 09:35:23 host.localdomain mysqld[24042]: 2018-12-17T14:35:23.315272Z 0 [System] [MY-010116] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.13) starting as process 24042
12月 17 09:35:24 host.localdomain mysqld[24042]: 2018-12-17T14:35:24.377008Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
12月 17 09:35:24 host.localdomain mysqld[24042]: 2018-12-17T14:35:24.414863Z 0 [System] [MY-010931] [Server] /usr/local/mysql/bin/mysqld: ready for connections. Version: ‘8.0.13’ socket: ‘/usr… Server – GPL.
12月 17 09:35:24 host.localdomain systemd[1]: Started MySQL Server.
Hint: Some lines were ellipsized, use -l to show in full.
连接并修改密码
$ mysql -uroot -h 127.0.0.1 -p
Enter password: # 输入密码

mysql> ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘MyNewPass4!’; # 修改密码为MyNewPass4!
Query OK, 0 rows affected (0.03 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.06 sec)

mysql> exit
Bye
连接测试

$ mysql -uroot -h 127.0.0.1 -p
Enter password: # 输入刚才设置的密码
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| sys |
+——————–+
4 rows in set (0.00 sec)

mysql> select user,host from mysql.user;
+—————+———–+
| user | host |
+—————+———–+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+—————+———–+
3 rows in set (0.00 sec)
Docker
如果你的环境比较复杂,搭建起来较为麻烦,没关系,我们可以用docker的方式一键启动,而且还是跨平台的。

运行MySQL
$ docker run -d –name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
上面的指令中,我们运行了8.x的mysql版本,把3306映射出来了,这样可以通过127.0.0.1:3306进行访问,其次设置了root密码为123456。

cera cloudiplc

相关推荐

CentOS 7安装指定内核 并启动BBR加速

前几天全新安装了BT面板, 但是发现BT的系统监控记录在最新的4.19.0-1.el7.elrepo.x86_64内核上不起作用, 今天重装了一下系统, 把CentOS 7的内核升级到4.18.16-1.el7.elrepo.x86_64, 顺便记录一下.   查看现在运行的内核 uname -r 显示 3.10.0-862.14.4.el7.x86_64 先下载内核 http://mirror.rc.usf.edu/compute_lock/elrepo/kernel/el7/x86_64/RPMS/ 下载地址好多, 我是顺便找的一个 wget http://mirror.rc.usf.edu/compute_lock/elrepo/kernel/el7/x86_64/RPMS/kernel-ml-4.18.16-1.el7.elrepo.x86_64.rpm wget http://mirror.rc.usf.edu/compute_lock/elrepo/kernel/el7/x86_64/RPMS/kernel-ml-devel-4.18.16-1.el7.elrepo.x86_64.rpm 安装 yum install kernel-ml-4.18.16-1.el7.elrepo.x86_64.rpm -y yum install kernel-ml-devel-4.18.16-1.el7.elrepo.x86_64.rpm -y 查看一下内核启动顺序 awk -F\’ ‘$1==”menuentry ” {print $2}’ /etc/grub2.cfg 显示如下 CentOS Linux (4.18.16-1.el7.elrepo.x86_64) 7 (Core) CentOS Linux (3.10.0-862.14.4.el7.x86_64) 7 (Core) CentOS Linux (3.10.0-693.21.1.el7.x86_64) 7 (Core) CentOS Linux …

linux系统之CentOS 7系列重启|查看|自动启动SSH等操作命令

CentOs 重启ssh服务的命令如下: # service sshd restart 不过上面的这个命令在于7以及以上的系统已经不再适用,在阿里云等云服务器中,CentOS 7 下重启服务不再通过 service 操作,而是通过 systemctl 操作,具体的操作命令如下: 查看状态: systemctl status sshd.service 启动服务: systemctl start sshd.service 重启服务: systemctl restart sshd.service 开机自启: systemctl enable sshd.service

宝塔面板云服务器如何安装帝国CMS

最近在邻居那看到一个#宝塔#面板安装#帝国CMS#的#教程#。说了一大堆废话。今天简单介绍下云服务器如何部署帝国CMS。目前最新版宝塔7.2.0已经包含一键安装帝国CMS的脚本。 帝国CMS 7.5一键脚本安装需求。 nginx,安装1.17或者以上都可以 MySQL 5.5版本以上即可,上不封顶。 PHP,一键部署需要7.2或者以下版本,等部署完成后可以切换成7.3或者7.4。都可以正常运行。 帝国CMS宝塔面板安装方法,安装好上面的安装需求软件,然后在宝塔面板 – 软件商店 – 一键部署 – 帝国CMS 安装过程中,只需要填入域名即可。数据库会自动部署,有需要的话记得记录数据库用户名以及密码。提交后即可自动安装。完成后记得进入设置下,就完成安装了。

宝塔面板安装卡在Connecting to download.bt.cn解决方法

有童鞋在安装宝塔面板的时候,遇到一只卡在Connecting to download.bt.cn,最初以为是机器没解析到这个域名,在机器内ping域名测试,一点毛病没有!机器DNS也改来改去,不见改变,甚至怀疑防火墙阻止了端口,直接关闭了防火墙,结果还是一样,一直就是类似于:   Resolving download.bt.cn (download.bt.cn)… 125.88.182.172 Connecting to download.bt.cn (download.bt.cn)|125.88.182.172|:80… failed: Connection timed out. Retrying. bt1 上面是关了防火墙,直接下载也是这样,最后通过修改hosts,将域名download.bt.cn固定在美国节点IP128.1.164.196后正常。 vi /etc/hosts 加入如下后保存: 128.1.164.196 download.bt.cn 之后再试,面板和组件均已正常可安装了。 bt2 这个问题比较变态,机器和对方源服务器都没问题,但是两者连接有问题,只能换源解决,更换到能用的源即可。

微信扫一扫,分享到朋友圈

CentOS 7安装MySQL 8.0