MySQL探索之旅之一:安装篇
章节提要
操作系统平台选择
以更专业的姿势安装MySQL
弱水三千,我娶哪个发行版/版本?
环境配置
安装
初始化实例以及启动MySQL服务
MySQL最最基础的命令
有一丢丢计算机基础知识的爱好者。
有一丢丢Linux操作基础。
[]
[]
[root ~]# echo "vm.swappiness=1" >> /etc/sysctl.conf[root ~]# sysctl -p
[root ~]# echo '* soft nofile 65535' >> /etc/security/limits.conf[root ~]# echo '* hard nofile 65535' >> /etc/security/limits.conf
[root ~]# echo "ulimit -n 65535" >> /etc/rc.local[root ~]# ulimit -n 65535
[root ~]# echo deadline > /sys/block/sda/queue/scheduler[root ~]# grubby --update-kernel=ALL --args="elevator=deadline"
[root ~]# sed -i '/^numa/c numa=off' /etc/default/grub[root ~]# grub2-mkconfig -o /etc/grub2.cfg >/dev/null
[]修改内容为SELINUX=disabled
[root@testsrv ~]# reboot
1.规划并创建MySQL的BASE DIR和DATA DIR
BASE DIR:MySQL的程序文件所在目录
[root@testsrv ~]# tar zxf /root/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /opt/[root@testsrv ~]# ls /opt/mysql-5.7.30-linux-glibc2.12-x86_64bin data docs include lib LICENSE man README share support-files
[root@testsrv ~]# mkdir -p /data/mysql/mysql3306/{data,logs,tmp}
[root@testsrv ~]# chown -R mysql:mysql /data/mysql/mysql3306/{data,logs,tmp}
/data/mysql/└── mysql3306 #实例端口号作为实例DATA DIR的一部分,便于管理和识别├── data #实例数据文件的存放目录(datadir就是它)├── logs #实例日志文件的存放目录└── tmp #实例的临时文件目录
2.为mysql3306实例编写参数文件
[root@testsrv ~]# vi /data/mysql/mysql3306/my3306.cnf
[mysqld]user =mysqlbasedir =/opt/mysql-5.7.30-linux-glibc2.12-x86_64/datadir =/data/mysql/mysql3306/data/server_id =1003306port =3306bind-address =127.0.0.1character_set_server =utf8mb4explicit_defaults_for_timestamp =onlog_timestamps =systemlower_case_table_names =1default_time_zone ='+08:00'socket =/data/mysql/mysql3306/tmp/mysql.socksecure_file_priv =/data/mysql/mysql3306/tmp/binlog_format =rowlog_bin =/data/mysql/mysql3306/logs/mysql-binbinlog_rows_query_log_events =onlog_slave_updates =onlog_error =/data/mysql/mysql3306/logs/error.loggeneral_log =offgeneral_log_file =/data/mysql/mysql3306/logs/general.logslow_query_log =onslow_query_log_file =/data/mysql/mysql3306/logs/slow_query.loglog_queries_not_using_indexes =onlong_query_time =1.000000gtid_mode =onenforce_gtid_consistency =on
3.检查MySQL的依赖
[root@testsrv ~]# ldd /opt/mysql-5.7.30-linux-glibc2.12-x86_64/bin/mysqld
4.创建软连接并配置PATH
[root@testsrv ~]# ln -sf /opt/mysql-5.7.30-linux-glibc2.12-x86_64 /usr/local/mysql[root@testsrv ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
5.使PATH生效
[root@testsrv ~]# . /etc/profile[root@testsrv ~]# which mysql/usr/local/mysql/bin/mysql
忙了这么久,做了这么多mysql3306的准备,在这一步都会体现价值。
1.随机密码方式初始化实例
[root@testsrv ~]# mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf --initialize
耐心的等待一会,没有报错的话就是可以啦(没有消息就是最好的消息)!
如果有报错,根据报错做一下排查:
datadir的目录中有文件(目录不为空拒绝初始化)?
是缺依赖包(xxx not found.)?
还是目录权限不对(permisson denied.)?
还是配置文件中的datadir目录里存在文件?
亦或是命令选项敲错了(误将 defaults-file 打成了 default-files)?
查看一下初始化后的结果
[root@testsrv ~]# ls /data/mysql/mysql3306/dataauto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sysca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ib_logfile2 performance_schema public_key.pem server-key.pem[root@testsrv ~]# cat /data/mysql/mysql3306/logs/error.log……2020-07-10T17:43:51.243252+08:00 0 [Warning] CA certificate ca.pem is self signed.2020-07-10T17:43:51.551619+08:00 1 [Note] A temporary password is generated for root@localhost: QsJqBL+7*;de
2.启动mysqld进程以启动实例
简单语法:mysqld --defaults-file=${mycnf_file} &--defaults-file 指定mysql cnf 配置文件路径
[root@testsrv ~]# mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf &
[root@testsrv ~]# ps -ef | grep mysqldmysql 527 465 1 09:20 pts/0 00:00:00 mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf
3.登陆mysql3306实例
简单语法:mysql -u root -p123 -h 127.0.0.1 -P 3306 -S xxxx.sock-u mysql user,不指定则以当前OS username-p mysql user的密码-h mysql实例的服务器地址/IP,可能与-P连用-P mysql实例的服务器端口,默认为3306-S 本地socket方式登陆时用来指定socket文件地址,用-S就不需要-h 和-P了
[root@testsrv ~]# mysql -S /data/mysql/mysql3306/tmp/mysql.sock -u root -pEnter password: # 此处密码不会显示,将初始化时errorlog中的临时密码:QsJqBL+7*;de 输入或粘贴进去即可Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.7.30-logCopyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>先退出mysql> exitBye
[root@testsrv ~]# mysql -h 127.0.0.1 -u root -pEnter password: # 此处密码不会显示,将初始化时errorlog中的临时密码:QsJqBL+7*;de 输入或粘贴进去即可Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.7.30-logCopyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
4.修改root@localhost密码
mysql> show databases;ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
修改密码为 hellomysql
mysql> alter user user() identified by 'hellomysql';Query OK, 0 rows affected (0.06 sec)
再进行操作就可以了
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys |+--------------------+4 rows in set (0.00 sec)
附.无密码方式初始化实例
[root@testsrv ~]# rm -fr /data/mysql/mysql3306/data/*[root@testsrv ~]# rm -fr /data/mysql/mysql3306/logs/*[root@testsrv ~]# rm -fr /data/mysql/mysql3306/tmp/*
[root@testsrv ~]# mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf --initialize-insecure
[root@testsrv ~]# ls /data/mysql/mysql3306/dataauto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sysca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ib_logfile2 performance_schema public_key.pem server-key.pem[root@testsrv ~]# cat /data/mysql/mysql3306/logs/error.log……2020-07-10T17:33:54.606511+08:001 [Warning] root@localhost is created with an empty password ! Pleaseconsider switching off the --initialize-insecure option.
[root@testsrv ~]# mysql -S /data/mysql/mysql3306/tmp/mysql.sock -u rootWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.7.30-logCopyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
初始化实例
随机密码:mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf --initialize无密码:mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf --initialize-insecure
启动实例
mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf &
登陆实例
mysql -S /data/mysql/mysql3306/tmp/mysql.sock -u root -p
修改密码
alter user username identified by 'newpassword';如果修改当前用户密码,username可以直接使用user() 函数。
查看实例状态
\s (清晰简要)或show status\G (长篇大论)
关闭实例
shutdown ; exit;
好了,本次的探索之旅已经结束,我是你们的老司机孔个个,请期待我们后续的旅程!
