centos7 rpm安装mysql(自定义datadir)
之前写过一个linux安装centos7过程,感觉写的不太完善,现在更新一个新版本的
安装条件
centos7 rpm安装mysql
参考资料
下载地址
安装过程
请严格按照步骤执行,否则可能入坑
解压
cd /opt/ tar xvf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
清除已安装版本
rpm -qa|grep mysql sudo yum remove mysql-libs rpm -qa|grep mariadb sudo yum remove mariadb-libs
按照以下顺序安装
sudo rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm sudo rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm sudo rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm sudo rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
做如下改动才能更换datadir
chown -R mysql.mysql /var/lib/mysql #临时关闭SELinux setenforce 0 #永久关闭SELinux vi /etc/selinux/config SELINUX=enforcing改为disabled
准备目录以及配置文件
cd /var/lib rm -rf mysql ln -s /opt/mysql/data ./mysql cd ./mysql # 清空data目录 rm -f * mkdir /opt/mysql mkdir /opt/mysql/data mkdir /opt/mysql/logs mkdir /opt/mysql_bak vi /etc/my.cnf
配置文件以及对应解释如下
# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [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 # # 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 # 不要改位置,建立软连接也算改位置需要关闭SELinux datadir=/var/lib/mysql # Mysql有两种连接方式: #(1)TCP/IP #(2)socket # 对mysql.sock来说,其作用是 程序与mysqlserver处于同一台机器,发起本地连接时可用。 socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 max_connections=10000 max_connect_errors=10000 default-time-zone = '+8:00' # 错误日志,不要改位置,否则很麻烦 log-error=/var/log/mysqld.log # MySQL pid 文件记录的是当前 mysqld 进程的 pid,pid 亦即 Process ID pid-file=/var/run/mysqld/mysqld.pid character-set-server=utf8 [mysql] default-character-set=utf8 [client] default-character-set=utf8
启动mysql命令(root用户)
# 启动mysql sudo systemctl start mysqld.service # 查看mysql状态 sudo systemctl status mysqld.service # 关闭mysql sudo systemctl stop mysqld.service # 重启mysql sudo service mysqld restart
设置root密码
[root@crpt-db data]# grep 'temporary password' /var/log/mysqld.log 2020-12-23T07:41:24.636462Z 1 [Note] A temporary password is generated for root@localhost: i*Ll(hjuw9Ho 2020-12-23T07:43:14.334119Z 1 [Note] A temporary password is generated for root@localhost: qKaLo<duo7l; 2020-12-23T07:46:52.810917Z 1 [Note] A temporary password is generated for root@localhost: :IFry3u<:IXr 2020-12-23T08:11:27.584015Z 1 [Note] A temporary password is generated for root@localhost: *Z.U#ojok9wt 2020-12-23T08:39:16.967025Z 1 [Note] A temporary password is generated for root@localhost: h-N8+Smgsj+f
h-N8+Smgsj+f为密码
mysql -uroot -p 输入密码即可登录 alter user root@localhost identified by 'yourpwd'; grant all privileges on *.* to 'root'@'%' identified by 'yourpwd'; flush privileges;