搞了一台云服务器,首先要干的活就是得安装数据库,在Windows下安装不用说,傻瓜式操作,在Linux上安装少说要记录一下。
我使用的是XShell7 ssh 连接工具访问的Linux,其他工具的用法和命令都一样,界面如下:
在这里我使用的不是root用户登录的,所以先切换到root用户,命令:su
#创建mysql的工作目录并切换过去
cd /usr/local/
mkdir mysql
cd mysql
wget的方式
#下载安装包
yum install -y wget
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
百度网盘
链接:https://pan.baidu.com/s/1N1oeM6BN9U1ptvMUT9vVzA
提取码:yyds
--来自百度网盘超级会员V5的分享
1.解压
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
2.创建数据文件夹
cd mysql-8.0
mkdir data
3.创建mysql系统用户
groupadd mysql
useradd -g mysql mysql
4.授权刚刚新建的用户
chown -R mysql.mysql /usr/local/mysql/mysql-8.0
chmod 750 /usr/local/mysql/mysql-8.0/data -R
5.修改环境变量
#配置环境,编辑/etc/profile.d/my_en.sh文件
vi /etc/profile.d/my_en.sh
export PATH=$PATH:/usr/local/mysql/mysql-8.0/bin:/usr/local/mysql/mysql-8.0/lib
source /etc/profile.d/my_en.sh
6.编辑my.cnf文件
vi /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
7.切换到/usr/local/mysql/mysql-8.0/bin目录下
cd /usr/local/mysql/mysql-8.0/bin
8.初始化基础信息,得到数据库的初始密码(在/usr/local/mysql/mysql-8.0/bin目录下执行)
#初始化完成后,复制最后的初始密码,保存下来后面需要
./mysqld --user=mysql --basedir=/usr/local/mysql/mysql-8.0 --datadir=/usr/local/mysql/mysql-8.0/data/ --initialize
9.复制 mysql.server 文件,在/usr/local/mysql/mysql-8.0目录下执行
cd /usr/local/mysql/mysql-8.0
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
10.检查一下/var/lib/mysql是否存在,否则进行创建
mkdir /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql/
11.启动数据库,有SUCCESS字眼说明MySQL安装完成
#出现SUCCESS 说明启动成功,效果图如下:
service mysql start
如果发现启动失败,可以先查看data里面的mysql.log,如果有提示3306这样的字样,第一个想到的应该是端口被占用,使用lsof -i:端口号,查看是哪个PID(进程号)占用了该端口,然后使用kill PID(进程号)杀掉进程,重新执行service mysql start
1.登录mysql
#加'的原因是之前生成的密码有/,会出现错误,如果没有斜杠那么直接可以使用mysql -uroot -pf9zESr/F!mo8
mysql -uroot -p'f9zESr/F!mo8'
2.修改数据库root账号的密码,并设置可以远程访问
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'Admin2022';
如果执行命令以后报错
执行下面的命令可以解决
ALTER USER USER() IDENTIFIED BY 'Admin2022!';
grant all privileges on *.* to 'root'@'%' with grant option;
FLUSH PRIVILEGES;
EXIT
#测试登录
mysql -uroot -pAdmin2022!
3.创建新用户远程登录命令
#创建用户授权流程
CREATE USER 'test'@'%' IDENTIFIED BY 'Mysql@123' WITH MAX_USER_CONNECTIONS 1000;
grant all privileges on *.* to 'test'@'%' with grant option;
FLUSH PRIVILEGES;
EXIT
#测试登录
mysql -utest -pMysql@123