一:实验背景;
在马哥教育学习到MYSQL这章时,留作业练习的实验
二:实验准备;
1、先检查虚拟机上是否存在mariadb: rpm -qa mariadb*,若有的话,则卸载;
2、通过yum info mariadb查找到mariadb的官网,进去下载mariadb的最新版本mariadb-10.2.9-linux-x86_64.tar.gz;
三:实验步骤;
1、创建mysql用户;
[root@localhost ~]# getent passwd mysql #查一下该用户是否存在,不存在的话,就执行下面的代码创建[root@localhost ~]# useradd -d /app/mysqldb -r -m -s /sbin/nologin mysql #创建mysql账户并指明家目录(可以自定义),为了安全起见将shell设为nologin
2、将下载的最新安装包拷进linux虚拟机,并解压缩;
[root@localhost ~]# rz #用rz将windows下下载好的安装包拷进linux[root@localhost ~]# tar xvf mariadb-10.2.8-linux-x86_64.tar.gz -C /usr/local/ #解压缩(因为是已经编译好的二进制文件,所以目录已经被制定好,必须放在/usr/local,故加上-C解压缩到指定目录)[root@localhost ~]# cd /usr/local/ #进到这个目录下面[root@localhost /usr/local]# ln -s mariadb-10.2.8-linux-x86_64/ mysql #创建软链接(因为名字必须是mysql)
3、创建mysql的配置文件;
[root@localhost /usr/local]# cd /usr/local/mysql/ #进入到刚创建的软链接目录下[root@localhost /usr/local/mysql]# mkdir /etc/mysql/ # 因为编译时默认配置文件放在/etc/mysql下,所以创建一个mysql的目录[root@localhost /usr/local/mysql]# ls support-files/ #配置文件已经被准备好,放在了/usr/local/mysql/support-files目录下binary-configure mysqld_multi.servermagic mysql-log-rotatemy-huge.cnf mysql.servermy-innodb-heavy-4G.cnf policymy-large.cnf wsrep.cnfmy-medium.cnf wsrep_notifymy-small.cnf[root@localhost /usr/local/mysql]# cp support-files/my-huge.cnf /etc/mysql/my.cnf #有好几个,选择其中一个复制到特定目录下/etc/mysql/并改名为特定的配置文件名my.cnf[root@localhost /usr/local/mysql]# vim /etc/mysql/my.cnf #编辑配置文件[mysqld] #在[mysqld]这一项下面加上下面三行,第一行是必须要加的,后面两行是为了方便管理、性能优化的,可有可无datadir = /app/mysqldb #指明数据库存放路径innodb_file_per_table = on #用的是innodb引擎,并且使每个表单独一个文件,方便以后复制单表时方便skip_name_resolve = on #为了加速访问速度,忽略反向解析
4、创建系统数据库;
#数据库文件已经被准备好的,放置在/usr/local/mysql/scripts/mysql_install_db里[root@localhost /usr/local/mysql]# scripts/mysql_install_db --user=mysql --datadir=/app/mysqldb #执行上条命令时,只能在/usr/local/mysql下执行,而不能进入到scripts下执行,会报错的[root@localhost /usr/local/mysql]# ls /app/mysqldb/ # 验证一下数据库是否生成aria_log.00000001 ib_logfile0 multi-master.info mysql-bin.000003aria_log_control ib_logfile1 mysql mysql-bin.000004ib_buffer_pool ibtmp1 mysql-bin.000001 mysql-bin.indexibdata1 localhost.localdomain.pid mysql-bin.000002 performance_schema[root@localhost /usr/local/mysql]#
5、尝试启动mysql服务;
[root@localhost /usr/local/mysql]# cp support-files/mysql.server /etc/init.d/mysqld # 将服务脚本放在特定的目录下[root@localhost /usr/local/mysql]# chkconfig --list mysqld # 查看一下mysqld服务是否生成,没生成的话就用下面的命令加上[root@localhost /usr/local/mysql]# chkconfig --add mysqld [root@localhost /usr/local/mysql]# service mysqld start # 我的这个报错是因为,我为了做实验演示,特意将原有的mysql用户删除重建,导致/var/log/mariadb目录没有了属主属组Restarting mysqld (via systemctl): Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details. [FAILED][root@localhost /usr/local]# chown -R mysql:mysql /var/log/mariadb/ #将/var/log/mariadb/及其子文件的属主属组都换成mysql
6、创建日志文件;
我的原本/var/log/mariadb/目录文件已经存在(才会出现第5中的问题)所以不用创建,没有这个日志文件的话,在第5步启动服务时会报错说没有找到日志文件/var/log/mariadb,我们只需要创建出来就好了;
[root@localhost /usr/local/mysql]# mkdir /var/log/mariadb/ #先创建出日志文件目录[root@localhost /usr/local/mysql]# chown mysql /var/log/mariadb/ #这一步是原先没有/var/log/mariadb/这个文件,所以创建后需要将其所属人换成mysql,这样以后mysql就有权限在其下创建或删除子文件[root@localhost /usr/local/mysql]# service mysqld start #再重启服务Starting mysqld (via systemctl): [ OK ][root@localhost /usr/local/mysql]#
7、添加PATH变量;
#因为mysql被放在/usr/local/mysql/bin下,所以可以将其路径写到配置文件中,使用时就不用写绝对路径了[root@localhost /usr/local/mysql]# vim /etc/profile.d/mysql.sh PATH=/usr/local/mysql/bin:$PATH[root@localhost /usr/local/mysql]# . /etc/profile.d/mysql.sh #使其生效[root@localhost /usr/local/mysql]# echo $PATH /usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin[root@localhost /usr/local/mysql]#
8、初始化,跑安全脚本,设置口令密码;
[root@localhost /usr/local/mysql]# mysql_secure_installation #跑安全脚本 NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!In order to log into MariaDB to secure it, we'll need the currentpassword for the root user. If you've just installed MariaDB, andyou haven't set the root password yet, the password will be blank,so you should just press enter here.Enter current password for root (enter for none): #在这里提示输入口令密码,因为我们之前并没有设,所以直接Enter回车OK, successfully used password, moving on...Setting the root password ensures that nobody can log into the MariaDBroot user without the proper authorisation.Set root password? [Y/n] y #问是否给root设密码,跑安全脚本就是为了安全,所以我们当然要设置密码喽! 输入“y”后回车New password: #输入口令密码Re-enter new password: #再次输入(要输两边确认) Password updated successfully!Reloading privilege tables.. ... Success!By default, a MariaDB installation has an anonymous user, allowing anyoneto log into MariaDB without having to have a user account created forthem. This is intended only for testing, and to make the installationgo a bit smoother. You should remove them before moving into aproduction environment.Remove anonymous users? [Y/n] y #是否移除匿名用户,为了安全,我们输入“y”,将匿名用户移除,不让其登录 ... Success!Normally, root should only be allowed to connect from 'localhost'. Thisensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n #是否禁用远程登录,不禁止,故输入“n”,回车 ... skipping.By default, MariaDB comes with a database named 'test' that anyone canaccess. This is also intended only for testing, and should be removedbefore moving into a production environment.Remove test database and access to it? [Y/n] y #是否移除test数据库文件,“y|n”都行,此处我选的y - Dropping test database... ... Success! - Removing privileges on test database... ... Success!Reloading the privilege tables will ensure that all changes made so farwill take effect immediately.Reload privilege tables now? [Y/n] y # 是否重新加载,即是否立即使以上的配置生效,当然要啊,所以“y” ... Success!Cleaning up...All done! If you've completed all of the above steps, your MariaDBinstallation should now be secure.Thanks for using MariaDB!
四:实验测试;
[root@localhost /usr/local/mysql]# mysql -uroot -p #因为我们设置过口令了,所以就要加上密码登录,可以“-p密码”,也可以直接 -p 后回车再输入密码,后者比较安全Enter password: #输入密码 Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 24Server version: 10.2.9-MariaDB-log MariaDB ServerCopyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> show databases; #显示数据库文件信息,而且从“[(none)]”看出现在我们并没有在哪个特定的数据库中+--------------------+| Database |+--------------------+| #mysql50#.mozilla || information_schema || mysql || performance_schema |+--------------------+4 rows in set (0.01 sec)MariaDB [(none)]> use mysql #设置指定数据库为当前数据库(注意:此处是个mysql的内部命令,后面不用加分号,加分号的都是SQL语句)Database changed
MariaDB [mysql]> show tables; #显示当前数据库中的所有表信息+---------------------------+| Tables_in_mysql |+---------------------------+| column_stats || columns_priv || db || event || func || general_log || gtid_slave_pos || help_category || help_keyword || help_relation || help_topic || host || index_stats || innodb_index_stats || innodb_table_stats || plugin || proc || procs_priv || proxies_priv || roles_mapping || servers || slow_log || table_stats || tables_priv || time_zone || time_zone_leap_second || time_zone_name || time_zone_transition || time_zone_transition_type || user |+---------------------------+30 rows in set (0.01 sec)
MariaDB [mysql]> desc user; #查看user表的表结构,名字、类型、值、....(内容只显示了前面的一部分)+------------------------+-----------------------------------+------+-----+----------+-------+| Field | Type | Null | Key | Default | Extra |+------------------------+-----------------------------------+------+-----+----------+-------+| Host | char(60) | NO | PRI | | || User | char(80) | NO | PRI | | || Password | char(41) | NO | | | || Select_priv | enum('N','Y') | NO | | N | || Insert_priv | enum('N','Y') | NO | | N | |+------------------------+-----------------------------------+------+-----+----------+-------+46 rows in set (0.01 sec)MariaDB [mysql]>select host,user,password from user; #从user表中查找host/user/password信息(如下图所示)+-----------------------+------+-------------------------------------------+| host | user | password |+-----------------------+------+-------------------------------------------+| localhost | root | *367DDCBB0F311F04DDA795BC971B23954EF636DD || localhost.localdomain | root | *367DDCBB0F311F04DDA795BC971B23954EF636DD || 127.0.0.1 | root | *367DDCBB0F311F04DDA795BC971B23954EF636DD || ::1 | root | *367DDCBB0F311F04DDA795BC971B23954EF636DD |+-----------------------+------+-------------------------------------------+4 rows in set (0.00 sec)
MariaDB [mysql]> select user(); #查看当前用户名+----------------+ | user() | #注意,mysql中的用户名和平常所说的用户名不太一样,+----------------+ mysql中的用户名是由“用户名@本机(或者ip地址)”共同组合而成的全称| root@localhost |+----------------+1 row in set (0.00 sec) # 注意,对于SQL语句,后面都要加上分号,否则回车后不执行,一直到你输入分号为止(如下所示) MariaDB [(none)]> show databases -> -> ;+--------------------+| Database |+--------------------+| #mysql50#.mozilla || information_schema || mysql || performance_schema |+--------------------+4 rows in set (0.00 secMariaDB [mysql]> quit #quit退出Bye[root@localhost /usr/local/mysql]#
五:实验总结;
这个实验其实步骤很简单,主要是将代码敲对就OK了,在试验中,有的同学在配置文件my.conf中的那三行代码打错了,导致了一些问题出现,根据步骤一步一不做,然后出错的时候就按照步骤去排查。
本文出自 “13162997” 博客,请务必保留此出处http://13172997.blog.51cto.com/13162997/1972311