1:查看 linux下是否有老版本的mysql(有删除)

查找old mysql:rpm -qa | grep mysql

卸载:卸载命令:rpm –ev {包名}——:rpm -ev mysql-community-common-5.7.23-1.el7.x86_64

查找老版本mysql相关的安装目录命令:find / -name mysql

若查找到相关目录使用命令:rm –rf {目录名}:删除目录

 

2:查看 linux 下是否安装 mariadb 数据库(有的话需要删除,因为有冲突)

检查是否安装了 mariadb: rpm -qamariadb | grep

删除mariadb:rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64

3:创建mysql存放目录(/root/software)

创建文件夹:mkdir /root/software

解压到当前文件夹,并把解压后文件移动到指定文件夹并修文件夹名称:

解压:tar -xzvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

移动并修改名字:mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql

4:创建主目录(data:存储目录)

创建主目录:mkdir /usr/local/mysql/data

5:主目录权限处理(查看是否有就得用户,有删除并新建用户)

查看组和用户情况:cat /etc/group | grep mysql
查看组和用户情况:cat /etc/passwd |grep mysql

若存在,则删除原mysql用户:userdel -r mysql,会删除其对应的组和用户并在次查看。

创建mysql组:groupadd mysql
创建mysql用户:useradd -r -g mysql mysql
修改目录拥有者:chown -R mysql:mysql /usr/local/mysql

6:创建配置文件及相关目录(如果在这个路径下已经存在的话就不用创建了)

创建配置文件:vim /etc/my.cnf

文件模板:

# 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.

[mysqld]

# 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 = .....
# datadir = .....
# port = .....
# 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

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

 

修改配置文件内容:注意要在模板的  [mysqld] 下面去修改(basedir:mysql安装路径,datadir:数据存储目录)

basedir=/usr/local/mysql

datadir=/usr/local/mysql/data

port = 3306

socket=/tmp/mysql.sock

pid-file=/tmp/mysqld/mysqld.pid

character-set-server = utf8

log-error=/var/log/mysqld.log

 

保存退出:wq!

创建文件/tmp/mysql.sock:设置用户组及用户,授权

cd /tmp
touch mysql.sock
chown mysql:mysql mysql.sock
chmod 755 mysql.sock

创建文件/tmp/mysqld/mysqld.pid

mkdir mysqld
cd mysqld
touch mysqld.pid
cd ..
chown -R mysql:mysql mysqld
cd mysqld
chmod 755 mysqld.pid

创建文件/var/log/mysqld.log:

touch /var/log/mysqld.log
chown -R mysql:mysql /var/log
cd /var/log
chmod 755 mysqld.log

 

7:安装和初始化数据库

进入初始化目录:cd /usr/local/mysql/bin/

初始化数据库:./mysqld --initialize --user=mysql --basedir=/usr/local/mysql--datadir=/usr/local/mysql/data

如果报错:(./mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory)

需要安装命令:yum -y install numactl

之后在执行初始化数据库:./mysqld --initialize --user=mysql --basedir=/usr/local/mysql--datadir=/usr/local/mysql/data

8:安全启动:

./mysqld_safe --user=mysql &

之后回车进入到bin目录

查看是否成功:ps -ef | grep mysql

默认密码在mysqld.log日志里, 找到后保存到安全的地方:cat /var/log/mysqld.log

其中root@localhost: 后面的就是默认密码,后面登录用(D;J.ogLj8ETr)

进入bin目录:

cd /usr/local/mysql/bin/

登录mysql:

./mysql -u root -p

但是,若输入相关命令,则会提示你修改用户密码(注意后面一定要加;)。

show databases;

密码修改为 aaa

mysql> set password=password("aaa");

9:设置远程登录权限(在mysql里面设置)

mysql>grant all privileges on *.* to 'root'@'%' identified by 'aaa';

刷新登录权限:

mysql> flush privileges;

退出quit 或者 exit

mysql> quit;

10:开机服务启动设置:

把support-files/mysql.server 拷贝为/etc/init.d/mysql:

命令:cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

查看是否成功:(名字为mysql)

cd /etc/init.d/

ll

查看mysql服务是否在服务配置中

chkconfig --list mysql

若没有,则把mysql注册为开机启动的服务,然后在进行查看

chkconfig --add mysql

chkconfig --list mysql

启动 或 停止

service mysql start

service mysql stop

11:创建快捷方式:
服务启动后,直接运行mysql -u root -p即可登录,不需要进入到对应的目录。

ln -s /usr/local/mysql/bin/mysql /usr/bin

12:使用Navicat 连接数据库时会出现(2003)
说明你的防火墙没有关。

解决方案:

//临时关闭 systemctl stop firewalld

//禁止开机启动

systemctl disable firewalld

Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.

Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.