MySQL服务器的运维和优化
来自CloudWiki
安装
yum install mariadb mariadb-server
systemctl start mariadb
systemctl status mariadb
配置
[root@controller ~]# 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 New password: Re-enter new password: Sorry, passwords do not match. New password: Re-enter new password: Sorry, passwords do not match. 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 ... 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] n ... skipping. 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 - 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 ... 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!
创建数据库
mysqladmin -uroot -p000000 create test
mysql -uroot -p000000
MariaDB [(none)]> use test; Database changed MariaDB [test]> CREATE TABLE IF NOT EXISTS `tables`( -> `tables_id` INT UNSIGNED AUTO_INCREMENT, -> `tables_title` VARCHAR(100) NOT NULL, -> `tables_author` VARCHAR(40) NOT NULL, -> `tables_date` DATE, -> PRIMARY KEY ( `tables_id` ) -> )ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.00 sec) MariaDB [test]> show tables; +----------------+ | Tables_in_test | +----------------+ | tables | +----------------+ 1 row in set (0.00 sec) MariaDB [test]> exit Bye
数据库备份
导出整个数据库,命令如下所示。
[root@mysql ~]# mysqldump -uroot -p000000 test > test.sql
[root@mysql ~]# ls
test.sql
导出一个表,命令如下所示:
[root@mysql ~]# mysqldump -uroot -p000000 test tables > test_tables.sql
[root@mysql ~]# ls
test.sql test_tables.sql
数据库导入
删除test数据库进行导入测试,用mysqldump备份的文件是一个可以直接导入的SQL脚本。有两种方法可以将数据导入,一种用msql命令,把数据库文件恢复到指定的数据库,命令如下所示:
[root@mysql ~]# mysqladmin -uroot -p000000 drop test Dropping the database is potentially a very bad thing to do. Any data stored in the database will be destroyed. Do you really want to drop the 'test' database [y/N] y Database "test" dropped
[root@mysql ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 26 Server version: 10.3.18-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)]> create database test; Query OK, 1 row affected (0.000 sec) MariaDB [(none)]> quit Bye [root@mysql ~]# mysql -uroot -p000000 test < test.sql
第二种,可以使用source语句方法导入数据库,把数据库文件恢复到指定的数据库,命令如下所示:
[root@mysql ~]# mysqladmin -uroot -p000000 drop test Dropping the database is potentially a very bad thing to do. Any data stored in the database will be destroyed. Do you really want to drop the 'test' database [y/N] y Database "test" dropped [root@mysql ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 30 Server version: 10.3.18-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)]> create database test; Query OK, 1 row affected (0.027 sec) MariaDB [(none)]> use test Database changed MariaDB [test]> source /root/test.sql;
添加用户并授权
授权root用户可以在任何节点访问test数据库下所有表,“%”代表所有节点机器,命令如下所示:
MariaDB [(none)]> GRANT ALL PRIVILEGES ON test.* TO 'root'@'%' IDENTIFIED BY '000000' ; Query OK, 0 rows affected (0.001 sec) MariaDB [(none)]> GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost' IDENTIFIED BY '000000' ; Query OK, 0 rows affected (0.001 sec) MariaDB [test]> flush privileges; Query OK, 0 rows affected (0.00 sec)
添加root用户对test数据库授增、删、改、查的权限,命令如下所示:
MariaDB [(none)]> GRANT SELECT,INSERT,DELETE,UPDATE ON test.* TO 'root'@'%' IDENTIFIED BY '000000' ; Query OK, 0 rows affected (0.001 sec) MariaDB [test]> flush privileges; Query OK, 0 rows affected (0.00 sec)
MySQL数据库优化
修改数据库配置文件,添加参数,命令如下所示:
vi /etc/my.cnf
在文件中添加以下命令,优化数据库:
[mysqld] thread_concurrency = 64 #CPU核数 * 2 max_connections=1500 #最大连接(用户)数。每个连接MySQL的用户均算作一个连接 max_connect_errors=30 #最大失败连接限制 bulk_insert_buffer_size = 32M #批量插入数据缓存大小 query_cache_type=1 #查询缓存 (0 = off、1 = on、2 = demand) query_cache_size = 64M #指定mysql查询缓冲区大小 max_allowed_packet = 128M #通信缓冲大小 read_buffer_size = 8M #顺序读取数据缓冲区使用内存 read_rnd_buffer_size = 32M #随机读取数据缓冲区使用内存
复制粘贴版:
thread_concurrency = 64 max_connections=1500 max_connect_errors=30 bulk_insert_buffer_size = 32M query_cache_type=1 query_cache_size = 64M max_allowed_packet = 128M read_buffer_size = 8M read_rnd_buffer_size = 32M