- 下载mysql安装包
cd /usr/local/
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
- 解压压缩包
tar -xzvf /data/software/mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.7.13-linux-glibc2.5-x86_64 mysql
- 创建用户组
groupadd mysql
useradd -r -g mysql mysql
chown -R mysql mysql/
chgrp -R mysql mysql/
- 修改my.cnf配置文件
vim /etc/my.cnf
#复制以下内容
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#不区分大小写
lower_case_table_names = 1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
max_connections=5000
default-time_zone = '+8:00'
:wq
- 初始化数据库
#先安装一下这个东东,要不然初始化有可能会报错
yum install libaio
#手动编辑一下日志文件,什么也不用写,直接保存退出
cd /var/log/
vim mysqld.log
:wq
chmod 777 mysqld.log
chown mysql:mysql mysqld.log
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --lc_messages_dir=/usr/local/mysql/share --lc_messages=en_US
- 查看初始密码(执行后关注最后一点:root@localhost: 这里就是初始密码)
cat /var/log/mysqld.log
- 创建启动脚本
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
- 修改配置文件
vim /etc/init.d/mysqld
#设置默认路径
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data/mysql
- 创建pid目录
#然后执行如下操作开启mysql服务,以及设置相关权限
cd /var/run/
mkdir mysqld
chmod 777 mysqld
cd mysqld
vim mysqld.pid
chmod 777 mysqld.pid
chown mysql:mysql mysqld.pid
- 配置环境变量
vim ~/.bash_profile
export PATH=$PATH:/usr/local/mysql/bin
source ~/.bash_profile
- 设置开启自启
chkconfig --add mysqld
- 启动mysql服务,设置密码
service mysqld start
mysql -uroot -p默认密码
set password=password('新密码');
flush privileges;
#开启远程连接
UPDATE `mysql`.`user` SET `Host` = '%', `User` = 'root' WHERE (`Host` = 'localhost') AND (`User` = 'root');
#添加新的远程用户
grant all privileges on *.* to '新用户名'@'%' identified by '新密码';
flush privileges;
忘记root密码处理方式
#1、停止mysql服务
sudo service mysqld stop
#2、安全模式启动
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables --skip-networking &
#3、打开新的窗口连接mysql
mysql -u root
#4、设置新密码
mysql> use mysql;
mysql> update user set authentication_string=password('你的新密码') where user='root';
mysql> flush privileges;
#5、关闭安全模式,重启mysql
service mysqld start