二进制安装MySQL数据库
程序员文章站
2022-03-08 14:47:51
二进制安装MySQL数据库 [toc] 实验准备: 1. 一个干净的centos7系统(确认是没有安装过数据库的系统) 2. 关闭防火墙和selinux 3. 创建一个逻辑卷分区(因为数据库存放数据一般都是在上涨的普通的分区到达分区容量极限时没办法在扩充分区所以使用逻辑卷分区比较好) 4. 从官方平 ......
二进制安装mysql数据库
实验准备:
- 一个干净的centos7系统(确认是没有安装过数据库的系统)
- 关闭防火墙和selinux
- 创建一个逻辑卷分区(因为数据库存放数据一般都是在上涨的普通的分区到达分区容量极限时没办法在扩充分区所以使用逻辑卷分区比较好)
- 从官方平台下载一个编译过的一个二进制程序包(官方包下载地址:https://dev.mysql.com/downloads/mysql/)
准备阶段:
- 创建逻辑卷分区:
[root@centos7 ~]# df filesystem 1k-blocks used available use% mounted on /dev/sda2 104806400 4233244 100573156 5% / devtmpfs 998216 0 998216 0% /dev tmpfs 1014056 0 1014056 0% /dev/shm tmpfs 1014056 10452 1003604 2% /run tmpfs 1014056 0 1014056 0% /sys/fs/cgroup /dev/sr0 10491772 10491772 0 100% /misc/cd /dev/sda1 1038336 167036 871300 17% /boot /dev/sda3 52403200 32992 52370208 1% /data tmpfs 202812 0 202812 0% /run/user/0 tmpfs 202812 12 202800 1% /run/user/42 [root@centos7 ~]# umount /data (确定这个分区没有需要的数据了取消挂载)
- 改为逻辑卷标识
[root@centos7 ~]# fdisk /dev/sda welcome to fdisk (util-linux 2.23.2). changes will remain in memory only, until you decide to write them. be careful before using the write command. command (m for help): p disk /dev/sda: 214.7 gb, 214748364800 bytes, 419430400 sectors units = sectors of 1 * 512 = 512 bytes sector size (logical/physical): 512 bytes / 512 bytes i/o size (minimum/optimal): 512 bytes / 512 bytes disk label type: dos disk identifier: 0x000a8280 device boot start end blocks id system /dev/sda1 * 2048 2099199 1048576 83 linux /dev/sda2 2099200 211814399 104857600 83 linux /dev/sda3 211814400 316671999 52428800 83 linux /dev/sda4 316672000 419430399 51379200 5 extended /dev/sda5 316674048 325062655 4194304 82 linux swap / solaris command (m for help): t partition number (1-5, default 5): 3 hex code (type l to list all codes): 8e changed type of partition 'linux' to 'linux lvm' command (m for help): p disk /dev/sda: 214.7 gb, 214748364800 bytes, 419430400 sectors units = sectors of 1 * 512 = 512 bytes sector size (logical/physical): 512 bytes / 512 bytes i/o size (minimum/optimal): 512 bytes / 512 bytes disk label type: dos disk identifier: 0x000a8280 device boot start end blocks id system /dev/sda1 * 2048 2099199 1048576 83 linux /dev/sda2 2099200 211814399 104857600 83 linux /dev/sda3 211814400 316671999 52428800 8e linux lvm /dev/sda4 316672000 419430399 51379200 5 extended /dev/sda5 316674048 325062655 4194304 82 linux swap / solaris command (m for help): w the partition table has been altered! calling ioctl() to re-read partition table. warning: re-reading the partition table failed with error 16: device or resource busy. the kernel still uses the old table. the new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) syncing disks.
- 使用partprobe同步一下分区
[root@centos7 ~]# lsblk name maj:min rm size ro type mountpoint sda 8:0 0 200g 0 disk ├─sda1 8:1 0 1g 0 part /boot ├─sda2 8:2 0 100g 0 part / ├─sda3 8:3 0 50g 0 part ├─sda4 8:4 0 1k 0 part └─sda5 8:5 0 4g 0 part [swap] sr0 11:0 1 10g 0 rom /misc/cd [root@centos7 ~]# partprobe warning: unable to open /dev/sr0 read-write (read-only file system). /dev/sr0 has been opened read-only.
- 更改为物理卷
[root@centos7 ~]# pvcreate /dev/sda3 warning: xfs signature detected on /dev/sda3 at offset 0. wipe it? [y/n]: y wiping xfs signature on /dev/sda3. physical volume "/dev/sda3" successfully created. [root@centos7 ~]# pvs pv vg fmt attr psize pfree /dev/sda3 lvm2 --- 50.00g 50.00g
- 把物理卷加为卷组并起个名字
[root@centos7 ~]# vgcreate vg_data /dev/sda3 volume group "vg_data" successfully created [root@centos7 ~]# vgs vg #pv #lv #sn attr vsize vfree vg_data 1 0 0 wz--n- <50.00g <50.00g
- 创建逻辑卷为mysql并设定为40g空间(空行之后的都是示例防止你们设错空间忘了怎么删!)
[root@centos7 ~]# lvcreate -n mysql -l 40g vg_data logical volume "mysql" created. [root@centos7 ~]# lvs lv vg attr lsize pool origin data% meta% move log cpy%sync convert mysql vg_data -wi-a----- 40.00g [root@centos7 ~]# lvremove /dev/vg_data/mysql do you really want to remove active logical volume vg_data/mysql? [y/n]: y logical volume "mysql" successfully removed [root@centos7 ~]# lvcreate -n mysql -l 100%free vg_data (这里是把所有的剩余空间都设为逻辑卷) logical volume "mysql" created. [root@centos7 ~]# lvs lv vg attr lsize pool origin data% meta% move log cpy%sync convert mysql vg_data -wi-a----- <50.00g
- 格式化分区并创建文件系统
[root@centos7 ~]# mkfs.xfs /dev/vg_data/mysql meta-data=/dev/vg_data/mysql isize=512 agcount=4, agsize=3276544 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=13106176, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=6399, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
- 挂载分区并永久保存(更改/data那一行)
[root@centos7 ~]# lsblk -f (注意使用逻辑卷对应的uuid) name fstype label uuid mountpoint sda ├─sda1 xfs 0f7c8887-58c8-4c16-98d4-32cf5635006a /boot ├─sda2 xfs 71131d8c-e6d0-4104-b270-dcb8d5ae959a / ├─sda3 lvm2_member irchvr-fkfh-euts-0de9-rgsj-4pi3-mgkmjd │ └─vg_data-mysql xfs 7d3d4b4d-66c1-43d7-8d33-9de94d6b812d /mnt ├─sda4 └─sda5 swap 045c4250-e51f-4af0-a2f5-6c248700e1fb [swap] sr0 iso9660 centos 7 x86_64 2018-11-26-14-22-58-00 /misc/cd [root@centos7 ~]# vim /etc/fstab uuid=7d3d4b4d-66c1-43d7-8d33-9de94d6b812d /data xfs defaults 0 0 [root@centos7 ~]# mount -a (重新读取配置文件进行挂载) [root@centos7 ~]# lsblk name maj:min rm size ro type mountpoint sda 8:0 0 200g 0 disk ├─sda1 8:1 0 1g 0 part /boot ├─sda2 8:2 0 100g 0 part / ├─sda3 8:3 0 50g 0 part │ └─vg_data-mysql 253:0 0 50g 0 lvm /data ├─sda4 8:4 0 1k 0 part └─sda5 8:5 0 4g 0 part [swap] sr0 11:0 1 10g 0 rom /misc/cd
- 准备二进制程序包(我是从服务器传下来的没有的可以去官网下载,放在哪个目录都可以。)
[root@centos7 ~]# rz -e rz waiting to receive. [root@centos7 ~]# ls anaconda-ks.cfg downloads music templates desktop initial-setup-ks.cfg pictures videos documents mariadb-10.2.29-linux-systemd-x86_64.tar.gz public
实验阶段:
- 确认系统上没有数据库文件
[root@centos7 ~]# rpm -qa mariadb*
- 创建文件夹和创建用户并让他有读、执行权限。
[root@centos7 ~]# mkdir /data/mysql [root@centos7 ~]# useradd -r -s /sbin/nologin -d /data/mysql mysql [root@centos7 ~]# ll /data total 0 drwxr-xr-x 2 root root 6 nov 19 16:12 mysql
- 程序包解压到/usr/local/目录下
[root@centos7 ~]# tar xvf mariadb-10.2.29-linux-systemd-x86_64.tar.gz -c /usr/local/
- 更改用户和所属组(先把mariadb目录做成软连接)
[root@centos7 ~]# ll /usr/local/ total 0 drwxr-xr-x. 2 root root 6 apr 11 2018 bin drwxr-xr-x. 2 root root 6 apr 11 2018 etc drwxr-xr-x. 2 root root 6 apr 11 2018 games drwxr-xr-x. 2 root root 6 apr 11 2018 include drwxr-xr-x. 2 root root 6 apr 11 2018 lib drwxr-xr-x. 2 root root 6 apr 11 2018 lib64 drwxr-xr-x. 2 root root 6 apr 11 2018 libexec drwxrwxr-x 13 yang yang 294 nov 8 01:21 mariadb-10.2.29-linux-systemd-x86_64 drwxr-xr-x. 2 root root 6 apr 11 2018 sbin drwxr-xr-x. 5 root root 49 sep 5 16:17 share drwxr-xr-x. 2 root root 6 apr 11 2018 src [root@centos7 local]# ln -s mariadb-10.2.29-linux-systemd-x86_64/ mysql [root@centos7 local]# chown -r root.root mysql/ [root@centos7 local]# ll total 0 drwxr-xr-x. 2 root root 6 apr 11 2018 bin drwxr-xr-x. 2 root root 6 apr 11 2018 etc drwxr-xr-x. 2 root root 6 apr 11 2018 games drwxr-xr-x. 2 root root 6 apr 11 2018 include drwxr-xr-x. 2 root root 6 apr 11 2018 lib drwxr-xr-x. 2 root root 6 apr 11 2018 lib64 drwxr-xr-x. 2 root root 6 apr 11 2018 libexec drwxrwxr-x 13 root root 294 nov 8 01:21 mariadb-10.2.29-linux-systemd-x86_64 lrwxrwxrwx 1 root root 37 nov 19 16:22 mysql -> mariadb-10.2.29-linux-systemd-x86_64/ drwxr-xr-x. 2 root root 6 apr 11 2018 sbin drwxr-xr-x. 5 root root 49 sep 5 16:17 share drwxr-xr-x. 2 root root 6 apr 11 2018 src
- 查看二进制文件并把二进制文件目录加到path变量里以后使用方便不用写绝对路径
[root@centos7 local]# ls mysql/bin/ aria_chk mysqladmin mysqlshow aria_dump_log mysqlbinlog mysqlslap aria_ftdump mysqlcheck mysqltest aria_pack mysql_client_test mysqltest_embedded aria_read_log mysql_client_test_embedded mysql_tzinfo_to_sql galera_new_cluster mysql_config mysql_upgrade galera_recovery mysql_convert_table_format mysql_waitpid garbd mysqld mytop innochecksum mysqld_multi perror mariabackup mysqld_safe replace mariadb_config mysqld_safe_helper resolveip mariadb-service-convert mysqldump resolve_stack_dump mbstream mysqldumpslow sst_dump msql2mysql mysql_embedded tokuftdump myisamchk mysql_find_rows tokuft_logprint myisam_ftdump mysql_fix_extensions wsrep_sst_common myisamlog mysqlhotcopy wsrep_sst_mariabackup myisampack mysqlimport wsrep_sst_mysqldump my_print_defaults mysql_ldb wsrep_sst_rsync myrocks_hotbackup mysql_plugin wsrep_sst_rsync_wan mysql mysql_secure_installation wsrep_sst_xtrabackup mysqlaccess mysql_setpermission wsrep_sst_xtrabackup-v2 [root@centos7 local]# echo 'path=/usr/local/mysql/bin:$path' > /etc/profile.d/mysql.sh [root@centos7 local]# . /etc/profile.d/mysql.sh [root@centos7 support-files]# echo $path (使用这个命令检查一下) /usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
- 准备配置文件(使用软件包里自带的配置文件稍加修改使用就可以了)
[root@centos7 local]# cd mysql/ [root@centos7 mysql]# ls bin data include man readme-wsrep sql-bench copying docs install-binary mysql-test scripts support-files credits exceptions-client lib readme.md share thirdparty [root@centos7 mysql]# ls support-files/ binary-configure my-innodb-heavy-4g.cnf my-small.cnf mysql.server wsrep.cnf magic my-large.cnf mysqld_multi.server policy wsrep_notify my-huge.cnf my-medium.cnf mysql-log-rotate systemd [root@centos7 mysql]# less support-files/my-huge.cnf (查看一下文件选择一个合适的内存大小,这个是最大的 1g-2g) [root@centos7 mysql]# [root@centos7 mysql]# cp -b support-files/my-huge.cnf /etc/my.cnf cp: overwrite ‘/etc/my.cnf’? y (把这个文件拷贝到配置文件目录下并备份原有文件) 修改配置文件 [root@centos7 mysql]# vim /etc/my.cnf # the mysql server [mysqld] datadir=/data/mysql (加入这一行指定路径就可以了)
- 使用软件包自带脚本生成数据库
[root@centos7 mysql]# cd scripts/ [root@centos7 scripts]# ls mysql_install_db [root@centos7 scripts]# pwd /usr/local/mysql/scripts 这里要注意一下要是你要写相对路径必须在scripts目录下执行,要不就写成绝对路径。 [root@centos7 scripts]# /usr/local/mysql/scripts/mysql_install_db --datadir=/data/mysql --user=mysql installing mariadb/mysql system tables in '/data/mysql' ... 2019-11-19 16:41:29 140185403569984 [warning] 'thread_concurrency' is deprecated and will be removed in a future release. 2019-11-19 16:41:29 140185402984192 [warning] failed to load slave replication state from table mysql.gtid_slave_pos: 1146: table 'mysql.gtid_slave_pos' doesn't exist ok to start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system please remember to set a password for the mariadb root user ! to do so, start the server, then issue the following commands: '/usr/local/mysql/bin/mysqladmin' -u root password 'new-password' '/usr/local/mysql/bin/mysqladmin' -u root -h centos7.localdomain password 'new-password' alternatively you can run: '/usr/local/mysql/bin/mysql_secure_installation' which will also give you the option of removing the test databases and anonymous user created by default. this is strongly recommended for production servers. see the mariadb knowledgebase at http://mariadb.com/kb or the mysql manual for more instructions. you can start the mariadb daemon with: cd '/usr/local/mysql' ; /usr/local/mysql/bin/mysqld_safe --datadir='/data/mysql' you can test the mariadb daemon with mysql-test-run.pl cd '/usr/local/mysql/mysql-test' ; perl mysql-test-run.pl please report any problems at http://mariadb.org/jira the latest information about mariadb is available at http://mariadb.org/. you can find additional information about the mysql part at: http://dev.mysql.com consider joining mariadb's strong and vibrant community: https://mariadb.org/get-involved/ [root@centos7 scripts]# ll /data/mysql/ (查看数据库生成文件) total 110660 -rw-rw---- 1 mysql mysql 16384 nov 19 16:41 aria_log.00000001 -rw-rw---- 1 mysql mysql 52 nov 19 16:41 aria_log_control -rw-rw---- 1 mysql mysql 938 nov 19 16:41 ib_buffer_pool -rw-rw---- 1 mysql mysql 12582912 nov 19 16:41 ibdata1 -rw-rw---- 1 mysql mysql 50331648 nov 19 16:41 ib_logfile0 -rw-rw---- 1 mysql mysql 50331648 nov 19 16:41 ib_logfile1 drwx------ 2 mysql mysql 4096 nov 19 16:41 mysql -rw-rw---- 1 mysql mysql 29310 nov 19 16:41 mysql-bin.000001 -rw-rw---- 1 mysql mysql 19 nov 19 16:41 mysql-bin.index -rw-rw---- 1 mysql mysql 7 nov 19 16:41 mysql-bin.state drwx------ 2 mysql mysql 20 nov 19 16:41 performance_schema drwx------ 2 mysql mysql 20 nov 19 16:41 test
- 设置启动相关文件
[root@centos7 support-files]# cp /usr/local/mysql/support-files/systemd/mariadb.service /usr/lib/systemd/system/ [root@centos7 support-files]# ll /usr/lib/systemd/system/ |grep mariadb -rw-r--r-- 1 root root 5231 nov 19 16:45 mariadb.service centos6 的话可以使用mysql.server [root@centos7 support-files]# ls binary-configure my-innodb-heavy-4g.cnf my-small.cnf mysql.server wsrep.cnf magic my-large.cnf mysqld_multi.server policy wsrep_notify my-huge.cnf my-medium.cnf mysql-log-rotate systemd [root@centos7 support-files]# cp mysql.server /etc/init.d/mysqld [root@centos7 support-files]# chkconfig --list mysqld 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]'. error reading information on service mysqld: no such file or direct [root@centos7 support-files]# chkconfig --add mysqld (把mysql注册为服务之后可以使用server来管理) [root@centos7 support-files]# chkconfig --list mysqld (在使用chkconfig查看你服务)
- 再查看一下文件确保设置的路径正确,就可以启动服务了。
[root@centos7 support-files]# vim /usr/lib/systemd/system/mariadb.service [root@centos7 support-files]# mysql welcome to the mariadb monitor. commands end with ; or \g. your mariadb connection id is 9 server version: 10.2.29-mariadb-log mariadb server copyright (c) 2000, 2018, oracle, mariadb corporation ab and others. type 'help;' or '\h' for help. type '\c' to clear the current input statement. mariadb [(none)]> mariadb [(none)]> show databases; +--------------------+ | database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) mariadb [(none)]> (已经可以正常使用了但是密码没有设置谁都可以登录重新设置一下密码)
- 跑一遍安全加固脚本(这也是软件包自带的)
(安全加固脚本路径) [root@centos7 support-files]# which mysql_secure_installation /usr/local/mysql/bin/mysql_secure_installation [root@centos7 support-files]# 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 current password for the root user. if you've just installed mariadb, and you 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): (输入当前密码,回车不输入任何字符) ok, successfully used password, moving on... setting the root password ensures that nobody can log into the mariadb root user without the proper authorisation. set root password? [y/n] y (是否要设置密码输y) new password: re-enter new password: (输入两遍密码) password updated successfully! reloading privilege tables.. ... success! by default, a mariadb installation has an anonymous user, allowing anyone to log into mariadb without having to have a user account created for them. this is intended only for testing, and to make the installation go a bit smoother. you should remove them before moving into a production environment. remove anonymous users? [y/n] y (是否删除默认用户,输入y) ... success! normally, root should only be allowed to connect from 'localhost'. this ensures that someone cannot guess at the root password from the network. disallow root login remotely? [y/n] y (不允许root远程登录,输入y安全考虑) ... success! by default, mariadb comes with a database named 'test' that anyone can access. this is also intended only for testing, and should be removed before moving into a production environment. remove test database and access to it? [y/n] y (是否删除测试数据库test,生产中用不到删掉y) - dropping test database... ... success! - removing privileges on test database... ... success! reloading the privilege tables will ensure that all changes made so far will 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 mariadb installation should now be secure. thanks for using mariadb! (直接使用mysql不可以登陆了) [root@centos7 support-files]# mysql error 1045 (28000): access denied for user 'root'@'localhost' (using password: no) [root@centos7 support-files]# mysql -p123456 (-p输入密码登录。注意:-p后不要加空格) welcome to the mariadb monitor. commands end with ; or \g. your mariadb connection id is 19 server version: 10.2.29-mariadb-log mariadb server copyright (c) 2000, 2018, oracle, mariadb corporation ab and others. type 'help;' or '\h' for help. type '\c' to clear the current input statement. mariadb [(none)]> exit bye [root@centos7 support-files]# mysql -uroot -p123456 (上面登录不写用户的话默认就为root登录既管理员)注意:这里的root不是操作系统的root他只是针对于mysql数据库的root用户不要搞混了。 welcome to the mariadb monitor. commands end with ; or \g. your mariadb connection id is 20 server version: 10.2.29-mariadb-log mariadb server copyright (c) 2000, 2018, oracle, mariadb corporation ab and others. type 'help;' or '\h' for help. type '\c' to clear the current input statement. mariadb [(none)]> exit bye