centos上mysql-8.0.28数据库安装
mysql目录为:/usr/local/mysql (仅举例,已安装则可不必再装) 下载地址https://dev.mysql.com/downloads/mysql/8.0.html?os=2
第一步:将MySQL安装包mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz放到/usr/local/src目录下,并解压缩
cd /usr/local/src
xz -d mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz
tar -xvf mysql-8.0.28-linux-glibc2.12-x86_64.tar
第二步:在/usr/local下创建安装目录mysql,并将/usr/local/src下的mysql-8.0.28-linux-glibc2.12-x86_64文件夹的内容移动到/usr/local/mysql下
mkdir /usr/local/msyql
mv /usr/local/src/mysql-8.0.28-linux-glibc2.12-x86_64/* /usr/local/mysql/
第三步:切换到/usr/local/mysql目录,创建目录data
cd /usr/local/mysql
mkdir /usr/local/mysql/data
第四步:分别创建用户组以及用户和密码(如果提示已存在说明之前有创建过了)
groupadd mysql
useradd -g mysql mysql
第五步:授权刚刚新建的用户
chown -R mysql.mysql /usr/local/mysql
chmod 750 /usr/local/mysql/data -R
第六步:配置环境,编辑/etc/profile文件
vim /etc/profile
加入环境变量
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
执行命令使配置生效
source /etc/profile
第七步:编辑my.cnf文件
vi /etc/my.cnf
按下面复制进去就好了,#号开头的不用管,一样,添加完之后,换行,打出“:sq”,用来保存文件并退出
[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=/usr/local/mysql/data/mysql.log
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock
log-bin=/usr/local/mysql/data/mysql-bin
innodb_data_home_dir=/usr/local/mysql/data
innodb_log_group_home_dir=/usr/local/mysql/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
symbolic-links=0
# 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/data/mysql.log
pid-file=/usr/local/mysql/data/mysql.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
第八步:切换到/usr/local/mysql/bin目录下,初始化基础信息,得到数据库的初始密码(在/usr/local/mysql/bin目录下执行)
cd bin
./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ --initialize
如图,复制出红框的初始密码,保存到本地,后面需要使用到
第九步:复制mysql.server 文件
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
cp /usr/local/mysql/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
第十一步:检查一下/var/lib/mysql是否存在,否则进行创建
mkdir /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql/
第十二步:启动数据库,有SUCCESS字眼说明MySQL安装完成
service mysql start
第十三步:修改root账号密码
登录myql:mysql -hlocalhost -uroot -p
录入上边初始化后的密码(第八步)
执行下面语句,修改root默认密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';
flush privileges;
注意:必须修改,不修改不让执行其他操作
可设置root用户远程可访问,先执行
use mysql
再执行以下命令
update user set host='%' where user='root';
flush privileges;