本文档中有三种安装方式,安装后的目录结构有区别,在后续的所有步骤中,特别要注意相关目录是否正确。具体如下。
1 2 |
rm -f /etc/my.cnf echo -e "[mysqld_safe]\nlog-error=/data/mysql/log/mysql.log\npid-file=/data/mysql/run/mysqld.pid\n[mysqldump]\nquick\n[mysql]\nno-auto-rehash\n[client]\ndefault-character-set=utf8\n[mysqld]\nbasedir=/usr/local/mysql\nsocket=/data/mysql/run/mysql.sock\ntmpdir=/data/mysql/tmp\ndatadir=/data/mysql/data\ndefault_authentication_plugin=mysql_native_password\nport=3306\nuser=mysql\n" > /etc/my.cnf |
datadir:MySQL数据存储路径
tmpdir:MySQL临时文件存储路径
log-error:MySQL错误日志文件路径
pid-file:MySQL进程ID文件路径
socket:MySQL通信socket文件路径
在本配置中,可执行以下命令确保相关目录已存在,否则可能导致MySQL启动失败。
1 2 3 4 |
mkdir -p /data/mysql/log mkdir -p /data/mysql/run mkdir -p /data/mysql/tmp mkdir -p /data/mysql/data |
1
|
cat /etc/my.cnf
|
预期结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[mysqld_safe] log-error=/data/mysql/log/mysql.log pid-file=/data/mysql/run/mysqld.pid [mysqldump] quick [mysql] no-auto-rehash [client] default-character-set=utf8 [mysqld] basedir=/usr/local/mysql socket=/data/mysql/run/mysql.sock tmpdir=/data/mysql/tmp datadir=/data/mysql/data default_authentication_plugin=mysql_native_password port=3306 user=mysql |
1 2 |
chown mysql:mysql /etc/my.cnf ll /etc/my.cnf |
预期结果如下,表示操作成功。
1
|
-rw-r--r-- 1 mysql mysql 353 Sep 14 14:33 /etc/my.cnf |
1 2 3 |
chmod 777 /usr/local/mysql/support-files/mysql.server cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql chkconfig mysql on |
修改“/etc/init.d/mysql”的用户组和用户权限为mysql:mysql。
1 2 |
chown -R mysql:mysql /etc/init.d/mysql ll /etc/init.d/mysql |
1
|
echo export PATH=$PATH:/usr/local/mysql/bin >> /etc/profile |
其中PATH中的“/usr/local/mysql/bin”路径,为MySQL软件安装目录下的bin文件的绝对路径,请根据实际情况修改。
1
|
source /etc/profile |
1
|
env |
预期结果:
1
|
chmod 755 /data/mysql/data/ |
1 2 |
su - mysql whoami |
1
|
mysqld --defaults-file=/etc/my.cnf --initialize |
或者
1
|
/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --initialize |
初始化回显倒数第2行中有数据库的初始密码,步骤8时将会用到,请妥善保存。
初始化完成后,查看数据目录下数据文件“/data/mysql/data”的用户组和用户权限为mysql:mysql(前面/etc/my.cnf文件中配置的操作系统用户是user=mysql)。
1
|
ll /data/mysql/data
|
预期结果:
1 2 |
ls /data/mysql/data rm -rf /data/mysql/data/ |
如果以root用户(su - root)第一次启动数据库服务(service mysql start),则启动时会提示缺少mysql.log文件而导致启动数据库失败。切换到mysql用户(su - mysql)启动数据库服务后,会在/data/mysql/log目录下生成mysql.log文件,停止数据库服务(service mysql stop),再次以root用户启动数据库服务可以解决该问题。
1
|
/usr/sbin/mysqld --defaults-file=/etc/my.cnf & |
执行命令后,回车,进行下一步骤。
1
|
service mysql start |
1
|
mysqld --defaults-file=/etc/my.cnf & |
1
|
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf & |
1
|
ps -ef | grep mysql |
1 2 3 |
netstat -anpt netstat -anpt | grep mysql netstat -anpt | grep 3306 |
如果netstat命令执行失败,则需要安装依赖包。
1
|
yum -y install net-tools |
1
|
mysql -uroot -p -S /data/mysql/run/mysql.sock |
或者
1
|
/usr/local/mysql/bin/mysql -uroot -p -S /data/mysql/run/mysql.sock |
预期结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[mysql@localhost ~]$ /usr/bin/mysql -uroot -p -S /data/mysql/run/mysql.sock Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.17 Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> |
文档中的用户名和密码仅供参考,请根据用户自身的实际情况进行配置。
1
|
alter user 'root'@'localhost' identified by "123456"; |
1
|
create user 'root'@'%' identified by '123456'; |
1 2 |
grant all privileges on *.* to 'root'@'%'; flush privileges; |
1
|
\q
|
1
|
exit
|
1
|
mysql -uroot -p -S /data/mysql/run/mysql.sock |
或者
1
|
/usr/local/mysql/bin/mysql -uroot -p -S /data/mysql/run/mysql.sock |
退出数据库。
\q
exit
1
|
service mysql stop |
或者
1
|
mysqladmin -uroot -p123456 shutdown -S /data/mysql/run/mysql.sock |
或者
1
|
/usr/local/mysql/bin/mysqladmin -uroot -p123456 shutdown -S /data/mysql/run/mysql.sock |
1
|
ps -ef | grep mysql |