1、下载安装包
http://dev.mysql.com/downloads/mysql/#downloads
Select Operating System:选择Linux-Generic
Select OS Version:选择Linux - Generic (glibc 2.12) (x86, 64-bit)
下载Compressed TAR Archive下面的tar.gz包
2、 检查库文件是否存在,如有删除。
[root@localhost Desktop]$ rpm -qa | grep mysql
mysql-libs-5.1.52-1.el6_0.1.x86_64
[root@localhost ~]# rpm -e mysql-libs-5.1.52.x86_64 --nodeps
3.检查mysql组和用户是否存在,如无创建。
[root@localhost ~]# cat /etc/group | grep mysql
mysql:x:490:
[root@localhost ~]# cat /etc/passwd | grep mysql
mysql:x:496:490::/home/mysql:/bin/bash
以上为默认存在的情况,如无,执行添加命令:
[root@localhost ~]#groupadd mysql
[root@localhost ~]#useradd -r -g mysql mysql
//useradd -r参数表示mysql用户是系统用户,不可用于登录系统。
4.解压TAR包并重命名(mysql包在/opt/software)、创建mysql的data文件夹
tar -zxvf /opt/software/mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz -C /usr/local
mv /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64/ /usr/local/mysql
mkdir /usr/local/mysql/data
5.安装和初始化数据库
warnging:部分mysql包support-file目录下没有my-default.cnf,可将下面方框内内容复制保存到/usr/localmysql/support-file/my-default.cnf
若support-file目录下存在my-default.cnf,则修改basedir、datadir部分即可(注意复制完全,查看有无多余乱码字符)
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
#log-bin=mysql-bin
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
把support-files下的my-default.cnf复制到/etc并重命名,mysql.server复制到/etc/init.d并重命
名
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
修改mysql目录的文件拥有者
chown -R mysql:mysql /usr/local/mysql/
执行安装
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2016-06-01T20:39:38.335054Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-06-01T20:39:38.335124Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2016-06-01T20:39:38.335129Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2016-06-01T20:39:40.439194Z 0 [Warning] InnoDB: New log files created, LSN=45790
2016-06-01T20:39:40.718820Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-06-01T20:39:40.781181Z 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: b9263cbc-7cd1-11e9-b630-000c29cad4b5.
2016-06-01T20:39:40.793759Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2016-06-01T20:39:40.794421Z 1 [Note] A temporary password is generated for root@localhost: EGYWO4sdT5,t
(记住root登录的临时密码,红色字体处)
linux下安全打开mysql
[root@localhost mysql]# /usr/local/mysql/bin/mysqld_safe --user=mysql &
[1]2932
[root@localhost bin]# 2016-06-01T22:27:09.708557Z mysqld_safe Logging to'/usr/local/mysql/data/localhost.localdomain.err'.
2016-06-01T22:27:09.854913Z mysqld_safe Starting mysqld daemon with databasesfrom /usr/local/mysql/data(这里需要回车)
[root@localhost bin]# /etc/init.d/mysqld restart
Shutting downMySQL..2017-11-03T02:55:33.863504Z mysqld_safe mysqld from pid file/usr/local/mysql/data/localhost.localdomain.pid ended
SUCCESS!
Starting MySQL. SUCCESS!
[1]+ Done mysqld_safe --user=mysql
[root@localhost bin]#
配置环境变量
在/etc/profile 尾部增加export PATH=/usr/local/mysql/bin:$PATH
[root@localhost /]# source /etc/profile
//设置开机启动
[root@localhost bin]# chkconfig --level 35 mysqld on
6.初始化密码
复制上面临时密码
//登陆数据库
[root@localhost bin]# mysql -u root -p
Enter password:
//修改root账号密码
mysql> SET PASSWORD = PASSWORD('root');
mysql> flush privileges;
7.添加远程访问权限
mysql> use mysql;
mysql> update user set host = '%' where user = 'root';
//退出数据库
mysql>exit;
[root@localhost bin]# /etc/init.d/mysqld restart
8、开启3306端口
[root@localhost bin]# vi /etc/sysconfig/iptables
增加
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
重启防火墙
[root@localhost bin]# /etc/init.d/iptables restart
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
[root@localhost bin]#