MySQL Centos

Centos7.4 安装MySQL 教程

Posted on 2020-11-11,5 min read

1、卸载mariadb

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

mariadb-libs-5.5.52-1.el7.x86_64

[root@VM_0_4_centos ~]# rpm -e mariadb-libs --nodeps

warning: /etc/my.cnf saved as /etc/my.cnf.rpmsave

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

2、下载MySQL安装包

[root@VM_0_4_centos ~]# cd /usr/local

[root@VM_0_4_centos local]# wget https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-8.0/mysql-8.0.17-linux-glibc2.12-x86_64.tar.xz 

[root@VM_0_4_centos local]# tar xvJf mysql-8.0.17-linux-glibc2.12-x86_64.tar.xz

[root@VM_0_4_centos local]# mv mysql-8.0.17-linux-glibc2.12-x86_64/ mysql80/

[root@VM_0_4_centos local]# mv mysql80/ /usr/local/

3、配置文件

[root@VM_0_4_centos mysql80]# touch /etc/my.cnf

修改/etc/my.cnf内容

[mysqld]

user=mysql

server_id=3

port=3306

mysqlx_port=33060

mysqlx_socket=/home/mysql80/sock/mysqlx.sock #放到大磁盘里,下同

basedir=/usr/local/mysql80

datadir=/home/mysql80/data

socket=/home/mysql80/sock/mysql.sock

pid-file=/home/mysql80/pid/mysqld.pid

log-error=/home/mysql80/logs/error.log

# slow-query-log=0

# slow-query-log-file=/home/mysql80/logs/slow.log

# long_query_time=2

# log_output=TABLE

log-bin=/home/mysql80/logs/bin.log

relay-log=/home/mysql80/logs/relay.log

binlog_format=ROW

relay_log_recovery=1

character-set-client-handshake=FALSE

character-set-server=utf8mb4

collation-server=utf8mb4_unicode_ci

init_connect ='SET NAMES utf8mb4'

default-time-zone='+8:00'

innodb_buffer_pool_size=1G

join_buffer_size=128M

sort_buffer_size=2M

read_rnd_buffer_size=2M

log_timestamps = SYSTEM

lower_case_table_names = 1

explicit_defaults_for_timestamp = OFF

default-authentication-plugin=mysql_native_password

sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION

max_connections=1000

#skip-grant-tables

auto_increment_offset = 1

auto_increment_increment = 2

log-slave-updates = true #多主多从时保证数据一致

max_binlog_size=5120M #binlog文件最大容量

replicate-ignore-db = mysql #忽略mysql库同步,下同

replicate-ignore-db = information_schema

replicate-ignore-db = performance_schema

replicate-ignore-db = sys

replicate-ignore-table = pmsdb.bs_syrmx_his #忽略pmsdb.bs_syrmx_his表同步,下同

replicate-ignore-table = pmsdb.bs_syrmx_text

[mysql]

socket=/home/mysql80/sock/mysql.sock

[client]

socket=/home/mysql80/sock/mysql.sock

4、创建用户及目录

[root@VM_0_4_centos data]# useradd mysql

[root@VM_0_4_centos data]# mkdir -p /home/mysql80/data

[root@VM_0_4_centos data]# mkdir -p /home/mysql80/logs

[root@VM_0_4_centos data]# mkdir -p /home/mysql80/pid

[root@VM_0_4_centos data]# mkdir -p /home/mysql80/sock



chown -R mysql:mysql /home/mysql80

5、初始化

[root@VM_0_4_centos bin]# cd /usr/local/mysql80/bin

[root@VM_0_4_centos bin]# ./mysqld  --initialize

[root@VM_0_4_centos bin]# cd ../support-files

[root@VM_0_4_centos support-files]# ./mysql.server start

Starting MySQL.. SUCCESS!

6、注册服务

[root@VM_0_4_centos mysql80]# cp  /usr/local/mysql80/support-files/mysql.server /etc/init.d/mysqld

[root@VM_0_4_centos mysql80]# service mysqld status

 SUCCESS! MySQL running (26360)

7、配置环境变量

vim /etc/profile

export MYSQL_HOME
MYSQL_HOME=/usr/local/mysql80
export PATH=$PATH:$MYSQL_HOME/lib:$MYSQL_HOME/binxxxxxxxxxx export MYSQL_HOMEMYSQL_HOME=/usr/local/mysql80export PATH=$PATH:$MYSQL_HOME/lib:$MYSQL_HOME/binMYSQL_HOME=/usr/local/mysql80PATH=$PATH:$HOME/bin:$MYSQL_HOME/bin

source /etc/profile

8、改密

查看初始密码::

grep "temporary password" /home/mysql80/logs/error.log

2020-04-29T19:46:58.392030+08:00 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 0QFs0!mhlYua

以上面查到的密码登录mysql修改root密码

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

Query OK, 0 rows affected (0.01 sec)

设置远程访问

mysql> CREATE USER 'root'@'%' IDENTIFIED BY '123456';

Query OK, 0 rows affected (0.01 sec)

 

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'  WITH GRANT OPTION;

Query OK, 0 rows affected (0.01 sec)

 

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)

 

9、开放端口

firewall-cmd --zone=public --add-port=3306/tcp --permanent

firewall-cmd --reload

10、开机启动

[root@VM_0_4_centos mysql80]# cp /usr/local/mysql80/support-files/mysql.server /etc/rc.d/init.d/mysqld

[root@VM_0_4_centos mysql80]# chmod u+x /etc/init.d/mysqld

[root@dhcjrbdb init.d]# chkconfig --add mysqld

[root@dhcjrbdb init.d]# chkconfig --list

 

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]'.

 

db2autostart      0:off     1:off     2:on     3:on     4:on     5:on     6:off

mysqld         0:off     1:off     2:on     3:on     4:on     5:on     6:off

netconsole       0:off     1:off     2:off     3:off     4:off     5:off     6:off

network         0:off     1:off     2:on     3:on     4:on     5:on     6:off

看到3、4、5状态为开或者为 on 则表示成功。如果是 关或者 off 则执行一下:chkconfig --level 345 mysqld on

重启计算机:reboot

下一篇: SpringBoot实现邮件发送功能→