提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
解压MySQL安装包
sudo tar -zvxf mysql-5.7.9-linux-glibc2.5-x86_64.tar.gz -C /usr/local
创建软连接
sudo ln -s /usr/local/mysql-5.7.9-linux-glibc2.5-x86_64/ /usr/local/mysql
创建MySQL用户
sudo useradd -s /sbin/nologin -M mysql
配置/etc/my.cnf文件
sudo vim /etc/my.cnf
[client]
port = 3306
#socket = /usr/local/mysql/mysql.sock
[mysqld]
character_set_server = utf8
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
server_id = 1
#socket = /usr/local/mysql/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log
初始化安装MySQL,会生成一个默认密码
sudo /usr/local/mysql/bin/mysqld --initialize --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql
最后一排root@localhost: xxxxx为临时生成的密码,后续会用到。
配置MySQL自启动文件
sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
配置MySQL环境变量:
sudo vim /etc/profile
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
环境生效
source /etc/profile
启动MySQL
sudo systemctl daemon-reload
sudo systemctl start mysqld
设置开机自启
sudo systemctl enable mysqld
设置用户权限(示例)
alter user 'root'@'localhost' identified by '123456';
赋予本地操作权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
建立远程连接权限
Grant all on *.* to 'root'@"%" identified by '123456' WITH GRANT OPTION;
(root用户不建议)
刷新数据库
FLUSH PRIVILEGES;
常见错误
离线安装MySQL时出现缺少库的问题,一般会提示缺失下面这几个库。
解决方法:
sudo apt-get install libaio1 libmecab2 libncurses5