[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 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