1. 下载mysql
社区版(MySQL Community Server (GPL))下载地址 http://dev.mysql.com/downloads/mysql/
此处下载了.tar.gz格式的文件,当然也可以下载别的格式,各个格式只是对应的解压方式不同。
拓展:
.tar是将文件打包,文件的大小没什么变化,一般用tar -cvf filename.tar filename格式来压缩文件,用 tar -xvf filename.tar 来解压文件。
.tar.gz是加入了gzip的压缩命令,会将文件压缩存放,可以有效压缩文件的大小,以便于缩短传输时间或者释放磁盘空间,一般用tar -czvf filename.tar.gz filename来压缩文件,用 tar -xzvf filename.tar.gz 来解压文件。
命令方式下载:
[root@VM_112_250_centos ~]# cd /usr/local/src 这里把压缩包下载到了 /usr/local/src目录下 [root@VM_112_250_centos src]# wget -c http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz
2. 检查mysql组和用户是否存在,如无创建
[root@VM_112_250_centos src]# cat /etc/group | grep mysql mysql:x:1000: [root@VM_112_250_centos src]# cat /etc/passwd | grep mysql mysql:x:996:1000::/home/mysql:/bin/bash 以上为存在的情况,如无,执行添加命令: [root@VM_112_250_centos src]# groupadd mysql [root@VM_112_250_centos src]# useradd -r -g mysql mysql //useradd -r参数表示mysql用户是系统用户,不可用于登录系统
3. 解压,创建mysql目录,并分配用户和组
解压 [root@VM_112_250_centos src]# tar -xzvf mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz 创建mysql目录 [root@VM_112_250_centos src]# mkdir /usr/local/mysql 创建mysql数据目录 [root@VM_112_250_centos src]# mkdir /usr/local/mysql/data 把解压后的所有文件移动到/usr/local/mysql目录下 [root@VM_112_250_centos src]# mv mysql-5.7.16-linux-glibc2.5-x86_64/* /usr/local/mysql 切换到mysql目录,然后就可以看到mysql目录下就有了这些内容 [root@VM_112_250_centos mysql]# dir COPYING README bin data docs include lib man share support-files 更改mysql目录所属的用户(用户为mysql) [root@VM_112_250_centos mysql]# chown -R mysql ../mysql/ //这里已经切到mysql目录了,所以添加了../ 更改mysql目录所属组(组为mysql) [root@VM_112_250_centos mysql]# chgrp -R mysql ../mysql/ #-R是递归的意思,就是把mysql目录下的全部文件和子目录都设置为mysql用户和mysql组。 #上面的做法是为了把mysql降权,以限定只能访问属于mysql用户的文件。
4. 安装及初始化数据库(创建系统数据库的表)
[root@VM_112_250_centos mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ 2016-11-17 13:07:49 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize 2016-11-17 13:08:10 [WARNING] The bootstrap log isn't empty: 2016-11-17 13:08:10 [WARNING] 2016-11-17T05:07:53.787853Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
5. 配置mysql数据库
复制配置文件 [root@VM_112_250_centos mysql]# cp -a ./support-files/my-default.cnf /etc/my.cnf [root@VM_112_250_centos mysql]# cp -a ./support-files/mysql.server /etc/init.d/mysqld 更改配置文件信息 使用命令 vim /etc/my.cnf 打开my.cnf文件 然后按键盘I按键进入VIM的插入模式进行编辑 # These are commonly set, remove the # and set as required. basedir = /usr/local/mysql datadir = /usr/local/mysql/data port = 3306 之后按ESC按键退出,:w进行保存,:q退出vim即可
加入环境变量,可以参考 linux中添加环境变量的方法
[root@VM_112_250_centos ~]# echo “PATH=/usr/local/mysql/bin:$PATH” >>/etc/profile
设置开机启动,可以参考 linux下配置mysql开机自启动
启动试一下:
[root@VM_112_250_centos init.d]# /etc/init.d/mysqld restart
ERROR! MySQL server PID file could not be found! —-因为还没启动所以会抛这个错误
Starting MySQL. SUCCESS!
// mysql的日志文件在/usr/local/mysql/data/foo.err文件中(foo为别名)
6. 初始化密码
mysql5.7会生成一个初始化密码,而在之前的版本首次登陆不需要密码。
[root@VM_112_250_centos bin]# ./mysql -uroot -p Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 所以这里需要初始化密码 [root@VM_112_250_centos bin]# cat /root/.mysql_secret # Password set for user 'root@localhost' at 2016-11-17 13:07:49 Roa%ZwziC%yS 然后进行登陆 [root@VM_112_250_centos bin]# ./mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.16 Copyright (c) 2000, 2016, 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.
本文固定链接:心知博客 » linux下安装mysql详细过程
本站内容除特别标注外均为原创,欢迎转载,但请保留出处!