我使用的是XShell7 ssh 连接工具访问的Linux,其他工具的用法和命令都一样,界面如下:
在这里我使用的不是root用户登录的,所以先切换到root用户,命令:su
cd /usr/local/
mkdir mysql
cd mysql
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
等待下载成功
tar xvJf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
mv mysql-8.0.20-linux-glibc2.12-x86_64 mysql-8.0
mkdir data
groupadd mysql
useradd -g mysql mysql
chown -R mysql.mysql /usr/local/mysql/mysql-8.0
chmod 750 /usr/local/mysql/mysql-8.0/data -R
注意:这里有一个坑,在配置完环境变量后,需要执行source /etc/profile命令,使配置文件生效
vim /etc/profile
#--------------------------
#点击键盘上的i键,进入输入模式,在配置文件最后增加一行:
#--------------------------
export PATH=$PATH:/usr/local/mysql/mysql-8.0/bin:/usr/local/mysql/mysql-8.0/lib
#添加完成后,按Esc键退出输入模式,进入命令模式,输入:wq,保存并退出
#然后在输入cat /etc/profile 查看是否保存成功,效果如下:↓↓↓↓↓↓
11. 编辑my.cnf文件
vim /etc/my.cnf
[mysql]
default-character-set=utf8mb4
[client]
#port=3306
socket=/var/lib/mysql/mysql.sock
[mysqld]
#port=3306
#server-id=3306
user=mysql
general_log = 1
general_log_file= /var/log/mysql/mysql.log
socket=/var/lib/mysql/mysql.sock
basedir=/usr/local/mysql/mysql-8.0
datadir=/usr/local/mysql/mysql-8.0/data
log-bin=/usr/local/mysql/mysql-8.0/data/mysql-bin
innodb_data_home_dir=/usr/local/mysql/mysql-8.0/data
innodb_log_group_home_dir=/usr/local/mysql/mysql-8.0/data/
character-set-server=utf8mb4
lower_case_table_names=1
autocommit=1
default_authentication_plugin=mysql_native_password
symbolic-links=0
# Disabling symbolic-links is recommended to prevent assorted security risks
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/usr/local/mysql/mysql-8.0/data/mysql.log
pid-file=/usr/local/mysql/mysql-8.0/data/mysql.pid
#
# include all files from the config directory
cd bin
./mysqld --user=mysql --basedir=/usr/local/mysql/mysql-8.0 --datadir=/usr/local/mysql//mysql-8.0/data/ --initialize
#初始化完成后,复制最后的初始密码,保存下来后面需要(这一步好像并没有用)
#igTn=3j-
cp -a ./support-files/mysql.server /etc/init.d/mysql
cp -a ./support-files/mysql.server /etc/init.d/mysqld
chown 777 /etc/my.cnf
chmod +x /etc/init.d/mysql
chmod +x /etc/init.d/mysqld
mkdir /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql/
service mysql start
#出现SUCCESS 说明启动成功,效果图如下:
ps:
如果发现启动失败,可以先查看data里面的mysql.log,如果有提示3306这样的字样,第一个想到的应该是端口被占用,
使用lsof -i:端口号,查看是哪个PID(进程号)占用了该端口,然后使用kill PID(进程号)杀掉进程,重新执行service mysql start
在这一步最开始就浪费了我好大的时间。如果用之前的初始密码登录,可能会出现以下的错误:
在这一步的时候,研究了好久,记录一下处理过程:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';
flush privileges;#刷新权限
use mysql;
mysql> create user 'root'@'%' identified by 'password';
Query OK, 0 rows affected (0.03 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.00 sec)
flush privileges;#刷新权限
1.填写IP、端口连接mysql服务
注:若连接不到可以查看mysql端口是否开放,若未开放则开放端口继续连接,最终测试成功!!!