linux系统版本:CentOS 7.3 64位
安装源文件版本:mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
mysql安装位置: /software/mysql
数据库文件数据位置: /data/mysql
mysql下载:mysql官网选择MySQL Community Edition -> MySQL Community Server -> Linux - Generic
然后选择64位中的后缀为tar.gz进行下载(不是test版本)
步骤:
1.在根目录下创建文件夹sotrware和数据库文件/data/mysql
#mkdir /software/
#mkdir /data/mysql
2.上传mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz文件到/software下
#cd /software
#tar -zxvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
3.更改解压缩后的文件夹名称
#mv /software/mysql-5.7.21-linux-glibc2.12-x86_64/ /software/mysql
4.创建mysql用户组和mysql用户
#groupadd mysql
#useradd mysql -g mysql
5.关联mysql用户到mysql用户组中
#chown -R mysql:mysql /software/mysql/
6.更改mysql安装文件夹mysql/的权限
#chmod -R 755 /software/mysql
7.安装libaio依赖包
查询libaio依赖包
#yum search libaio
如果没有安装
#yum install libaio
8.初始化mysql命令
# /software/mysql/bin/mysqld --user=mysql --basedir=/software/mysql --datadir=/data/mysql --initialize
在执行命令时特别注意一行内容
[Note] A temporary password is generated for root@localhost: rqM-VItqW0s
rqM-VItqW0s是mysql数据库登录的临时密码
9.启动mysql服务
#./software/mysql/support-files/mysql.server start
上面启动mysql服务命令是会报错的,因为没有修改mysql的配置文件,报错内容大致如下:
./support-files/mysql.server: line 239: my_print_defaults: command not found
./support-files/mysql.server: line 259: cd: /usr/local/mysql: No such file or directory
Starting MySQL ERROR! Couldn’t find MySQL server (/usr/local/mysql/bin/mysqld_safe)
10.修改MySQL配置文件
#cp /software/mysql/support-files/mysql.server /etc/init.d/mysqld
#chmod 755 /etc/init.d/mysqld
#vi /etc/init.d/mysqld
修改63-73行(输入:set nu查看行号)
修改前
then
basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin
if test -z "$datadir"
then
datadir=/usr/local/mysql/data
fi
sbindir=/usr/local/mysql/bin
libexecdir=/usr/local/mysql/bin
else
修改后
# Set some defaults
mysqld_pid_file_path=
if test -z "$basedir"
then
basedir=/software/mysql
bindir=/software/mysql/bin
if test -z "$datadir"
then
datadir=/data/mysql
fi
sbindir=/software/mysql/bin
libexecdir=/software/mysql/bin
else
保存退出
11.修改my.cnf文件
#vi /etc/my.cnf
将下面内容复制替换当前的my.cnf文件中的内容
[client]
no-beep
socket =/software/mysql/mysql.sock
# pipe
# socket=0.0
port=3306
[mysql]
default-character-set=utf8
[mysqld]
basedir=/software/mysql
datadir=/data/mysql
port=3306
pid-file=/software/mysql/mysqld.pid
#skip-grant-tables
skip-name-resolve
socket = /software/mysql/mysql.sock
character-set-server=utf8
default-storage-engine=INNODB
explicit_defaults_for_timestamp = true
# Server Id.
server-id=1
max_connections=2000
query_cache_size=0
table_open_cache=2000
tmp_table_size=246M
thread_cache_size=300
#限定用于每个数据库线程的栈大小。默认设置足以满足大多数应用
thread_stack = 192k
key_buffer_size=512M
read_buffer_size=4M
read_rnd_buffer_size=32M
innodb_data_home_dir = /data/mysql
innodb_flush_log_at_trx_commit=0
innodb_log_buffer_size=16M
innodb_buffer_pool_size=256M
innodb_log_file_size=128M
innodb_thread_concurrency=128
innodb_autoextend_increment=1000
innodb_buffer_pool_instances=8
innodb_concurrency_tickets=5000
innodb_old_blocks_time=1000
innodb_open_files=300
innodb_stats_on_metadata=0
innodb_file_per_table=1
innodb_checksum_algorithm=0
back_log=80
flush_time=0
join_buffer_size=128M
max_allowed_packet=1024M
max_connect_errors=2000
open_files_limit=4161
query_cache_type=0
sort_buffer_size=32M
table_definition_cache=1400
binlog_row_event_max_size=8K
sync_master_info=10000
sync_relay_log=10000
sync_relay_log_info=10000
#批量插入数据缓存大小,可以有效提高插入效率,默认为8M
bulk_insert_buffer_size = 64M
interactive_timeout = 120
wait_timeout = 120
log-bin-trust-function-creators=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
保存退出
12.启动mysql
#/etc/init.d/mysqld start
出现错误
Starting MySQL.Logging to '/data/mysql/localhost.localdomain.err'.
..... ERROR! The server quit without updating PID file (/software/mysql/mysqld.pid).
mysql默认使用mysql用户运行,添加参数–user=mysql
#/etc/init.d/mysqld start --user=mysql
报错
Starting MySQL... ERROR! The server quit without updating PID file (/software/mysql/mysqld.pid)
输入
#chown -R mysql:mysql /software/mysql
再次启动,成功
13.登录mysql
#/software/mysql/bin/mysql -u root -p
14.输入临时密码
15.修改mysql的登录密码
>mysql set password=password('root');
>mysql grant all privileges on *.* root@'%' identified 'root';
>mysql flush privileges;
grant all privileges on *.* to root@"%" identified by "密码";
这相当于是给IP-xxx.xxx.xxx.xxx赋予了所有的权限,包括远程访问权限,%百分号表示允许任何IP访问数据库。
flush privileges;
相当于重新夹在一下mysql权限,这步必须有
16.使用navicat远程连接mysql,需要开放linux上防火墙3306端口
CerntOS7使用的防火墙是firewalld
CentOS6使用的是iptables
>>> 关闭防火墙
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
>>> 开启端口
firewall-cmd -zone=public --add-port=3306/tcp --permanent
命令含义:
--zone #作用域
--add-port=3306/tcp #添加端口,格式为:端口/通讯协议
--permanent #永久生效,没有此参数重启后失效
>>>重启防火墙
firewall-cmd --reload
>>>常用命令介绍
firewall-cmd --state ##查看防火墙状态,是否是running
firewall-cmd --reload ##重新载入配置,比如添加规则之后,需要执行此命令
firewall-cmd --get-zones ##列出支持的zone
firewall-cmd --get-services ##列出支持的服务,在列表中的服务是放行的
firewall-cmd --query-service ftp ##查看ftp服务是否支持,返回yes或者no
firewall-cmd --add-service=ftp ##临时开放ftp服务
firewall-cmd --add-service=ftp --permanent ##永久开放ftp服务
firewall-cmd --remove-service=ftp --permanent ##永久移除ftp服务
firewall-cmd --add-port=80/tcp --permanent ##永久添加80端口
iptables -L -n ##查看规则,这个命令是和iptables的相同的
man firewall-cmd ##查看帮助
更多命令,使用 firewall-cmd --help 查看帮助文件
Mysql命令
service mysqld start #启动
service mysqld restart #重启
service mysqld shutdown #关闭