MySQL Install(Linux)

1. 获取MySQL源码包

 # wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
 # tar xf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
 
 以下步骤是解压之后移动到/usr/local/mysql下进行操作的

2. 创建MySQL用户

# useradd mysql -s /sbin/nologin
# mkdir -p /data/mysql
# chown -R mysql.mysql /data/mysql

3. 配置文件my.cnf

[mysqld]
server-id                      = 1
port                           = 3306
mysqlx_port                    = 33060
mysqlx_socket                  = /tmp/mysqlx.sock
max_connections                =300
datadir                        = /data/mysql
socket                         = /tmp/mysql.sock
pid-file                       = /tmp/mysqld.pid
log-error                      = error.log
slow-query-log                 = 1
slow-query-log-file            = slow.log
long_query_time                = 0.2
log-bin                        = bin.log
relay-log                      = relay.log
binlog_format                 =ROW
relay_log_recovery            = 1
character-set-client-handshake = FALSE
character-set-server           = utf8mb4
collation-server               = utf8mb4_unicode_ci
init_connect                   ='SET NAMES utf8mb4'
innodb_buffer_pool_size        = 1G
join_buffer_size               = 128M
sort_buffer_size               = 2M
read_rnd_buffer_size           = 2M
log_timestamps                 = SYSTEM
lower_case_table_names         = 1
default-time-zone              = '+8:00'
default-authentication-plugin  =mysql_native_password
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

4. 初始化MySQL

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

注意:可能会有报错信息,如下:
/usr/local/mysql/bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

如果安装mysql出现了以上的报错信息.这是缺少numactl;
执行:yum -y install numactl就可以解决这个问题了.

然后,重新执行初始化:
# /usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql --initialize-insecure

启动服务:
# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.. SUCCESS!

=====================================================================
=====================================================================
MySQL service options:
Usage: mysql.server  {start|stop|restart|reload|force-reload|status}  [ MySQL server options ]

5. 安装初始化(并非必须执行)


# /usr/local/mysql/bin/mysql_secure_installation
首先是设置密码,会提示先输入密码
Press y|Y for Yes, any other key for No: 初次运行直接回车

设置密码:
Set root password?	是否设置root密码
New password: 	设置root密码
Re-enter new password: 	在输入一次密码

其他配置:
Remove anonymous users? (Press y|Y for Yes, any other key for No) : 是否删除匿名用户
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : 是否禁止root远程登录	
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : 是否删除test database
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : 是否重新加载授权表

以上是执行安全初始化之后输出的信息,根据自身需求进行配置即可

补充:

1. 忘记MySQL密码解决方法
1. 编辑my.cnf:
	在[mysqld]标签中加入:
		skip-grant-tables
		
2. mysql.server restart

3. restart之后可以不用密码进入MySQL,进入之后重新设置新密码就🆗

注意:设置完新密码,删除my.cnf中的skip-grant-tables!

2. 环境变量
进入数据库:
# /usr/local/mysql/bin/mysql 

这样比较麻烦,加入环境变量之后可以简化此步骤,如下:

方法一:
export PATH=$PATH:/usr/local/mysql/bin
只对当前会话有效,当注销系统或者登出以后,PATH的设置就会失效

方法二:
vim ~/.bash_profile修改文件中PATH
将/usr/local/mysql/bin加入到PATH=$PATH:$HOME/bin之后即可
这种修改方式只对当前登录用户生效

方法三:
vim /etc/profile	//永久生效,并对所有系统用户生效
在文件末尾加上如下两行:
PATH=$PATH:/usr/local/mysql/bin
export PATH

最后,重新读取该文件,使其生效:
source OR . 	//重新读取
echo $PATH		//查看是否添加成功
3. MySQL加入到Linux服务列表中

启动数据库:
# /usr/local/mysql/support-files/mysql.server start

这样启动比较麻烦,可以通过加入到系统服务中来简化操作没如下:

# cp  -ar /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# chkconfig --add mysqld
# chkconfig --list mysqld
mysql.server   	0:off	1:off	2:on	3:on	4:on	5:on	6:off
# systemctl restart mysqld.service 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值