注:安装任务尽量选择小版本最新版本安装,避免出现后期需要安全升级
本机环境:
虚拟机:CentOS 7.5 x64
rpm -qa |grep mariadb #或 yum list installed | grep mariadb
image
卸载mariadb包(不清理会报错)
rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64 # 或 yum -y remove mariadb-libs.x86_64
下载或上传解压MySQL-RPM包
#联网下载 wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.38-1.el7.x86_64.rpm-bundle.tar #解压 tar -xvf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
mysql-community-client:MySQL客户端应用程序和工具 mysql-community-common:服务器和客户端库的通用文件 mysql-community-devel:用于MySQL数据库客户端应用程序的开发头文件和库 mysql-community-embedded:MySQL嵌入式库 mysql-community-embedded-compat:MySQL服务器作为嵌入式库,与使用库版本18的应用程序兼容 mysql-community-embedded-devel:MySQL的开发标头文件和库作为可嵌入库 mysql-community-libs:MySQL数据库客户端应用程序的共享库 mysql-community-libs-compat:以前的MySQL安装的共享兼容性库 mysql-community-server:数据库服务器和相关工具 mysql-community-test:MySQL服务器的测试套件
安装MySQL
按照common→libs→client→server→libs-compat→devel的顺序安装,否则安装过程会提醒依赖顺序,其他的为非必要安装包
rpm -ivh mysql-community-common-5.7.38-1.el7.x86_64.rpm.rpm rpm -ivh mysql-community-libs-5.7.38-1.el7.x86_64.rpm rpm -ivh mysql-community-client-5.7.38-1.el7.x86_64.rpm rpm -ivh mysql-community-server-5.7.38-1.el7.x86_64.rpm //上四包必装,libs-compat和devel非必装 rpm -ivh mysql-community-libs-compat-5.7.38-1.el7.x86_64.rpm rpm -ivh mysql-community-devel-5.7.38-1.el7.x86_64.rpm
客户端程序和脚本:/usr/bin
配置文件:/etc/my.cnf
数据目录路径:/var/lib/mysql
错误日志文件:/var/log/mysqld.log
PID文件:/var/run/mysql/mysqld.pid
Socket文件:/var/lib/mysql/mysql.sock
手册页:/usr/share/man
包含(头)文件:/usr/include/mysql
lib库文件:/usr/lib/mysql
其他支持文件(例如,错误消息和字符集文件):/usr/share/mysql
服务启动脚本:/usr/lib/systemd/system/mysqld.service
启动服务并查看状态
## 启动服务 systemctl start mysqld.service ## 查看服务状态 systemctl status mysqld.service
到这里安装结束 下面开始配置MySQL
查看密码(使用YUM及RPM包安装时会产生一个默认的密码,密码一般存在于日志文件/var/log/mysqld.log上)
grep 'temporary password' /var/log/mysqld.log
登录
mysql -uroot -p # 输入 刚才的原始密码
修改密码(第一次登录必须修改密码)
#修改密码安全策略 (可选 不设置这两个参数不能设置简单密码) #修改validate_password_policy参数 密码安全策略 # 0 or LOW 校验长度 # 1 or MEDIUM 校验长度数字、小写/大写和特殊字符 # 2 or STRONG 长度数字、小写/大写和特殊字符、字典文件 set global validate_password_policy=0; #validate_password_length(密码长度)参数默认为8,我们修改为1 set global validate_password_length=0; #修改密码 alter user 'root'@'localhost' identified by 'root';
mysql_secure_installation
询问修改root密码 (设置过,不修改)
默认情况下,MySQL安装有一个匿名用户,匿名用户允许任何人登录到MySQL,不必再创建用户帐户。这只是为了方便测试使用。正式生产环境中建议将其删除。
移除匿名用户?输入 y 继
一般情况下,root用户只允许localhost(即本机)登录,以此确保某些人无法猜测root网络远程密码。
禁用 root 远程登录?不禁用 输入 0 继续 (后续可以通过命令行开启关闭root远程登录)
默认情况下,MySQL带有一个名为“ test”的数据库,该数据库任何人都可以访问。 这也仅用于测试。正式生产环境中建议将其删除。
重新加载特权表将确保所有更改可以立即生效
完成
远程访问设置
mysql -uroot -p #输入上述步骤中设置的root本地登录密码“root”登录数据库 use mysql; CREATE USER 'user'@'localhost' IDENTIFIED BY '1234'; GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' identified by "1234" WITH GRANT OPTION; FLUSH PRIVILEGES;
更改数据存放目录
#创建目录 mkdir -p /data/mysql #关闭MySQL服务 systemctl stop mysqld.service 移动数据到数据存放目录 cd /var/lib/mysql mv * /data/mysql/ #修改数据目录属性及权限 chown -R mysql.mysql /data/mysql chmod 0700 /data/mysql
配置my.cnf
## 备份默认生成的my.cnf mv /etc/my.cnf /etc/my.cnf.bak ## 新建my.cnf文件 vi /etc/my.cnf ##输入以下配置内容 [client] port = 3306 socket = /data/mysql/mysql.sock default-character-set = utf8mb4 [mysqld] port = 3306 socket = /data/mysql/mysql.sock datadir = /data/mysql character-set-server = utf8mb4 collation-server = utf8mb4_general_ci init_connect = 'SET NAMES utf8mb4' server-id = 1 log-slave-updates=true skip-external-locking skip-name-resolve back_log = 300 table_open_cache = 128 max_allowed_packet =16M read_buffer_size = 8M read_rnd_buffer_size = 64M sort_buffer_size = 16M join_buffer_size = 8M key_buffer_size = 128M thread_cache_size = 16 log-bin = mysql-bin binlog_format = row ######主从 log-slave-updates = true ######慢日志 slow_query_log = on long_query_time = 1 slow_query_log_file = /data/mysql/db-slow.log gtid_mode = ON enforce_gtid_consistency = ON expire_logs_days = 7 default_storage_engine = InnoDB innodb_buffer_pool_size = 1G innodb_data_file_path = ibdata1:10M:autoextend innodb_file_per_table = on innodb_write_io_threads = 4 innodb_read_io_threads = 4 innodb_thread_concurrency = 8 innodb_purge_threads = 1 innodb_flush_log_at_trx_commit = 1 innodb_log_buffer_size = 8M innodb_log_file_size = 512M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 60 max_connections = 5000 interactive_timeout = 28800 wait_timeout = 28800 sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION symbolic-links=0 ######时间参数 log_timestamps=SYSTEM [mysqldump] quick max_allowed_packet = 32M [mysql] no-auto-rehash default-character-set = utf8mb4 [myisamchk] key_buffer_size = 64M sort_buffer_size = 64M read_buffer = 8M write_buffer = 8M [mysqlhotcopy] interactive-timeout [mysqld_safe] log-error = /data/mysql/mysql_err.log pid-file = /data/mysql/mysqld.pid
PID文件位置需修改
/usr/lib/systemd/system/mysqld.service 启动脚本内位置(修改后需执行 systemctl daemon-reload),否则配置文件内pid-file选项无法生效。
vi /usr/lib/systemd/system/mysqld.service
## 修改后执行如下命令,重新加载服务配置文件 systemctl daemon-reload ## 启动MySQL服务 systemctl start mysqld.service
mysql -uroot -p ## 创建test库,指定字符集及排序规则 CREATE DATABASE test DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; ##查看 show databases;
mysql -uroot -p ## 切换至mysql库 use mysql; ##修改密码策略 set global validate_password_policy=0; set global validate_password_length=0; ## 创建用户 CREATE USER 'test'@'%' IDENTIFIED BY '1234'; ## 授权 GRANT select,insert,update,delete,create on test.* to 'test'@'%' ; FLUSH PRIVILEGES; ## 查看用户 use mysql; select user,host from user; ## 查看test用户权限 show GRANTS for 'test'@'%';
启动:systemctl start mysqld.service 查看状态:systemctl status mysqld.service 关闭:systemctl stop mysqld.service 重启:systemctl restart mysqld.service 设置开机启动:systemctl enable mysqld.service