MySQL5.7.21 for Centos7
目录
1、环境参数
1.1、操作系统环境
1.2、创建mysql帐号
[root@t2 software]# groupadd mysql
[root@t2 software]# cat /etc/group |grep mysql
mysql:x:1000:
[root@t2 mysql]# useradd -r -g mysql -s /bin/false mysql
[root@t2 mysql]# cat /etc/passwd |grep mysql
mysql:x:994:1000::/home/mysql:/bin/false
创建mysql目录安装位置:
mkdir /usr/local/mysql/
ll /usr/local/mysql/
2、安装数据库
2.1、安装数据库
[root@t2 software]# tar -zxvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
重命名:
[root@t2 local]# pwd
/usr/local
解压后的文件移到创建mysql的安装目录下
[root@t2 local]# mv mysql-5.7.21-linux-glibc2.12-x86_64/* /usr/local/mysql/
[root@t2 local]# rm –rf mysql-5.7.21-linux-glibc2.12-x86_64
创建数据库保存位置:
mkdir /usr/local/mysql/data/
设置mysql安装目录和数据库保存目录文件权限
chown -R mysql:mysql /usr/local/mysql
chmod -R 750 /usr/local/mysql
2.2、初始化数据库
切换到目录:
/usr/local/mysql/
执行命令:
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/
进行初始化。
注意:记录好这个临时密码,第一次登录要用到。root@localhost: DmqQHkrO,3jt
2.3、安装SSL
[root@MYSQL mysql]# pwd
/usr/local/mysql
[root@MYSQL mysql]#
执行命令:
bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data/
2.4、修改系统配置参数(注意当前目录),命令如下。
切换目录:
/usr/local/mysql/support-files
执行命令:
cp mysql-log-rotate /etc/my.cnf
cp mysql.server /etc/init.d/mysql
修改vi /etc/init.d/mysql 如图
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data/
修改 vi /etc/my.cnf
添加
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data/
socket=/tmp/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/usr/local/mysql/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'
创建 /usr/local/mysql/mysqld.pid 文件
touch /usr/local/mysql/mysqld.pid
创建连接
# ln -s /usr/local/mysql/bin/mysql /usr/bin/
# ln -s /usr/local/mysql/bin/mysqladmin /usr/bin/
2.5、启动mysql
执行命令:
service mysql start
3、数据库配置
3.1、设置密码
登陆mysql:
执行:
mysql -u root –p
输入初始化保存的密码DmqQHkrO,3jt
设备mysql密码:
mysql> set password=password('xxx@123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
重启mysql
service mysql restart
丢失root密码时,设置root密码
1、修改mysql的配置文件(默认为/etc/my.cnf),在[mysqld]下添加一行skip-grant-tables
2、保存配置文件后,重启mysql服务 service mysqld restart
3、mysql -u root -p登录mysql,然后不输入密码直接回车,然后按照上面的流程修改密码
mysql>
mysql> update user set authentication_string=password('xxx@123') where user='root';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
4、密码修改完毕,按照流程1的删除配置文件中的那行,然后重启mysql服务
3.2、设置开机启动
添加到开机服务服务中:
chkconfig --list mysql
[root@t2 bin]# chkconfig --add mysql
设置开机启动:
[root@t2 bin]# chkconfig mysql on
3.4、设置环境变量
/usr/local/mysql/bin
[root@t2 bin]# vi ~/.bash_profile
#mysql5.7.21 bin
export MYSQL_HOME=/usr/local/mysql/
export PATH=$MYSQL_HOME/bin:$PATH
使环境变量生效:
[root@t2 bin]# source ~/.bash_profile
测试是否生效:
[root@t2 bin]# mysql -uroot -p
Enter password:
4.设置授权链接
[root@IFOS1 mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21 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> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
3 rows in set (0.00 sec)
mysql> grant all privileges on *.* to 'iams'@'xxx.xxx.xx.xxx' identified by 'xxx@123' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> ^C
mysql> select host,user from user;
+----------------+---------------+
| host | user |
+----------------+---------------+
| xxx.xxx.xx.xxx | iams |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+----------------+---------------+
4 rows in set (0.00 sec)
注:mysql 新设置用户或更改密码后需用flush privileges刷新MySQL的系统权限相关表,否则会出现拒绝访问
grant all privileges on *.* to 'iams'@'xxx.xxx.xxx.xxx' identified by 'xxx@123' with grant option;
grant all privileges on *.* to 'root'@'xxx.xxx.x.%' identified by 'xxx@123' with grant option;
5.报错处理
mysql5.7免安装版报错处理
安装完mysql 之后,登陆以后,不管运行任何命令,总是提示这个
You must reset your password using ALTER USER statement before executing
完成以下三步退出再登,使用新设置的密码就行了,以上除了红色的自己修改成新密码外,其他原样输入即可
step 1: SET PASSWORD = PASSWORD('xxx@123');
step 2: ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
step 3: flush privileges;
MySQL安装结束!