Linux环境 MySql详细安装步骤

*注:本文安装的是mysql5.6的版本,反复安装三遍总结下来的详细安装步骤。

1.解压MySQL压缩包到当前目录

	tar -xvzf mysql-5.6.45-linux-glibc2.12-x86_64.tar.gz

2.把安装包移动到指定目录并修改文件名为‘mysql’

	mv mysql-5.6.45-linux-glibc2.12-x86_64 /usr/local/mysql

3.创建数据仓库目录

	 mkdir /data/mysql         

4.添加 mysql 用户和 mysql 用户组

这里添加的 mysql 用户和用户组是稍后用来给 MySQL 安装目录分配权限用的,所以并不需要设定密码,因为这个用户是不能直接登录 Linux 系统的。

4.1 添加mysql用户组

	groupadd mysql

在这里插入图片描述
4.2. 添加mysql用户,同时指定mysql用户的初始组是mysql组

	useradd -g mysql mysql

在这里插入图片描述

5.进入MySQL安装目录

	cd /usr/local/mysql/

6.修改当前目录拥有者为mysql用户

	chown -R mysql .
	chgrp -R mysql .
	修改data/mysql目录权限为MySQL用户
	chown -R mysql /data/mysql

在这里插入图片描述

7.初始化数据库:执行命令

	./scripts/mysql_install_db --user=mysql 

在这里插入图片描述

7.1初始化报错(不报错请忽略):解决方法是安装autoconf库

	执行命令:yum -y install autoconf 

在这里插入图片描述

然后再次执行初始化数据库的命令即可。

8.修改当前目录权限为root用户

	chown -R root:root ./ 	

在这里插入图片描述
至此,安装完成。

10.配置参数

bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

在这里插入图片描述

11.修改系统配置文件

11.1进入配置目录

		cd /usr/local/mysql/support-files/

11.2复制并修改文件名到指定目录

	1)把mysql配置文件放到指定目录

	cp my-default.cnf /etc/my.cnf
如果不存在my-default.cnf文件或不清楚my.cnf的配置,可以参考博主的my.cnf文件配置,在本博客底部。		

	2)启动脚本放到开机初始化目录
	cp mysql.server /etc/init.d/mysql

在这里插入图片描述
11.3编辑mysql文件

		vim /etc/init.d/mysql
		添加如下配置:
		basedir=/usr/local/mysql
		datadir=/data/mysql

在这里插入图片描述

12.启动mysql服务

	service mysql start

在这里插入图片描述

13.查看mysql当前状态

	ps -ef|grep mysql

在这里插入图片描述

14.修改mysql的root用户密码(root初始密码为空)

	./bin/mysqladmin -u root password '密码'

	修改报错(不报错的可以略过):找不到mysql.sock文件

在这里插入图片描述

处理: 1.使用kill命令杀掉mysql的进程
      2.然后重新启动mysql服务
	  3.再次查看/tmp目录,

在这里插入图片描述

	此时已经生成mysql.sock文件,问题解决

15.登陆mysql

	mysql -hlocalhost -uroot -p

16.设置root账户的host地址

**a)使用%代替IP地址,代表所有ip都可以远程连接**

	mysql> grant all privileges on *.* to root@'%' identified by 'root'; 

**b)刷新,使配置生效**
	mysql>flush privileges;

17.查看mysql表,配置生效

	mysql> use mysql;
	mysql> select host,user from user;

在这里插入图片描述

	退出mysql命令窗口
	mysql>  exit

至此,就可以使用Navicat等客户端工具连接mysql数据库了,如果是阿里云服务器,要新建3306端口。

18.添加系统路径

a)编辑profile文件

	vim /etc/profile
	添加配置export PATH=/usr/local/mysql/bin:$PATH

在这里插入图片描述b)刷新,使profile文件的修改生效

	source /etc/profile

19.配置mysql开机自动启动

 chmod 755 /etc/init.d/mysql
 chkconfig --add mysql
 chkconfig --level 345 mysql on

至此,正式完成mysql的安装配置。

–查看mysql状态

#service mysql status
–停止mysql
#service mysql stop
–启动mysql
#service mysql start

查找mysql用户及用户组

more /etc/passwd | grep mysql
more /etc/shadow | grep mysql

删除用户

userdel mysql

my.cnf配置信息:

	#For advice on how to change settings please see
	# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
	# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
	# *** default location during install, and will be replaced if you
	# *** upgrade to a newer version of MySQL.

	[mysqld]

	# Remove leading # and set to the amount of RAM for the most important data
	# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
	innodb_buffer_pool_size = 128M

	# Remove leading # to turn on a very important data integrity option: logging
	# changes to the binary log between backups.
	log_bin
	character-set-server=utf8
	collation-server=utf8_bin
	init-connect='SET NAMES utf8'
	# These are commonly set, remove the # and set as required.
	basedir = /usr/local/mysql
	datadir = /data/mysql
	port = 3306
	bind-address = 0.0.0.0
	server_id = 22206
	socket = /data/mysql/mysql.sock
	binlog_format = statement
	# Remove leading # to set options mainly useful for reporting servers.
	# The server defaults are faster for transactions and fast SELECTs.
	# Adjust sizes as needed, experiment to find the optimal values.
	join_buffer_size = 128M
	sort_buffer_size = 2M
	read_rnd_buffer_size = 2M
	log_bin_trust_function_creators = on
	sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

猜你喜欢

转载自blog.csdn.net/weixin_43945983/article/details/108198941