1. 卸载旧 MySQL
- 查看 rpm 包
rpm-qa | grep mysql
如果存在,使用如下命令卸载
rpm -e
- 查找是否存在
mysql
相关目录
find / -name mysql
- 卸载系统自带 mariadb
[root@localhost ~]# rpm -qa|grep mariadb mariadb-libs-5.5.68-1.el7.x86_64 mariadb-libs-5.5.64-1.el7.x86_64 [root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64 mariadb-libs-5.5.64-1.el7.x86_64 [root@localhost ~]# rm -rf /etc/my.cnf
2. 创建 mysql 用户和组
- 检查有无创建过 mysql 用户组,如果没有创建
# 检查mysql 用户组是否存在 [root@localhost ~]# cat /etc/group | grep mysql [root@localhost ~]# cat /etc/passwd | grep mysql # 创建mysql 用户组和用户 [root@localhost ~]# groupadd mysql [root@localhost ~]# useradd -r -g mysql mysql
3. 安装 MySQL
- 下载
从官网下载 MySQL
https://dev.mysql.com/downloads/mysql/
使用如下命令下载 MySQL
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
下载下来是一个gz
的压缩包,使用如下命令解压
tar -zxvf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
将解压的文件重命名,并移动到/usr/local
目录
mv mysql-5.7.36-linux-glibc2.12-x86_64 /usr/local/mysql5.7
更改 mysql5.7 目录下所有文件夹所属的用户、用户组、以及权限
chown -R mysql:mysql /usr/local/mysql5.7 chmod -R 755 /usr/local/mysql5.7
- 创建 MySQL 相关目录
mkdir -p /usr/local/mysql5.7/{data,logs,tmp} # 更改文件夹所属 chown -R mysql.mysql /usr/local/mysql5.7/data chown -R mysql.mysql /usr/local/mysql5.7/logs chown -R mysql.mysql /usr/local/mysql5.7/tmp
- 创建 MySQL 配置文件 my.cnf
简单模板如下
[client] port = 3306 socket = /usr/local/mysql5.7/tmp/mysql.sock [mysqld] user = mysql basedir = /usr/local/mysql5.7 datadir = /usr/local/mysql5.7/data port = 3306 socket = /usr/local/mysql5.7/tmp/mysql.sock pid-file = /usr/local/mysql5.7/tmp/mysqld.pid tmpdir = /usr/local/mysql5.7/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 = /usr/local/mysql5.7/logs/error.log slow_query_log = 1 slow_query_log_file = /usr/local/mysql5.7/logs/slow.log long_query_time = 3 log-bin = /usr/local/mysql5.7/logs/binlog binlog_format = row expire_logs_days = 15 log_bin_trust_function_creators = 1 relay-log = /usr/local/mysql5.7/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
- 配置
mysql.server
cd /usr/local/mysql5.7/support-files cp mysql.server /etc/init.d/mysql vi /etc/init.d/mysql # 修改目录位置 basedir=/usr/local/mysql5.7 datadir=/usr/local/mysql5.7/data # 注册开机启动服务 chkconfig --add mysql chkconfig --list
- 添加 mysql 到环境变量
把/usr/local/mysql5.7/bin
添加到PATH环境变量中
添加的内容export PATH=$PATH:/usr/local/mysql5.7/bin
vi /etc/profile source /etc/profile
- 初始化 mysql
[root@localhost ~]# /usr/local/mysql5.7/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql5.7 --datadir=/usr/local/mysql5.7/data # 临时密码保存在 errlog 中;获取临时密码 [root@localhost ~]# more /usr/local/mysql5.7/logs/error.log | grep password 2022-04-07T23:28:28.894994+08:00 1 [Note] A temporary password is generated for root@localhost: w(fYri(tk8Dj
- 启动 mysql 服务,并修改
root
用户密码
# 启动mysql服务 [root@localhost ~]# service mysql start Starting MySQL... SUCCESS! # 使用初始密码登录mysql服务 并修改密码 [root@localhost ~]# mysql -uroot -p alter user 'root'@'localhost' identified by 'root'; flush privileges;
为了方便使用,可以创建软链接
ln -s /usr/local/mysql5.7/bin/mysql /usr/bin/mysql
4. 附:常用命令与开启远程连接
- 启动
service mysql start
- 关闭
service mysql stop
- 重启
service mysql restart
- 开启远程连接
进入 MySQL 服务
mysql -u root -proot
操作 mysql 库
mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select user, host from user; +---------------+-----------+ | user | host | +---------------+-----------+ | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +---------------+-----------+ 3 rows in set (0.00 sec) mysql>
host 字段中,localhost 表示只允许本机访问;要实现远程连接,可以将 root 用户的 hos t改为 %,% 表示允许任意 host 访问;如果需要设置只允许特定 ip 访问,则应改为对应的 ip。
修改 root 用户的 host 字段
update user set host='%' where user='root'; flush privileges;
让 root 用户可以远程连接,存在安全风险;可以采用如下方案:创建独立的用户。
GRANT ALL PRIVILEGES ON *.* TO 'deployop'@'%' IDENTIFIED BY 'deployop' WITH GRANT OPTION;
第一个 deployop 表示用户名,% 表示所有的电脑都可以连接;也可以设置某个 ip 地址运行连接。第二个 deployop 表示密码。
再次select user, host from user;
可查看到 deployop 用户的 host 是 % ;代表任何 ip 都可以连接。然后使用flush privileges;
语句立即生效。
如果还是连接不上,就有可能是防火墙的问题。添加 3306 端口防火墙策略,重启防火墙。