1.下载mysql5.7二进制包
[root@node1 ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
2.解包
[root@node1 ~]# tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
3.将解过的mysql5.7包移动到/usr/local/下并改名为mysql
[root@node1 ~]# mv mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql
4.切换到/usr/local/mysql下
[root@node1 mysql]# cd /usr/local/mysql
5.新增mysql用户,并禁止shell登陆
[root@node1 mysql]# useradd -M -s /sbin/nologin mysql
6.初始化mysql5.7数据库
[root@node1 mysql]# ./bin/mysqld --initialize --user=mysql --datadir=/data/mysql
6.1 注意生成的临时密码AM8FWl&Kd7ot
2018-04-23T08:32:48.223884Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2018-04-23T08:32:48.451367Z 0 [Warning] InnoDB: New log files created, LSN=45790 2018-04-23T08:32:48.575541Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2018-04-23T08:32:48.665475Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e78c02e8-46d0-11e8-8d63-080027de0e0e. 2018-04-23T08:32:48.667660Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2018-04-23T08:32:48.668510Z 1 [Note] A temporary password is generated for root@localhost: AM8FWl&Kd7ot
6.2 继续执行
[root@node1 mysql]# ./bin/mysql_ssl_rsa_setup --datadir=/data/mysql
7.复制配置文件并修改
7.1 复制主配置文件(如果没有my-default.cnf,直接添加或修改/etc/my.cnf文件)
[root@node1 mysql]# cp ./support-files/my-default.cnf /etc/my.cnf
7.2修改 /etc/my.cnf 配置文件
[root@node1 mysql]# vim /etc/my.cnf
必须全部添加或修改
[mysqld] basedir = /usr/local/mysql datadir = /data/mysql port = 3306 socket = /tmp/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 = /data/mysql/mysql-error.log pid-file = /data/mysql/mysql.pid # # include all files from the config directory #
8.复制启动文件并修改相关参数
8.1 复制启动脚本文件到init.d下
[root@node1 mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
8.2 修改启动脚本相关参数
[root@node1 mysql]# vim /etc/init.d/mysqld
找到basedir,datadir添加或修改路径
basedir = /usr/local/mysql //指定程序路径 datadir = /data/mysql //指定数据存放路径
9.启动mysql服务,并查看服务启动状态
9.1加入开机启动
[root@node1 mysql]# chkconfig --add mysqld
9.2启动mysql服务
[root@node1 mysql]# /etc/init.d/mysqld start
9.3查看mysql进程
[root@node1 mysql]# ps aux |grep mysqld
9.4查看3306端口监听情况
[root@node1 mysql]# netstat -ntlp | grep 3306
10.添加环境变量
10.1 打开profile文件
[root@node1 mysql]# vim /etc/profile
10.2 添加PATH变量,如果有PATH直接添加,没有的话直接在文件最后一行添加。
export PATH=/usr/local/mysql/bin:$PATH
10.3 重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录。
[root@node1 mysql]# source /etc/profile
11.设置密码
11.1 使用初始化临时密码登陆
[root@node1 mysql]# mysql -uroot -p'AM8FWl&Kd7ot'
11.2 修改mysql登陆密码
[root@node1 mysql]# set password = password('123456');
11.3 测试修改后的密码是否能登录
[root@node1 mysql]#mysql -uroot -p'123456' mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.22 MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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>
12. 数据库授权
12.1 远程连接新建一个帐号(帐号名不能为root)
如:添加一个用户名为 www,密码为 123456,授权为% (%表示所有 ip能连接,可以设置指定ip)对数据库所有权限,命令如下:(授权语句,特别注意有分号)
mysql> grant all privileges on *.* to 'www'@'%' identified by '123456' WITH GRANT OPTION;
12.2 重新加载权限表,否则会出现拒绝访问;然后退出
mysql> flush privileges; mysql> exit;
13.配置MySQL远程连接
为了安全考虑,mysql仅允许本机(localhost)连接数据库,如果需要远程连接数据库,需要如下操作
13.1 打开iptables 3306端口
[root@node1 ~]# iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
13.2 保存iptables规则(如果出现指令使用失败请点击)
[root@node1 ~]# service iptables save
13.3 查看当前表的所有规则
[root@node1 ~]# iptables -nvL --line-number
14.远程测试mysql是否能连接
[root@node1 ~]# mysql -h192.168.100.10 -uwww -p123456 -P3306 -A
您可以选择一种方式赞助本站