安装前奏
1:去官网下载 5.7或以上版本的mysql tar.gz包
下载地址:
2:使用root在Linux下创建 mysql用户及用户组
2.1:创建组 [root@localhost]# groupadd mysql 2.2: 创建用户并加入到组中 [root@localhost]# useradd -r -g mysql mysql 2.3: 禁止mysql用户登录系统(可选 防止别人使用mysql用户登录到主机) [root@localhost]# usermod -s /bin/false mysql复制代码
3: 将下载好的包放入到 /opt/mysql/ 下(目录按自己的喜好存储)
3.1:解压文件 [root@localhost]# tar -xvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz3.2: 修改解压好的目录命名 [root@localhost]# mv mysql-5.7.21-linux-glibc2.12-x86_64 mysql-5.7.21 3.3: 更改目录所属的用户和组 [root@localhost]# chown -R mysql mysql/ [root@localhost]# chgrp -R mysql mysql/复制代码
开始安装Mysql
- 进入到 /opt/mysql/mysql-5.7.21/support-files 目录下,查看是否有 my_default.cnf 如果没有执行1.1的步骤:
1.1. 在support-files目录下创建 my_default.cnf 文件
[root@localhost]# mkdir my_default.cnf复制代码
1.2. 将以下内容复制到文件中
# For advice on how to change settings please see# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the# *** default location during install, and will be replaced if you# *** upgrade to a newer version of MySQL.[client]port = 3306socket = /opt/mysql/mysql.sock[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# These are commonly set, remove the # and set as required.# basedir = .....# datadir = .....# port = .....# server_id = .....# socket = .....#skip-grant-tables #跳过登录验证#innodb_force_recovery=1basedir = /opt/mysql/mysql-5.7.21/datadir = /opt/mysql/data/port = 3306server_id = 1socket = /opt/mysql/mysql.socklanguage = /opt/mysql/mysql-5.7.21/share/english#log-bin = /opt/mysql/mysql-5.7.21/mysql-binexpire_logs_days = 10max_binlog_size = 100M#general_log = 1#general_log_file = /opt/mysql/mysql-5.7.21/logs/query.logslow_query_log = 1slow_query_log_file = /opt/mysql/mysql-5.7.21/logs/slow-query.loglong-query_time = 2log-error = /opt/mysql/mysql-5.7.21/logs/error.log# 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 = 2Msql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES复制代码
- 将该文件移到/etc/目录下并改名
1: mysql 启动默认读取/etc/my.cnf配置文件,使用命令将文件挪到该目录[root@localhost support-files]# mv my_default.cnf /etc/my.cnf复制代码
- 将/opt/mysql/mysql-5.7.21/supprot-files/mysql.server 移动到 /etc/init.d/
[root@localhost]# mv /opt/mysql/mysql-5.7.21/supprot-files/mysql.server /etc/init.d/mysqld复制代码
4.创建mysql的数据存储目录以及日志目录
1:创建data目录[root@localhost]# mkdir /opt/mysql/data2: 创建日志目录及日志文件,如果不创建启动会报错[root@localhost]# mkdir /opt/mysql/mysql-5.7.21/logs[root@localhost]# touch /opt/mysql/mysql-5.7.21/logs/error.log复制代码
- 配置 my.cnf 文件
1:修改 my.cnf[root@localhost etc]# vi /etc/my.cnf basedir = /opt/mysql/mysql-5.7.21(mysql安装目录) datadir = /opt/mysql/data (上一步创建的data路径) socket = /opt/mysql/mysql.sock (该文件会自动创建在你指定的位置) log*的配置:只需要改一些log存放的路径,上一步已经创建了logs目录,把所以关于log的路径都指向 /opt/mysql/mysql-5.7.21/logs目录复制代码
- 一切就绪准备安装服务
1:进入到Mysql bin目录下执行命令[root@localhost bin]# ./mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql/mysql-5.7.21/ --datadir=/opt/mysql/data/如果上不没有ERROR 就表示安装成功复制代码
7:启动服务
进入到 /etc/init.d/下执行启动命令: [root@localhost init.d] ./mysqld start
执行完毕看到 Mysql Start SUCCESS 表示成功
8:连接到mysql服务端
1. 进入到mysql bin目录下执行命令[root@localhost bin] ./mysql -uroot -p输入密码即可连接复制代码
9: 忘记了root密码
1. 先停止已启动服务[root@localhost init.d] ./mysqld stop2. 修改my.cnf文件[root@localhost etc] vi my.cnf 加入一行:skip-grant-tables #跳过登录验证3. 重启mysql服务 ./mysqld start4. bin目录下连接mysql : ./mysql5. mysql登录上后修改root密码 ,因为5.7以上版本没有了password字段,执行一下sql修改密码:mysql> update mysql.user set authentication_string=password('root1234') where user='root' and Host = 'localhost';mysql> flush privileges;mysql> exit;6:将my.cnf加上的跳过验证删除,然后在重启mysql服务。至此,mysql安装完成复制代码