淘先锋技术网

首页 1 2 3 4 5 6 7

Centos7安装MySQL8

MySQL版本:8.0.34

1.安装前准备

(1)查看是否安装mariadb

[root@kb135 ~]# rpm -qa|grep mariadb

(2)卸载mariadb并检查是否卸干净

[root@kb135 ~]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64

2.安装mysql

(1)将安装包复制到/opt/software文件夹下

(2)解压文件至/usr/local目录下

[root@kb135 software]# tar -xvf mysql-8.0.34-linux-glibc2.12-x86_64.tar.xz -C /usr/local/

(3)重命名

[root@kb135 local]# mv mysql-8.0.34-linux-glibc2.12-x86_64/ mysql

(4)创建data文件夹

[root@kb135 local]# mkdir mysql/data

(5)创建用户和用户组

创建用户组

[root@kb135 mysql]# groupadd mysql

创建用户并指定用户组

[root@kb135 mysql]# useradd -r -g mysql mysql

更改权限

[root@kb135 mysql]# chmod -R 755 /usr/local/mysql/

(6)初始化MySQL

进入MySQL的bin目录

[root@kb135 mysql]# cd /usr/local/mysql/bin/

初始化

[root@kb135 bin]# ./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql

初始化完成后会得到一个随机密码

(7)配置参数文件

[root@kb135 bin]# vim /etc/my.cnf

修改文件为以下内容

[client]

port = 3306

socket = /usr/local/mysql/data/mysql.sock

default-character-set = utf8mb4

[mysql]  

default-character-set = utf8mb4

[mysqld] 

character-set-client-handshake = FALSE

character-set-server = utf8mb4

collation-server = utf8mb4_general_ci

init_connect = 'SET NAMES utf8mb4'

port = 3306

socket = /usr/local/mysql/data/mysql.sock

skip-external-locking

key_buffer_size = 16M

max_allowed_packet = 1M

table_open_cache = 64

sort_buffer_size = 512K

net_buffer_length = 8K

read_buffer_size = 256K

read_rnd_buffer_size = 512K

myisam_sort_buffer_size = 8M

datadir = /usr/local/mysql/data

#lower_case_table_names=1

#如果要设置lower_case_table_names可以在初始化里面设置 ./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql --lower_case_table_names=1

[mysqldump]

quick

max_allowed_packet = 16M

[mysql]

no-auto-rehash

[myisamchk]

key_buffer_size = 20M

sort_buffer_size = 20M

read_buffer = 2M

write_buffer = 2M

[mysqlhotcopy]

interactive-timeout

修改文件权限为777

[root@kb135 bin]# chmod 777 /etc/my.cnf

(8)启动MySQL

[root@kb135 bin]# /usr/local/mysql/support-files/mysql.server start

(9)设置软连接,并重启MySQL

[root@kb135 bin]# ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

[root@kb135 bin]# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql

[root@kb135 bin]# ln -s /usr/local/mysql/mysql.sock /var/mysql.sock

[root@kb135 bin]# service mysql restart

(10)登录并修改密码

[root@kb135 bin]# mysql -uroot -p

输入之前的随机密码

修改密码

mysql> set password for root@localhost = '123456';

(11)开放远程连接

mysql> use mysql;

mysql> update user set user.Host='%' where user.User='root';

mysql> flush privileges;

(12)使用navicat测试连接