环境:当前使用centos7.6系统
官方提供的5.7通用二进制安装文档:https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html
安装前确保之前没有安装过mysql,如果之前安装过mysql,确保他们已完全删除。
1、创建mysql用户
1 |
useradd -s /bin/false -M mysql |
2、下载二进制包
1 2 |
cd /usr/local/src wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz |
3、安装依赖
1 |
yum install libaio |
4、解压缩设置软链
1 2 3 |
tar xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ cd /usr/local/ ln -sv mysql-5.7.24-linux-glibc2.12-x86_64/ mysql |
5、添加环境变量
1 2 3 4 |
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile echo 'export PATH=$PATH:/usr/local/mysql/bin' >> ~/.bashrc source /etc/profile source ~/.bashrc |
6、创建数据和日志目录
建议单独存在一个盘,以免后续日志增大占满磁盘空间
1 2 3 4 5 6 7 |
mkdir -p /data/mysql/{data,binlogs,log} mkdir -p /usr/local/mysql/{etc,run} ln -sv /data/mysql/data/ /usr/local/mysql/data ln -sv /data/mysql/log/ /usr/local/mysql/log ln -sv /data/mysql/binlogs/ /usr/local/mysql/binlogs chown -R mysql.mysql /data/mysql/ chown -R mysql.mysql /usr/local/mysql/{data,binlogs,log,etc,run} |
7、配置my.cnf
具体相关参数,根据个人需求进行更改,这里提供的my.cnf只是个示例
rm -f /etc/my.cnf
1 |
vi /usr/local/mysql/etc/my.cnf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
[client] port = 3306 socket = /usr/local/mysql/run/mysql.sock [mysqld] port = 3306 socket = /usr/local/mysql/run/mysql.sock pid_file = /usr/local/mysql/run/mysql.pid datadir = /usr/local/mysql/data skip-external-locking key_buffer_size = 32M max_allowed_packet = 16M table_open_cache = 1024 sort_buffer_size = 1m net_buffer_length = 8K read_buffer_size = 1m read_rnd_buffer_size = 512K myisam_sort_buffer_size = 8M thread_cache_size = 16 query_cache_size = 32M tmp_table_size = 32M performance_schema_max_table_instances = 500 explicit_defaults_for_timestamp = true #skip-networking max_connections = 500 max_connect_errors = 100 open_files_limit = 65535 slow_query_log=on long_query_time=2 slow_query_log_file=/usr/local/mysql/log/mysql_slow_query.log log-error=/usr/local/mysql/log/mysql_error.log log-bin=/usr/local/mysql/binlogs/mysql-bin binlog_format=mixed server-id = 10 expire_logs_days = 10 early-plugin-load = "" default_storage_engine = InnoDB innodb_file_per_table = 1 innodb_data_home_dir = /usr/local/mysql/data innodb_data_file_path = ibdata1:10M:autoextend innodb_log_group_home_dir = /usr/local/mysql/data innodb_buffer_pool_size = 128M innodb_log_file_size = 32M innodb_log_buffer_size = 8M innodb_flush_log_at_trx_commit = 1 innodb_lock_wait_timeout = 50 [mysqldump] quick max_allowed_packet = 16M [myisamchk] key_buffer_size = 32M sort_buffer_size = 768K read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout |
做一个my.cnf的软链
1 |
ln -sv /usr/local/mysql/etc/my.cnf /etc/my.cnf |
8、初始化数据库
1 |
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data |
初始化后会随机产生一个密码
1 2 3 4 5 |
2018-12-27T02:39:21.463563Z 0 [Warning] InnoDB: New log files created, LSN=45790 2018-12-27T02:39:21.733314Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2018-12-27T02:39:21.790741Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: a5513a42-f1ed-11e8-a3cc-005056ac76f7. 2018-12-27T02:39:21.791436Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2018-12-27T02:39:21.792521Z 1 [Note] A temporary password is generated for root@localhost: Hh3q7-d;nFym |
该密码文件保存在/data/mysql/log/mysql_error.log中
我这里的密码就是Hh3q7-d;nFym
9、生成ssl(非必须)
1 |
mysql_ssl_rsa_setup --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ |
10、设置启动服务
1 |
vi /usr/lib/systemd/system/mysqld.service |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
[Unit] Description=MySQL Server Documentation=man:mysqld(8) Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html After=network.target After=syslog.target [Install] WantedBy=multi-user.target [Service] User=mysql Group=mysql Type=forking PIDFile=/usr/local/mysql/run/mysqld.pid # Disable service start and stop timeout logic of systemd for mysqld service. TimeoutSec=0 # Execute pre and post scripts as root PermissionsStartOnly=true # Needed to create system tables #ExecStartPre=/usr/bin/mysqld_pre_systemd # Start main service ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/run/mysqld.pid $MYSQLD_OPTS # Use this to switch malloc implementation EnvironmentFile=-/etc/sysconfig/mysql # Sets open_files_limit LimitNOFILE = 65535 Restart=on-failure RestartPreventExitStatus=1 PrivateTmp=false |
11、设置开机服务启动
1 |
systemctl enable mysqld.service |
查询mysqld服务是否开机启动
1 |
systemctl is-enabled mysqld |
12、启动mysql
1 |
systemctl start mysqld |
13、重置密码,删除匿名用户,删除测试数据库
1 |
mysql_secure_installation |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
Securing the MySQL server deployment. Enter password for user root: The existing password for the user account root has expired. Please set a new password. New password: Re-enter new password: VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin? Press y|Y for Yes, any other key for No: y There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2 Using existing password for root. Estimated strength of the password: 100 Change the password for root ? ((Press y|Y for Yes, any other key for No) : n ... skipping. By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : y Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y - Dropping test database... Success. - Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y Success. All done! |
14、导入时区信息
1 |
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql |
15、最后配置日志转储(非必须)
1 |
cp /usr/local/mysql/support-files/mysql-log-rotate /etc/logrotate.d/ |
修改/etc/logrotate.d/mysql-log-rotate,以下是修改后的内容(注意要修改password为你的密码)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/usr/local/mysql/log/mysql*.log { # create 600 mysql mysql notifempty daily rotate 30 missingok compress dateext delaycompress sharedscripts postrotate /usr/local/mysql/bin/mysqladmin -uroot -p'password' flush-logs endscript } |
设置mysql-log-rotate权限
1 |
chmod 644 /etc/logrotate.d/mysql-log-rotate |
测试
1 |
/usr/sbin/logrotate -fv /etc/logrotate.d/mysql-log-rotate |
原文链接:Centos7安装mysql5.7(二进制),转载请注明来源!