一、环境准备
- centos7
- mysql-5.7.26-linux-glibc2.12-x86_64.tar
二、安装
-
下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
-
解压
tar -xvf mysql-5.7.26-linux-glibc2.12-x86_64.tar
-
再移动并重命名一下
mv mysql-5.7.26-linux-glibc2.12-x86_64 /usr/local/mysql
-
创建mysql用户组和用户并修改权限
groupadd mysql useradd -r -g mysql mysql
-
创建数据目录并赋予权限
mkdir -p /data/mysql #创建目录 chown mysql:mysql -R /data/mysql #赋予权限
-
配置my.cnf
vim /etc/my.cnf # 内容如下 [mysqld] bind-address=0.0.0.0 port=3306 user=mysql basedir=/usr/local/mysql datadir=/data/mysql socket=/tmp/mysql.sock log-error=/data/mysql/mysql.err pid-file=/data/mysql/mysql.pid #character config character_set_server=utf8mb4 symbolic-links=0 explicit_defaults_for_timestamp=true # datadir=/var/lib/mysql # socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks # symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd # [mysqld_safe] # log-error=/var/log/mariadb/mariadb.log # pid-file=/var/run/mariadb/mariadb.pid # # include all files from the config directory # !includedir /etc/my.cnf.d
三、初始化数据库
-
进入mysql的bin目录
cd /usr/local/mysql/bin/
-
初始化
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
-
查看密码
cat /data/mysql/mysql.err
- 启动mysql,并更改root密码
-
启动
service mysql start ps -ef|grep mysql
-
到这里说明mysql已经安装成功了!!
-
下面修改密码
-
首先登录mysql,密码是前面的那个是随机生成的。
./mysql -u root -p #bin目录下
- 再执行下面三步操作,然后重新登录。
SET PASSWORD = PASSWORD('1234'); ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER; FLUSH PRIVILEGES;
-
-
设置远程连接数据库
use mysql #访问mysql库 update user set host = '%' where user = 'root'; #使root能在任何host访问 FLUSH PRIVILEGES; #刷新
-
ok!!!!MySQL5.7就装好了
如果不希望每次都到bin目录下使用mysql命令则执行以下命令
ln -s /usr/local/mysql/bin/mysql /usr/bin
四、配置开机启动
-
将服务文件拷贝到init.d下,并重命名为mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
-
赋予可执行权限
chmod +x /etc/init.d/mysqld
-
添加服务
chkconfig --add mysqld
-
显示服务列表
chkconfig --list
如果看到mysql的服务,并且3,4,5都是on的话则成功,如果是off,则键入
chkconfig --level 345 mysql on
-
重启电脑
reboot
-
验证
netstat -na | grep 3306