一、检查是否已经安装了mysql
[[email protected] /]# rpm -qa | grep -i mysql
如果有,则使用删除语句删除
[[email protected] /]# yum -y remove mariadb-libs-5.5.56-2.el7.x86_64
再使用查询命令,查找mysql相关的文件
[[email protected] /]# find / -name mysql
然后把存在的文件都删除
[[email protected] /]# rm –rf {目录名}
二、下载mysql安装包
进入包下载所放位置下载
[[email protected] /]# cd /usr/local/src
[[email protected] src]# wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
进行解压
[[email protected] src]# tar zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
[[email protected] src]# ls
mysql-5.7.25-linux-glibc2.12-x86_64 mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
移位置并改名
[[email protected] src]# mv mysql-5.7.25-linux-glibc2.12-x86_64 /usr/local/mysql
[[email protected] src]# cd /usr/local/mysql
[[email protected] mysql]# ls
bin COPYING docs include lib man README share support-files
三、配置初始化mysql
添加用户组和新用户名
[[email protected] mysql]# groupadd mysql
[[email protected] mysql]# useradd -r -g mysql -s /bin/false mysql
创建data目录
[[email protected] mysql]# mkdir data
修改mysql目录用户为刚刚新建的mysql组中的mysql用户
[[email protected] mysql]# chown -R mysql:mysql ./
初始化安装mysql数据库
[[email protected] mysql]# ./bin/mysqld --user=mysql --basedir=/usr/local/mysql -- datadir=/usr/local/mysql/data --initialize
2019-07-07T07:51:36.066665Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-07-07T07:51:37.197916Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-07-07T07:51:37.299392Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-07-07T07:51:37.823229Z 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: 0c916440-a08c-11e9-87be-000c2954a0d9.
2019-07-07T07:51:37.846198Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
2019-07-07T07:51:37.847191Z 1 [Note] A temporary password is generated for [email protected]: yrntyj)qo6Nd
修改my.conf配置文件,从版本5.7.18开始,mysql免安装二进制包中就不包含该文件了,即不需要my.conf文件也可以正常运行;my.conf文件中配置的选项会在命令行启动mysql的时候作为参数进行启动,为了后面搭建mysql主从环境方便,下面可以添加了一个简单的my.conf文件作为实例(如果只是单纯的搭建一个mysql实例,可以直接忽略此步骤),使用vim /etc/my.conf命令,如果在该目录上不存在则会新建
[mysqld]
character_set_server=utf8
init_connect=‘SET NAMES utf8‘
basedir=/usr/local/mysql
datadir=/usr/local/software/data
socket=/usr/local/software/mysql.sock
#设置忽略大小写(简单来说就是sql语句是否严格),默认库名表名保存为小写, 不区分大小写
lower_case_table_names = 1
# 开启ip绑定
bind-address = 0.0.0.0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/usr/local/mysql/data/mysqld.pid
#指定客户端连接mysql时的socket通信文件路径
[client]
socket=/usr/local/mysql/mysql.sock
default-character-set=utf8
四、其他配置
添加开机启动
[[email protected] mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
修改mysqld,使用vim /etc/init.d/mysqld 命令 修改以下代码部分
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
设置开机启动
[[email protected] mysql]# chkconfig --add mysqld
为了可以在任意目录上都可以使用mysql命令登录mysql,将mysql安装目录配置到环境变量中,在/etc/profile文件的末尾添加以下代码
export PATH=$PATH:/usr/local/mysql/bin
使配置文件的配置立即生效
[[email protected] mysql]# source /etc/profile
重启mysql服务,并且使用mysql的root用户登录mysql
[[email protected] mysql]# service mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL.. SUCCESS!
[[email protected] mysql]# 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>
修改root用户的密码为root,并且刷新
mysql> alter user ‘root‘@‘localhost‘ identified by ‘root‘;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
此时mysql数据库只能在本机上使用mysql命令进行登录,还无法使用navicat等数据库可视化工具进行远程登录,下面设置允许root用户远程连接数据库
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 user.Host=‘%‘ where user.User=‘root‘;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
查看防火墙
[[email protected] mysql]# firewall-cmd --state
running
打开3306端口
[[email protected] mysql]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
防火墙重启
[[email protected] mysql]# firewall-cmd --reload
success
至此,可以直接使用navicat等连接数据库
原文地址:https://blog.51cto.com/13242922/2417891
时间: 2024-10-17 12:05:39