MySQL5.6安装
程序员文章站
2022-05-26 23:40:46
...
一、下载MySQL安装包
下载链接:https://dev.mysql.com/downloads/mysql/
文中使用MySQL 5.6.50版本。
二、安装
[[email protected] ~]# cd /usr/local/src/
[[email protected]localhost src]# wget https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.50-linux-glibc2.12-x86_64.tar.gz
[[email protected] src]# tar -zxf mysql-5.6.50-linux-glibc2.12-x86_64.tar.gz
[[email protected] src]# ln -s /usr/local/src/mysql-5.6.50-linux-glibc2.12-x86_64 /usr/local/mysql
创建软连接主要是方便以后的版本升级等。
三、配置文件
在配置文件中定义MySQL相关路径:
[[email protected] ~]# cat /usr/local/mysql/my.cnf
[mysqld]
port=3306
bind-address=0.0.0.0
user=mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
四、初始化
创建mysql
用户:
[[email protected] ~]# useradd -M -s /sbin/nologin mysql
修改目录的属主和属组为mysql
:
[[email protected] ~]# chown -R mysql:mysql /usr/local/mysql/
执行初始化命令:
[[email protected] ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ --defaults-file=/usr/local/mysql/my.cnf
错误处理:
1)缺少perl模块导致的错误
FATAL ERROR: please install the following Perl modules before executing /usr/local/mysql/scripts/mysql_install_db:
Data::Dumper
安装autoconf
解决:
[[email protected] ~]# yum -y install autoconf
2)缺少libaio导致的错误
Installing MySQL system tables.../usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
安装libaio-devel
解决:
[[email protected] ~]# yum -y install libaio-devel
五、启动
后台启动:
[[email protected] ~]# /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/my.cnf &
编写启动脚本:
[[email protected] ~]# cat /usr/local/mysql/start_3306.sh
#!/bin/bash
ulimit -n 40960
sysctl -w vm.swappiness=0
numactl --interleave=all /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/my.cnf &
编写停止脚本:
[[email protected] ~]# cat /usr/local/mysql/stop_3306.sh
#!/bin/bash
/usr/local/mysql/bin/mysqladmin -S /usr/local/mysql/mysql.sock shutdown
脚本添加执行权限:
[[email protected] ~]# chmod 755 /usr/local/mysql/start_3306.sh /usr/local/mysql/stop_3306.sh
六、添加环境变量
MySQL提供了一系列命令,为了方便地执行这些命令,将其添加到环境变量中:
[[email protected] ~]# cat /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin/:$PATH
[[email protected] ~]# source /etc/profile
[[email protected] ~]# which mysql
/usr/local/mysql/bin/mysql
七、连接MySQL
在本地服务器,通过mysql
命令连接数据库:
[[email protected] ~]# which mysql
/usr/local/mysql/bin/mysql
[[email protected] ~]#
[[email protected] ~]# mysql -h 127.0.0.1
八、使用systemd管理MySQL
在步骤五中,使用shell脚本控制MySQL的启动和停止。这里用CentOS 7系统的systemd管理MySQL:
[[email protected] ~]# cat /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL 5.6.50
[Service]
User=mysql
Group=mysql
LimitNOFILE=40960
Restart=on-failure
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/my.cnf
[Install]
WantedBy=multi-user.target
[[email protected] ~]# systemctl daemon-reload
[[email protected] ~]# systemctl enable mysqld # 将MySQL设置为开机自启动
[[email protected] ~]# systemctl start mysqld # 启动MySQL
[[email protected] ~]# systemctl status mysqld # 查看MySQL状态
九、修改root用户密码
9.1、通过mysqladmin命令修改密码
为root用户设置密码:
[[email protected] ~]# mysqladmin -h 127.0.0.1 -u root -p password '123123'
修改root用户密码:
[[email protected] ~]# mysqladmin -h 127.0.0.1 -u root -p'123123' password '123456'
9.2、通过MySQL客户端修改密码
首先执行mysql
命令连接到MySQL,再执行设置密码命令:
[[email protected] ~]# mysql -h 127.0.0.1 -u root -p
mysql> set password for [email protected]=password('123123');
Query OK, 0 rows affected (0.00 sec)