一、关闭防火墙
[root@kettle01 ~]# firewall-cmd –state
running
[root@kettle01 ~]# systemctl stop firewalld.service #停止防火墙
[root@kettle01 ~]# systemctl disable firewalld.service #禁止firewall开机启动
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@kettle01 ~]# firewall-cmd –state #查看默认防火墙状态
not running
二、修改服务器文件打开数
[root@kettle01 ~]# vi /etc/security/limits.conf
添加下面这个这段,修改最大文件打开数
∙soft nofile 65536
∙hard nofile 65536
∙soft nproc 65536
∙hard nproc 65536
重新登录,查看是否修改成功 ulimit -a
修改/etc/selinux/config 文件
将SELINUX=enforcing改为SELINUX=disabled
三、新建mysql用户并修改用户密码
[root@kettle01 ~]# useradd mysql
[root@kettle01 ~]# passwd mysql
Changing password for user mysql.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
四、在/opt目录下解压mysql安装包并改名为mysql,在mysql下创建data数据目录
[root@kettle01 opt]# tar -zxvf mysql-5.7.15-linux-glibc2.5-x86_.tar.gz
[root@kettle01 opt]# mv mysql-5.7.15-linux-glibc2.5-x86_ mysql
[root@kettle01 opt]# cd mysql
[root@kettle01 mysql]# mkdir data
[root@kettle01 mysql]#chown -R mysql:mysql /opt/mysql #变更/opt/mysql所有者为mysql
五、添加环境变量
echo $PATH 查看/opt/mysql/bin这个路径是否有被加到path路径中
如果没有的话,可以 vim /etc/profile.d/mysql.sh
编辑增加一行
export PATH=$PATH:/opt/mysql/bin
可以通过 source /etc/profile.d/mysql.sh 让其立即生效,在查看一下 echo $PATH
六、执行mysql安装命令
[root@kettle01 mysql]# /opt/mysql/bin/mysqld –initialize-insecure –user=mysql –basedir=/opt/mysql –datadir=/opt/mysql/data –explicit_defaults_for_timestamp=true
#初始化安装默认ROOT用户无密码登录
安装提示(正常):
2016-09-13T10:02:59.5766Z 0 [Warning] InnoDB: New log files created, LSN=45790
2016-09-13T10:03:00.0076Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-09-13T10:03:00.126120Z 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: 408c1f61-7999-11e6-ae80-000c2910c386.
2016-09-13T10:03:00.131358Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.
2016-09-13T10:03:00.133248Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the –initialize-insecure option.
六、复制配置文件mysql.server到/etc/init.d/mysqld目录
[root@kettle01 ~]# cp /opt/mysql/support-files/mysql.server /etc/init.d/mysqld #服务器配置文件
[root@kettle01 ~]# chkconfig –add mysqld #添加到服务列表中
[root@kettle01 ~]# chkconfig –list mysqld #查看是否会默认启动,2、3、4、5是on
[root@kettle01 ~]# cp /opt/mysql/support-files/my-default.cnf /etc/my.cnf #数据库配置文件
cp: overwrite ‘/etc/my.cnf’? yes
[root@kettle01 ~]# vi /etc/my.cnf #修改数据库配置文件
添加以下参数
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER
basedir=/opt/mysql
datadir=/opt/mysql/data
user=mysql
port=3306
socket = /tmp/mysql.sock
character_set_server=utf8
init_connect=’SET NAMES utf8’
collation-server=utf8_general_ci
max_connections=100
skip-name-resolve # mysql会解析域名, 导致访问速度很慢, 会有2,3秒延时!添加这段解决
[client]
default-character-set=utf8
socket = /tmp/mysql.sock
七、启动mysql数据库
[root@kettle01 ~]# service mysqld start #启动数据库start|stop|status|restart 四种状态
Starting MySQL. SUCCESS!
[root@kettle01 ~]# netstat -lntp | grep 3306 #查看是否有启动mysqld
tcp6 0 0 :::3306 :::* LISTEN 9315/mysqld
[root@kettle01 ~]mysql -uroot #无密码登录
root用户设置一个初始密码
mysql> update mysql.user set authentication_string=password(‘root’) , password_expired = ‘N’, password_last_changed = now() where user = ‘root’; #变更用户权限
mysql> GRANT ALL PRIVILEGES ON . TO ‘root’@’%’ IDENTIFIED BY ‘root’ WITH GRANT OPTION; #授权root用户远程登录权限
mysql> FLUSH PRIVILEGES; #权限修改生效
mysql> show variables like ‘character_set%’; #查看字符集
mysql> quit; #退出
安装完毕。
建库
CREATE DATABASE aisp_pass DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
grant create,select,insert,update,delete,drop,alter on aisp_pass.* to aisp_pass@’%’ identified by ‘aisp_pass’;
八、常见报错处理:
1 、ERROR 1045 (28000): Access denied for user ‘root’@’localhost’
1.停止mysql数据库
/etc/init.d/mysqld stop
2.执行如下命令
mysqld_safe –user=mysql –skip-grant-tables –skip-networking & #不想被远程连接
3.使用root登录mysql数据库
mysql -u root mysql
4.更新root密码
mysql> UPDATE user SET Password=PASSWORD(‘newpassword’) where USER=’root’;
5.刷新权限
mysql> FLUSH PRIVILEGES;
6.退出mysql
mysql> quit
7.重启mysql
/etc/init.d/mysqld restart
8.使用root用户重新登录mysql
mysql -uroot -p
Enter password: <输入新设的密码newpassword>
2、忘记root密码
1、修改 /etc/my.cnf,在 [mysqld] 小节下添加一行:skip-grant-tables=1
#这一行配置让 mysqld 启动时不对密码进行验证
2、重启 mysqld 服务:service mysql restart
3、使用 root 用户登录到 mysql:mysql -u root
4、切换到mysql数据库,更新 user 表:
update user set authentication_string = password(‘root’), password_expired = ‘N’, password_last_changed = now() where user = ‘root’;
5、退出 mysql,编辑 /etc/my.cnf 文件,删除 skip-grant-tables=1 的内容
6、重启 mysqld 服务,再用新密码登录即可