MySQL安装、卸载、修改密码
一、安装mysql
mysql版本介绍
GPL版本为开源,非商用;commercial为商用版
- MySQL Enterprise Edition
企业版,不开源,可商用
- MySQL Community (GPL)
社区版,开源,非商用
二、安装前的注意事项
MySQL对libaio
库有依赖性。如果未在本地安装此库,则数据目录初始化和随后的服务器启动步骤将失败。
在有yum的系统上安装libaio
依赖:
#从yum仓库中查询libaio的信息
yum search libaio
#从yum仓库安装libaio
yum install libaio
在有apt的系统上安装libaio
依赖:
# 查询libaio的信息
apt-cache search libaio
# 通过apt的方式获取并安装libaio
apt-get install libaio1
如果不安装依赖会在初始化mysql的时候出现错误码,如下:
./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
有些linux系统的安装镜像中会附带数据库,例如Mariadb这个数据库
如果有Mariadb这个数据库会影响安装mysql数据库
所以我们先检查以下是否安装有 Mariadb :
rpm -qa | grep mariadb
把他们全部卸载掉:
可以先卸载完整包(不带后缀的方式),然后再查询,再卸载其他包
#卸载完整包
yum -y remove mariadb
#查询
rpm -qa | grep mariadb
#卸载其他包
yum -y remove mariadb-libs-x.x.xx-x.exx.x86_64
名字后面的数字代表版本号(例如-5.5.56-2.el7.x86_64),我们可以通过tab键来快速补全名字,不用照着前面查询的内容全部手动输入。
最后最好再查询一遍来确保卸载干净
linux上安装mysql
通过yum源来安装要确保配置好yum仓库,没有设置的,可以看我的文章:CentOS7下更改yum源为国内源
获取方式
对于安装文件的获取:
- 可以通过命令行,从镜像文件仓库下载
- 如果有图形化界面,可以在图形化界面的浏览器中进入官网下载
- 通过文件传输,在windows上下载通过ftp(或sftp)进行文件传输
1. yum源安装
创建mysql用户和组
#创建MySQL组
groupadd mysql
#创建MySQL用户并添加到MySQL组中
useradd -g mysql mysql
如果你想创建的mysql用户不能用来登陆,那么使用下面的命令
useradd -r -g mysql -s /bin/false mysql
-r
表示创建系统用户
-s
表示更改默认登陆的shell 参数/bin/false
表示,更改后的shell为/bin/false
,mysql用户不能用于登陆
获取
通过wget获取rpm文件信息来安装mysql(这里获取的是最新的rpm)
当然可以直接从官网下载下来然后通过ftp(sftp)传到指定的linux系统上
wget -i http://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm
rpm列表可以通过官网查看https://dev.mysql.com/downloads/repo/yum/
括号里的内容为rpm版本,找到你想要的版本,将括号里的名字替换掉http://dev.mysql.com/get/
后面的内容就可以通过wget获取。
mysql产品列表包含所有的产品,可以该页面下载rpm、tar格式的mysql
添加mysql yum库
yum -y install mysql80-community-release-el8-1.noarch.rpm
安装
yum -y install mysql-community-server
开启服务
service mysqld start
查看mysqld服务状态
service mysql status
样例输出
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running)
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 25325 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 25276 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 25329 (mysqld)
CGroup: /system.slice/mysqld.service
└─25329 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Active: active (running)
表示服务开启成功
查看初始密码
在日志里查看一下mysql初始密码
grep "password" /var/log/mysqld.log
样例输出:
[Note] A temporary password is generated for root@localhost: ju_Dfa*5gx
登陆
使用初始密码登陆
mysql -u root -p
样例输出:
Enter password:
输入初始密码,默认是隐藏显示,直接输入然后回车
样例输出:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql 的配置文件
通过yum安装会创建一个默认的配置文件/etc/my.cnf
默认配置如下:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
需要注意的是: