Linux版本:CentOS7
MySQL版本:mysql-8.0.31 https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
1、本地安装包上传到服务器的/usr/local目录下
scp /Users/yj-333/Desktop/mysql-8.0.31.tar.xz [email protected]:/usr/local/mysql-8.0.31.tar.xz
2、添加gmysql用户组,umysql用户(当前用户root)
#> groupadd gmysql
#> useradd -g gmysql umysql
3、解压tar.xz文件(当前用户root)
#> tar -xvf mysql-8.0.31.tar.xz #源文件500多M,但是解压后只有1/5左右
#> mv mysql-8.0.31-linux-glibc2.12-x86_64 mysql8 #修改名称
4、创建data文件夹(当前用户root)
#> cd /usr/local/mysql8
#> mkdir data
5、修改文件夹的归属用户(当前用户root)
#> chown -R umysql:gmysql /usr/local/mysql8
6、配置环境,编辑/etc/profile文件(当前用户root)
#> vim /etc/profile
在文件最后添加:export PATH=$PATH:/usr/local/mysql8/bin:/usr/local/mysql8/lib
#> source /etc/profile
7、编辑/etc/my.cnf文件(当前用户root)
[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/mysql8
datadir=/usr/local/mysql8/data
log-bin=/usr/local/mysql8/data/mysql-bin
innodb_data_home_dir=/usr/local/mysql8/data
innodb_log_group_home_dir=/usr/local/mysql8/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/mysql8/data/mysql.log
pid-file=/usr/local/mysql8/data/mysql.pid
#
# include all files from the config directory
8、修改、拷贝/ect/init.d/mysql中的datadir、basedir(当前用户root)
#> vim /usr/local/mysql8/support-files/mysql.server
#> basedir=/usr/local/mysql8
#> datadir=/usr/local/mysql8/data
#> 拷贝服务端工具到init.d目录下:
#> cp -a /usr/local/mysql8/support-files/mysql.server /etc/init.d/mysql
#> cp -a /usr/local/mysql8/support-files/mysql.server /etc/init.d/mysqld
9、切换到umysql用户(当前用户root)
#> su umysql
10、初始化数据,执行过后将在/usr/local/mysql8/data下产生数据(当前用户umysql)
#> ./mysqld --user=mysql --basedir=/usr/local/mysql8 --datadir=/usr/local/mysql8/data/ --initialize
此步骤将会产生密码,需要把密码记录下来。
11、修改/etc/my.cnf文件权限(执行前用户切换为root)
#> chmod 644 /etc/my.cnf
#> chmod +x /etc/init.d/mysql
#> chmod +x /etc/init.d/mysqld
12、查看/var/lib/mysql是否存在,否则进行创建(当前用户root)
#> mkdir /var/lib/mysql
#> chown -R umysql:gmysql /var/lib/mysql/
# > su umysql
13、启动MySQL(当前用户umysql)
#> service mysql start
14、修改账户信息(当前用户umysql)
#> mysql -u root -p
#> 输入初始化时候的密码
#> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';
#> flush privileges;
#> use mysql;
#> create user 'root'@'%' identified by 'password';
#> grant all privileges on *.* to 'root'@'%' with grant option;
#> flush privileges;
15、防火墙中打开3306端口,让外界可以访问到(当前用户root)
#> firewall-cmd --add-port=3306/tcp --permanent
#> firewall-cmd --reload
16、问题总结:
a、如果存在问题,需要重新安装,直接删除掉/usr/local/mysql8/data文件夹即可,然后在重新初始化数据(当前用户umysql)
#> cd /usr/local/mysql8
#> rm -rf data
#> ./mysqld --user=mysql --basedir=/usr/local/mysql8 --datadir=/usr/local/mysql8/data/ --initialize
b、启动MySQL的时候提示Warning: World-writable config file '/etc/my.cnf' is ignored。大概意思是权限全局可写,任何一个用户都可以写。mysql担心这种文件被其他用户恶意修改,所以忽略掉这个配置文件。这样mysql无法关闭。(当前用户root)
#> chmod 644 /etc/my.cnf
c、mysql -u root -p登录的时候报错:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)(当前用户root)
可能是/etc/my.cnf文件中的socket字段赋值错误。
解决mysql.sock连接不上问题:Can 't connect to local MySQL server through socket '/tmp/mysql.sock '(2)_雨客cs的博客-CSDN博客
d、启动数据库报错service mysql start:Starting MySQL. ERROR! The server quit without updating PID file (/usr/local/mysql8/data/localhost.localdomain.pid).
可能是用错了用户,需要切换为umysql用户。
注意kill掉的时候需要用root权限,启动的时候用umysql权限。