1、安装之前先按照jdk,jdk安装步骤参考上一篇文章。
2、官网下载安装包,上传至服务器
官网上没有CentOS的版本,因为CentOS使用的是RedHat内核,所以可以选择RedHat版本,也可以选择Generic源码安装,我选择的是如下版本
注意:解压tar.xz文件:先 xz -d xxx.tar.xz 将 xxx.tar.xz解压成 xxx.tar 然后,再用 tar xvf xxx.tar来解包。
3、更改mysql目录下所有的目录及文件夹所属的用户组和用户,以及权限
cd /usr/local/mysql/
# 创建文件夹
mkdir data
# 给文件夹授权
chown -R root:root /usr/local/mysql
# 创建mysql用户
useradd mysql
# 给文件夹授权
chown -R mysql:mysql /usr/local/mysql/data/
4、编译安装并初始化mysql
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
5、可能会出现如下错误
①检查该链接库文件有没有安装使用命令
rpm -qa|grep libaio
②运行命令后发现系统中无该链接库文件,使用如下命令安装
yum install libaio-devel.x86_64
③继续执行初始化命令,如仍然出错,则键入如下命令
yum -y install numactl
6、cd support-files/
#创建文件
touch my-default.cnf
#授权
chmod 777 ./my-default.cnf
#复制空白文件到my.cnf
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
7、配置my.cnf
vim /etc/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
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
注:如果后期mysql运行报错,可以直接到log-error = /usr/local/mysql/data/error.log目录下直接查看错误日志
8、开机自启
cd support-files/
cp mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
9、注册服务并检测
chkconfig --add mysql
如果命令没有,在需要处理chkconfig
rpm -aq |grep chkconfig
export PATH=/sbin:$PATH
chkconfig
echo $PATH
PATH="$PATH":/sbin
echo $PATH
检测:chkconfig --list mysql
10、
vim /etc/ld.so.conf
# 添加如下内容:
/usr/local/mysql/lib
11、配置环境变量
# 配置环境变量
vim /etc/profile
# 添加如下内容:
# MYSQL ENVIRONMENT
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
# 系统重新加载
source /etc/profile
12、登录,修改密码
#启动mysql
service mysql start
#登录
mysql -uroot -p
#修改密码
alter user 'root'@'localhost' identified by 'password';
13、开启Navicat远程连接
#登录状态下
#查看用户和端口
select host, user, authentication_string, plugin from user;
#更新root信息,改为任何ip都可连接
update user set host = '%' where user = 'root';
#授权root用户可以远程登陆,报了个错
GRANT ALL ON *.* TO 'root'@'%';
ERROR 1410 (42000): You are not allowed to create a user with GRANT
# 立即生效,重新授权
flush privileges;
#重新授权
GRANT ALL ON *.* TO 'root'@'%';
#立即生效
flush privileges;
#修改密码
alter user 'root'@'%' identified with mysql_native_password by 'password';
#立即生效
flush privileges;
14、原本到此就可以顺利使用navicat连接了,结果发现连不上(报错代码10038),好吧,喜闻乐见,过程中发现一大堆问题
①如果是新买的服务器,先考虑开启外网端口
在控制台-安全组-入规则处,增加3306端口
②发现本地ping不同外网ip,再在安全组增加80端口
③关闭防火墙
#查看防火墙状态,如果是绿色则是开启
systemctl status firewalld
#暂时关闭防火墙
systemctl stop firewalld
service iptables stop
#永久关闭防火墙
systemctl disable firewalld
chkconfig iptables off
#重启防火墙
systemctl enable firewalld
service iptables restart
④结果发现还是连不上,在服务器监听3306端口,竟然发现没有被占用!
netstat -ano|grep 3306
再连接mysql,查看端口,发现竟然是0!
[root@hecs-x-medium-2-linux-20201129090029 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.20 MySQL Community Server - GPL
Copyright (c) 2000, 2020, 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> 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> show global variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 0 |
+---------------+-------+
1 row in set (0.00 sec)
mysql>
一番度娘后,发现在最初初始化mysql时,因为没有输出默认密码,在my.cnf配置文件中增加了免密登录,好吧。
skip-grant-tables
注释该配置项,service mysql restart重启mysql,连接成功!
附:mysql常用操作命令
一、 启动
1、使用 service 启动:service mysql start 或 systemctl start mysqld
2、使用 mysqld 脚本启动:/etc/inint.d/mysql start
3、使用 safe_mysqld 启动:safe_mysql&
二、停止
1、使用 service 启动:service mysql stop 或 systemctl stop mysqld
2、使用 mysqld 脚本启动:/etc/inint.d/mysql stop
3、mysqladmin shutdown
三、重启
1、使用 service 启动:service mysql restart 或 systemctl restart mysqld
2、使用 mysqld 脚本启动:/etc/inint.d/mysql restart
四、查看mysql状态
service mysqld status
查看状态时报错
#查找mysql.server
find / -name mysql.server
#复制到/etc/init.d下
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld