1.创建目录
mkdir -p /opt/soft
cd/opt/soft
2.下载并解压
wget "https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz"
tar -zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz -C /opt/
3.创建软连接和必要目录
ln -s /opt/mysql-5.7.20-linux-glibc2.12-x86_64 /opt/mysql
mkdir /opt/mysql-5.7.20-linux-glibc2.12-x86_64/data
mkdir /opt/mysql/log
touch /opt/mysql/log/mysql-error.log
#添加用户组mysql
groupadd mysql
#新建msyql用户,-r为系统用户,并禁止登录shell
useradd -r -s /sbin/nologin -g mysql mysql -d /opt/mysql/
#修改权限
chown -R mysql:mysql /opt/mysql/
5.初始化数据库
/opt/mysql/bin/mysql_install_db --user=mysql --basedir=/opt/mysql/ --datadir=/opt/mysql/data/
遇到问题:/opt/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
执行
yum install libaio-devel
6.mysql5.7的包里没有默认的my.cnf,因此需要自己建一个
备份系统自带my.cnf
mv /etc/my.cnf /etc/my.cnf.bak
新建
vi /etc/my.cnf
录入内容
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8
[mysqld]
server_id=1
port = 3306
user = mysql
character-set-server = utf8
default_storage_engine = innodb
log_timestamps = SYSTEM
socket = /tmp/mysql.sock
basedir = /opt/mysql
datadir = /opt/mysql/data
pid-file = /opt/mysql/data/mysql.pid
max_connections = 1000
max_connect_errors = 1000
table_open_cache = 1024
max_allowed_packet = 2048M
open_files_limit = 65535
#####====================================[innodb]==============================
innodb_buffer_pool_size = 512M
innodb_file_per_table = 1
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_purge_threads = 2
innodb_flush_log_at_trx_commit = 1
innodb_log_file_size = 512M
innodb_log_files_in_group = 2
innodb_log_buffer_size = 16M
innodb_max_dirty_pages_pct = 80
innodb_lock_wait_timeout = 30
#innodb_data_file_path=ibdata1:1024M:autoextend
#####====================================[log]==============================
log_error = /opt/mysql/log/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /opt/mysql/log/mysql-slow.log
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
7.把mysql放到本地系统服务中
cp -a /opt/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod a+x /etc/init.d/mysqld
修改里面的路径
vi /etc/init.d/mysqld
basedir=/opt/mysql
datadir=/opt/mysql/data
8.增加环境变量
vi /etc/profile
在最后增加:
export PATH=$PATH:/opt/mysql/bin
9.启动mysql
service mysqld start
启动出错请查看/opt/mysql/log/mysql-error.log
10.修改root密码
查看root的初始密码
cat /root/.mysql_secret
登录mysql,输入root密码
mysql -p
修改密码
SET PASSWORD FOR 'root'@localhost=PASSWORD('test123');