一.在mysql官网下载压缩文件
https://dev.mysql.com/downloads/mysql/5.7.html#downloads
二.将下载下来的文件拷贝到centos系统的/opt目录下
三.开始安装
- 1.解压mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@centosDemo opt]# tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
- 2.剪切到/usr/local/mysql目录下
[root@centosDemo opt]# mv mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql
- 3.创建mysql用户和用户组
[root@centosDemo opt]# useradd mysql
[root@centosDemo opt]# id mysql
- 4.进入到mysql文件夹并修改所有文件和目录的所属人和所属组
[root@centosDemo opt]# cd /usr/local/mysql/
[root@centosDemo mysql]# chown -R mysql .
[root@centosDemo mysql]# chgrp -R mysql .
- 5.创建数据存放的目录
[root@centosDemo mysql]# mkdir data
- 6.安装mysql需要的依赖库
[root@centosDemo mysql]# yum install libaio
[root@centosDemo mysql]# yum -y install numactl.x86_64
- 7.进入到mysql的bin目录下,执行安装
[root@centosDemo mysql]# cd bin/
[root@centosDemo bin]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
- 8.查看mysql的初始密码,下面显示的是我安装后的初始密码:#eODhmNio>-P
[root@centosDemo bin]# cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2018-07-15 16:48:35
#eODhmNio>-P
- 9.启动mysql
[root@centosDemo bin]# cd ../support-files/
[root@centosDemo support-files]# ./mysql.server start
Starting MySQL.Logging to '/usr/local/mysql/data/centosDemo.err'.
SUCCESS!
- 10.登录mysql
[root@centosDemo support-files]# cd ../bin/
[root@centosDemo bin]# ./mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.22
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> set password=password('root');
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql>
四.设置mysql为自动启动
[root@centosDemo mysql]# cp support-files/mysql.server /etc/init.d/mysql
[root@centosDemo mysql]# chkconfig --add mysql
[root@centosDemo mysql]# chkconfig --list mysql
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
五.解决mysql命令找不到的问题
[root@centosDemo mysql]# mysql -uroot -p
-bash: /usr/bin/mysql: No such file or directory
[root@centosDemo mysql]# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
六.解决远程登录的问题
- 1.设置防火墙,允许3306端口被访问
[root@centosDemo ~]# vim /etc/sysconfig/iptables
- 2.重启防火墙
[root@centosDemo ~]# service iptables restart
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
3.设置mysql数据库允许远程登录
登录mysql成功后,输入下面两条命令即可:
GRANT ALL PRIVILEGES ON . TO root@’%’ identified by ‘root’;
flush privileges;
[root@centosDemo ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
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> GRANT ALL PRIVILEGES ON *.* TO root@'%' identified by 'root';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)