环境准备
CentOS 7
jdk1.8
mysql 5.7.25
- 下载地址:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
安装步骤
卸载旧的mysql
卸载已安装的MySQL,卸载mysql命令,如下:
rpm -e --nodeps mysql-connector-odbc-5.2.5-6.el7.x86_64
删除mysql的安装文件
查询mysql的安装文件:
[root@ali228 ~]# find / -name mysql
/usr/local/env/mysql
/usr/local/env/mysql/mysql
/usr/local/env/mysql/mysql/bin/mysql
/usr/local/env/mysql/mysql/include/mysql
/usr/local/env/mysql/mysql/data/mysql
[root@ali228 ~]#
卸载:
[root@ali228 ~]# rm -rf /usr/local/env/mysql/mysql
[root@ali228 ~]#
删除mysql的配置文件
删除/etc/my.cnf文件
rm -rf /etc/my.cnf
删除/etc/init.d/下跟mysql有关的全部文件,一般包括mysql文件或mysqld文件。
rm -rf /etc/init.d/mysql
rm -rf /etc/init.d/mysqlId
删除mysql用户和用户组
userdel mysql
至此,卸载OK!
安装MySQL
下载安装包
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
上传mysql-5.7.25-linux-glibc2.5-x86_64.tar.gz到/opt目录
解压到/usr/local
tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
添加用户组mysql和用户mysql,并将其添加到mysql用户组中
groupadd mysql
useradd -r -g mysql mysql
注: useradd -r参数表示mysql用户是系统用户,不可用于登录系统**。**
useradd -g参数表示把mysql用户添加到mysql用户组中。
检查是否安装了 libaio
rpm -qa | grep libaio
如果没有安装,使用如下命令安装
yum search libaio
配置my.cnf文件
touch /etc/my.cnf
将如下内容拷贝到里面:
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
socket=/tmp/mysql.sock
[mysqld]
#skip-name-resolve
#设置3306端口
port=3306
socket=/tmp/mysql.sock
# 设置mysql的安装目录
basedir=/usr/local/env/mysql/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/env/mysql/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符雿
character-set-server=utf8
# 创建新表时将使用的默认存储引擿
default-storage-engine=INNODB
#lower_case_table_name=1
max_allowed_packet=16M
创建data目录
在/usr/local/mysql目录下创建data目录,数据库文件将会放在这里
mkdir -p /usr/local/mysql/data
将mysql目录的所属用户和组改为mysql
chown -R mysql:mysql ./
初始化mysqld 生成初始化密码
./bin/mysqld --initialize --user=mysql --basedir=/usr/local/env/mysql/mysql --datadir=/usr/local/env/mysql/mysql/data
此时,控制台打印如下:
2018-10-24T04:25:13.673365Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-10-24T04:25:15.808961Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-10-24T04:25:16.105505Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-10-24T04:25:16.184776Z 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: cec94f21-d744-11e8-a0b5-fa163ed8e403.
2018-10-24T04:25:16.188372Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-10-24T04:25:16.189074Z 1 [Note] A temporary password is generated for root@localhost: <_OQf5Gz?h,v
结尾:<_OQf5Gz?h,v 就是初始化的密码。
常规配置
配置开启启动
拷贝mysql.server到init.d目录下
复制mysql.server脚本到资源目录,并赋予执行权限
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
将 mysqld 服务加入到系统服务并检测是否生效
[admin@localhost opt]$ chkconfig --add mysqld
[admin@localhost opt]$ chkconfig --list mysqld
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
得到如下结果说明生效。
注: 查看启动项:chkconfig --list | grep -i mysql 删除启动项:chkconfig --del mysql
启动mysqld:
[admin@localhost opt]$ service mysqld start
Starting MySQL SUCCESS!
配置环境变量
打开/etc/profile配置文件,添加如下内容
#mysql环境变量
PATH=$PATH:/usr/local/env/mysql/mysql/bin
export PATH
执行命令,使其生效
source /etc/profile
校验是否成功
admin@localhost opt]$ echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/java/jdk1.8.0_144/bin:/usr/local/mysql/bin:/home/admin/.local/bin:/home/admin/bin
修改登录密码
注意: 此时密码使用上面自动生成的初始化密码: <_OQf5Gz?h,v 即可
[admin@localhost opt]$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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> update user set authentication_string=PASSWORD('root') where user='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
允许远程访问
mysql> grant all privileges on *.* to root@"%" identified by "root" 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>
开放端口号
# 添加3306端口
sudo firewall-cmd --add-port=3306/tcp --permanent
# 重启
firewall-cmd --reload
# 查看
firewall-cmd --list-all