1、准备解压缩包
2、添加用户组
非常重要!!!
#添加用户组
groupadd mysql
#添加用户
useradd -r -s /sbin/nologin -g mysql mysql -d /opt/local/mysql/
3、创建并编辑配置文件my.cnf
[mysqld]
socket=/var/lib/mysql/mysql.sock
sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
# set mysql server port
port = 3306 #默认是3306,这里发现3306已经被占用,因此防止这种情况发生,可以避免使用3306mysql默认端口
# set mysql install base dir
basedir=/usr/local/mysql
# set the data store dir
datadir=/usr/local/mysql/data
# set the number of allow max connnection
max_connections=200
# set server charactre default encoding
character-set-server=utf8
# the storage engine
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
explicit_defaults_for_timestamp=true
character-set-server=utf8
collation-server=utf8_general_ci
[mysqld_safe]
log-error=/usr/local/mysql/mysql.log
pid-file=/usr/local/mysql/mysql.pid
[mysql.server]
user=mysql
basedir=/usr/local/mysql
[client]
port=3306
socket=/var/lib/mysql/mysql.sock
4、解压缩并初始化mysql
#1、上传压缩文件到/usr/local目录下
rz 命令或者ftp
#2、解压压缩文件
tar -zxvf mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz
#3、修改解压文件命成为mysql
mv mysql-5.7.13-linux-glibc2.5-x86_64 mysql
#4、创建data文件夹
mkdir /usr/local/mysql/data
#5、切换到mysql的bin目录
cd /usr/local/mysql/bin
#6、执行mysql初始化命令,所有选项成一行
./mysqld --defaults-file=/usr/local/mysql/my.cnf --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize-insecure --ssl --explicit_defaults_for_timestamp --user root --verbose
参数说明
–initialize-insecure \ #默认第一次不使用密码登录
–basedir=/usr/local/mysql/ #该参数指定了安装 MySQL 的安装路径(mysql安装目录)
–datadir=/usr/local/mysql/data \ #该参数指定MySQL的数据文件的存放目录
初始化成功
2021-08-03T06:50:16.725299Z 0 [ERROR] --verbose is for use with --help; did you mean --log-error-verbosity?
2021-08-03T06:50:17.425091Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-08-03T06:50:17.565738Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-08-03T06:50:17.647997Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 1021ba3e-f427-11eb-8768-fa163e122055.
2021-08-03T06:50:17.655141Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-08-03T06:50:17.657207Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
5、启动mysql
非常重要!!!
#给mysql文件夹赋权限
chmod -R 777 /usr/local/mysql
启动服务
#mysql用户添加数据目录权限.
chown -R mysql.mysql /usr/local/mysql/data
#mysql.server默认的路径是/usr/local/mysql,若自定义目录先修改连两个配置再启动
vi mysql.server
#键入自定义目录
basedir=/data/mysql/mysqldir
datadir=/data/mysql/mysqldir/data
#其中/usr/local/mysql/为自己的mysql安装目录
/usr/local/mysql/support-files/mysql.server start
#启动成功控制台输出
my_print_defaults: [Warning] World-writable config file '/usr/local/mysql/my.cnf' is ignored.
Starting MySQL. SUCCESS!
6、修改root用户密码
#1、登录mysql (初始化时默认第一次不需要输入密码直接回车)
mysql -uroot -p
#2、修改root用户密码
SET PASSWORD FOR 'root'@localhost=PASSWORD('123456');
7、允许远程ip连接
mysql -u root -p
use mysql;
update user set host = '%' where user = 'root';
FLUSH PRIVILEGES;
8、设置开机自启
#切换到目录/usr/local/mysql/support-files
cd /usr/local/mysql/support-files
#复制文件
cp ./mysql.server /etc/init.d/mysqld
#赋予可执行权限
chmod +x /etc/init.d/mysqld
#添加配置
chkconfig --add mysqld
#查看配置
chkconfig --list
可查看结果
[root@zbbjserver support-files]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
如果看到mysql的服务,并且3,4,5都是on的话则成功,如果是off,则键入
chkconfig --level 345 mysqld on
9、遇到的问题
./mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
准备相应的libaio.so.1
文件 放到 /usr/lib64/
目录下
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
从my.cnf
已经声明了mysql.sock的文件位置,而错误提示为/tmp/mysql.sock
; 也就是说虽然我的mysql声明了sock文件的位置,但是客户端在连接的时候, linux系统总是去/tmp目录下去找,
为mysql.sock创建链接文件:
$ ln -s /opt/mysql/mysql.sock /tmp/mysql.sock
然后再次连接mysql,成功连接