下载mysql并放到Linux系统
官网下载linux系统下的mysql,地址:mysql官网
我的Linux虚拟机是CentOS的,所以选择的是Linux通用版本的MySQL
通过xftp工具,将mysql压缩包放到linux系统的 /opt目录下:
解压包:
tar -xvf mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz
将解压好的文件复制并放在 /usr/local路径下,重命名为mysql:
cp -r mysql-8.0.13-linux-glibc2.12-x86_64 /usr/local/mysql
配置环境变量
执行命令:
vi /etc/profile
添加系统的环境变量:
export PATH=$PATH:/usr/local/mysql/bin
执行下列命令使配置文件生效:
source /etc/profile
创建mysql用户组和mysql用户
groupadd mysql
useradd -r -g mysql mysql
授权
注意,一定要进入mysql目录下进行授权操作
chown -R mysql:mysql ./
修改mysql用户密码
passwd mysql
初始化数据库
他会生成一个随机密码,这里需要记一下,一会登录时使用
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
修改data文件的用户组:
chown [选项]… [所有者][:[组]] 文件…
chown -R root:root ./
chown -R mysql:mysql data
创建my-default.cnf 文件
进入到mysql中的support-files文件,并创建my-default.cnf文件
touch my-default.cnf
为该文件授权:
chmod 777 ./my-default.cnf
将该文件复制到指定目录
cp support-files/my-default.cnf /etc/my.cnf
配置my.cnf文件
vim /etc/my.cnf
将下面代码放到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
tmpdir = /tmp
port = 5186
#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
注册服务
注意当前路径应该在mysql8.0,即support-files的根目录)
添加Mysql到系统服务
cp -a ./support-files/mysql.server /etc/init.d/mysql # 若mysqld,以下mysql相应的修改mysqld,如下图所示
chmod +x /etc/init.d/mysql
chkconfig --add mysql
检查服务是否生效
chkconfig --list mysql
etc/ld.so.conf配置路径
vim /etc/ld.so.conf
添加如下内容:
/usr/local/mysql/lib
登录
启动mysql服务
service mysql start
查看启动状态:
service mysql status;
登录mysql
mysql -uroot -p
在这里输入刚才生成的初始密码
修改密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';