1、检查是否已经安装过mysql,执行命令
# rpm -qa | grep mysql
如果能查到,说明已经安装,需要删除
# rpm -e --nodeps mysql-libs-5.1.73-5.el6_6.x86_64
再次执行查询命令,查看是否删除
# rpm -qa | grep mysql
2、查询所有Mysql对应的文件夹
# find / -name mysql
删除所有相关文件(不要删除 /home/mysql)
# rm -rf目录文件(或文件)
4、解压mysql包
下载安装包,上传到mysql,并解压
(https://dev.mysql.com/downloads/mysql/)
# tar xvJf mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz
解压后多出一个 mysql-8.0.18-linux-glibc2.12-x86_64的文件
将文件重命名为mysql,并移动到 /usr/local 下
# mv mysql-8.0.18-linux-glibc2.12-x86_64 mysql
# mv mysql /usr/local
3、创建mysql组和用户
检查是否存在组和用户
# cat /etc/group | grep mysql
# cat /etc/passwd |grep mysql
如果已经存在,则删除
# userdel -r -f mysql
在 /usr/local目录下添加分组和用户
# groupadd mysql
# useradd -r -g mysql mysql
在mysql下新建文件夹data
# cd mysql
# mkdir data
给用户授权(进入mysql目录下授权所有的文件)
# chown -R mysql:mysql ./
初始化mysql,生成随机密码(等下要用)
# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
5、配置文件
初始化没有产生my.cnf文件,需新建一个my-default.cnf并设置权限,将其复制到 /etc下,并更名为my.cnf
# cd support-files/
# touch my-default.cnf
(创建)
# chmod 777 ./my-default.cnf
# cd ..
# cp support-files/my-default.cnf /etc/my.cnf
6、编辑配置文件
# cd /etc
# vim my.cnf
[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 = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /tmp/mysql.sock
log-error = /usr/local/mysql/data/error.log
pid-file = /usr/local/mysql/data/mysql.pid
tmpdir = /tmp
port = 3306
#lower_case_table_names = 1
# server_id = .....
# socket = .....
#lower_case_table_names = 1
max_allowed_packet=32M
default-authentication-plugin = mysql_native_password
#lower_case_file_system = on
#表名不区分大小写
#lower_case_table_names = 1
log_bin_trust_function_creators = ON
# 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
开机自启,进入/usr/local/mysql/support-files进行设置
# cd support-files/
# cp mysql.server /etc/init.d/mysql
# chmod +x /etc/init.d/mysql
注册服务
# chkconfig --add mysql
查看是否成功
# chkconfig --list mysql
.etc/ld.so.conf要配置路径
# vim /etc/ld.so.conf
添加/usr/local/mysql/lib
配置环境变量
# vim /etc/profile
添加export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
7、启动服务
# service mysql start
糟糕,一直报错(下图),找了好多方法都没有解决
然后就查看日志
尝试删除 /tmp/mysql.sock.lock,再重启,失败,继续查看日志(下图),惊喜的发现上图中的错误(Could not open unix socket lock file /tmp/mysql.sock.lock)不见了!!说明有效果。
然后又删除 /tmp/mysql.sock,再次重启,问题解决!
8、进入数据库,修改密码
# mysql -uroot -p
mysql>alter user 'root'@'localhost' identified by 'root';
9、开启navicat远程连接
mysql>use mysql;
查看用户
mysql>select host, user, authentication_string, plugin from user
->;
授权root用户可以远程登陆,结果提示没有权限(因为用户表中的user的host不为%,修改为%,重新执行两次即可)
mysql>GRANT ALL ON *.* TO 'root'@'%';
立即生效
mysql> flush privileges;
修改密码为root,立即生效
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
mysql> flush privileges;
退出数据库,重启服务
mysql> exit;
# service mysql restart
初次安装花了两三天的时间,刚开始我是安装的自定义的目录下,后来重启就报错了,具体是什么错忘记了,可能是配置得有问题。为了准确率高一点,就重新安装,放在大家都用的 /usr/local下,跟着网上大神的指导一步步来,以为一切顺利,结果又出错了,这回总是报找不到mysql8.0.18/mysql/data(大概就是这个错误,这是我上一次自己安装的目录),但是我实在找不到哪个地方还使用了这个路径,就只能又重来~~ 心力交瘁
后来编辑 /etc/ld.so.conf的时候,发现里面的路径还是mysql8.0.18/mysql/lib,应该问题就出在这里了,忘记修改(卸载mysql时配置文件的内容仍存在)。之后就经历了sock报错的折磨~ 总算成功了,也有了收获!