Centos7安装mysql5.7
1、查看rpm包
rpm-qa|grep mysql 若有可用 rpm-e卸载
查找是否有mysql:find / -name mysql
检测系统是否自带mysql
rpm -qa | grep mysql
如果是,则使用下面命令进行删除:
rpm -e --nodeps ‘上一步查找的名称’
删除成功后,查询所有Mysql对应的文件夹
whereis mysql
find / -name mysql
删除上面查找的所有文件夹
2、卸载CentOS7系统自带mariadb
# 查看系统自带的Mariadb # rpm -qa|grep mariadb mariadb-libs-5.5.44-2.el7.centos.x86_64 # 卸载系统自带的Mariadb # rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64 # 删除etc目录下的my.cnf ,一定要删掉,等下再重新建,之前我将就用这个文件,后面改配置各种不生效 # rm /etc/my.cnf
3、下载安装,从官网安装下载mysql包:
cd /usr/local/ -- 进入local目录,下载mysql tar.gz包 1、# wget下载或者本地下载后上传 wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.35-el7-x86_64.tar.gz 2、官网下载: https://dev.mysql.com/downloads/mysql/ 3、解压安装mysql 自行下载解压 tar -zxvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz 4、将解压的文件做文件改名 mv mysql-5.7.31-linux-glibc2.12-x86_64 mysql
4、检查有无安装过mysql 用户组,没有的话创建
//检查mysql 用户组是否存在 cat /etc/group | grep mysql cat /etc/passwd |grep mysql // 创建mysql 用户组和用户 groupadd mysql useradd -r -g mysql mysql 更改mysql 目录下所有文件夹所属的用户组和用户,以及权限 chown -R mysql:mysql /usr/local/mysql chmod -R 777 /usr/local/mysql
最高权限
5、创建mysql ,安装
1、创建mysql 相关目录 mkdir -p /data/mysql/{data,logs,tmp} # 更改文件夹所属 chown -R mysql.mysql /data/mysql/ 2、编辑my.cnf文件:vi /etc/my.cnf # 简单模板如下: [client] port = 3306 socket = /data/mysql/tmp/mysql.sock [mysqld] user = mysql basedir = /usr/local/mysql datadir = /data/mysql/data port = 3306 socket = /data/mysql/tmp/mysql.sock pid-file = /data/mysql/tmp/mysqld.pid tmpdir = /data/mysql/tmp skip_name_resolve = 1 symbolic-links=0 max_connections = 2000 group_concat_max_len = 1024000 sql_mode = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION lower_case_table_names = 1 log_timestamps=SYSTEM character-set-server = utf8 interactive_timeout = 1800 wait_timeout = 1800 max_allowed_packet = 32M binlog_cache_size = 4M sort_buffer_size = 2M read_buffer_size = 4M join_buffer_size = 4M tmp_table_size = 96M max_heap_table_size = 96M max_length_for_sort_data = 8096 #logs server-id = 1003306 log-error = /data/mysql/logs/error.log slow_query_log = 1 slow_query_log_file = /data/mysql/logs/slow.log long_query_time = 3 log-bin = /data/mysql/logs/binlog binlog_format = row expire_logs_days = 15 log_bin_trust_function_creators = 1 relay-log = /data/mysql/logs/relay-bin relay-log-recovery = 1 relay_log_purge = 1 #innodb innodb_file_per_table = 1 innodb_log_buffer_size = 16M innodb_log_file_size = 256M innodb_log_files_in_group = 2 innodb_io_capacity = 2000 innodb_io_capacity_max = 4000 innodb_flush_neighbors = 0 innodb_flush_method = O_DIRECT innodb_autoinc_lock_mode = 2 innodb_read_io_threads = 8 innodb_write_io_threads = 8 innodb_buffer_pool_size = 2G 3、配置mysql.server cd /usr/local/mysql/support-files cp mysql.server /etc/init.d/mysql vi /etc/init.d/mysql # 修改目录位置 basedir=/usr/local/mysql datadir=/data/mysql/data # 注册开机启动服务 chkconfig --add mysql chkconfig --list 4、添加环境变量 echo "PATH=$PATH:/usr/local/mysql/bin " >> /etc/profile source /etc/profile 5、初始化mysql /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data # 临时密码保存在errlog中 # 获取临时密码 more /data/mysql/logs/error.log |grep password
6、启动服务,修改密码,设置外网可链接
1、启动mysql服务,并修改密码 # 启动mysql服务 service mysql start # 使用初始密码登录mysql服务 并修改密码 mysql -uroot -p alter user 'root'@'localhost' identified by '123456'; flush privileges; 2、使用前面随机生成的密码登录mysql数据库,再重置密码; mysql -u root -p(回车后输入前面的随机密码) ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; flush privileges; 3、开启mysql的远程访问权限 use mysql; create user 'root'@'%' identified by '123456'; grant all on *.* to 'root'@'%' with grant option; flush privileges; 4、退出 exit
7、重启服务
service mysql stop
service mysql start
8、防火墙放行
1、查看防火墙状态 firewall-cmd --state 如果没有开启,则开启 开启防火墙: systemctl start firewalld.service 2、查看监听的端口: firewall-cmd --list-port 3、添加3306端口监听 firewall-cmd --zone=public --add-port=3306/tcp --permanent 4、刷新防火墙设置 firewall-cmd --reload
9、mysql自启动
1.检查mysql是否自启动 enabled是 disabled不是 systemctl is-enabled mysql 2.设置自启动 systemctl enable mysql