Linux、Centos7 中安装mysql5.7
1、下载mysql 二进制安装包
https://dev.mysql.com/downloads/mysql/
选择 linux-Generic
mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
2、查看操作系统是否有自带的mysql,如有则卸载
rpm –qa |grep mariadb
卸载:
rpm -e mariadb-libs-5.5.41-2.el7_0.x86_64 --nodeps
3、安装
Tar zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
Mv mysql-5.7.17-linux-glibc2.5-x86_64 /usr/local/mysql
4、创建mysql组及mysql用户
Groupadd mysql
Useradd -m –r –g mysql –s /bin/false mysql
5、创建相关的目录
mkdir /var/log/mysql 文件夹所有者改为mysql,
chown -R mysql:mysql /var/log/mysql
chmod 777 /var/log/mysql 或权限改为777 ,
Mkdir /usr/local/mysql/data 文件夹所有者改为mysql,或权限改为777
6、初始化mysql
cd /usr/local/mysql/bin
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --explicit_defaults_for_timestamp
显示如下:
017-03-23T01:57:06.821038Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-03-23T01:57:07.048639Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-03-23T01:57:07.165356Z 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: 04ed965a-0f6c-11e7-85a1-00163e02462c.
2017-03-23T01:57:07.167725Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-03-23T01:57:07.168255Z 1 [Note] A temporary password is generated for root@localhost: Dlicoc_!h6Vs
---有几条warning是正常的。
---最后一条给出的随机root密码要记住
7、生成数据库需要的秘钥文件
./mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data
8、创建my.cnf文件
vi /etc/my.cnf
写入如下内容: ---各参数根据环境不同调整
[mysqld]
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
character-set-server=utf8
server-id=1
log-error=/usr/local/mysql/data/error.log
pid-file=/usr/local/mysql/data/mysql.pid
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld]
port = 3306
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
server-id = 1
socket=/tmp/mysql.sock
character-set-server = utf8
log-error = /var/log/mysql/error.log
pid-file = /usr/local/mysql/data/mysql.pid
general_log = 1
skip-name-resolve
#skip-networking
back_log = 300
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 28M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 30 ----binlog日志过期时间(过期的日志自动删除),如果单个日志文件特别大,可以适当调小日志过期时间
performance_schema = 0
explicit_defaults_for_timestamp
#lower_case_table_names = 1
myisam_sort_buffer_size = 8M
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
symbolic-links=0
sql_mode=NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER,STRICT_TRANS_TABLES
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
9、添加mysql环境变量
vi /etc/profile
添加如下内容:
Export PATH=/usr/local/mysql/bin:$PATH
保存后退出,执行如下命令:
source /etc/profile
10、Mysql 服务添加为系统服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql.service
chkconfig --add mysql.service ---mysql添加为系统服务
chkconfig --list mysql.service ----查看服务是否添加成功
chkconfig mysql.service on ----设为开机启动
11、启动mysql
systemctl start mysql.service
验证是否启动成功
netstat –apn |grep 3306
ps –aux |grep mysql
12、连接mysql
mysql –u root –p
然后输入之前随机的密码
更改root密码
set password=password('123456');
授予root远程连接权限
grant all privileges on *.* to root@'%' identified by '123456';
flush privileges;
13、登录报错
第一次登录报错:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
- 解决办法:关闭数据库
vi /etc/my.cnf
- 添加如下:
skip-grant-tables
- 启动数据库,不需要输入密码
执行命令
update mysql.user set authentication_string=password('123456') where user
='root' and host='localhost';
flush privileges;
- 关闭数据库,把刚才添加的注释掉(my.cnf),启动数据库
alter user 'root'@'localhost' identified by '123456';
- 必须执行此命令,否则很多功能使用不了。