目標 理解線上部署考慮的因素 學會編譯安裝以及二進位安裝mysql 學會升級mysql 學會多實例部署mysql資料庫 學會合理部署mysql線上庫 考慮因素: 版本選擇,5.1,5.5還是5.6? 分支選擇,官方社區版?percona server?mariadb? 推薦官方版,簡單易上手 安裝方 ...
目標
- 理解線上部署考慮的因素
- 學會編譯安裝以及二進位安裝mysql
- 學會升級mysql
- 學會多實例部署mysql資料庫
- 學會合理部署mysql線上庫
- 版本選擇,5.1,5.5還是5.6?
- 分支選擇,官方社區版?percona server?mariadb?
-
- 推薦官方版,簡單易上手
- 安裝方式,包安裝?二進位包安裝?源碼安裝?
-
- 線上推薦二進位包安裝
- 路徑配置,參數配置(儘量模板化,標準化)
- 一個實例多個庫,or多個實例單個庫?
- 下載軟體安裝包
- 解壓放到指定目錄(比如/usr/local)並將mysql目錄放到PATH中
- 初始化實例,編輯配置文件 並啟動
- 賬號安全設置
#tar -zxf mysql-5.5.50-linux2.6-i686.tar.gz #mv mysql-5.5.50-linux2.6-i686 /usr/local/ #cd /usr/local #mv mysql-5.5.50-linux2.6-i686 mysql55 #cd mysql55/bin #export PATH=/usr/local/mysql55/bin:$PATH
3)初始化實例 使用腳本mysql_install_db
#mkdir -p /tmp/mysqldata/node1 #/usr/local/mysql55/script/mysql_install_db --user=mysql --basedir=/usr/local/mysql55 --datadir=/tmp/mysqldata/node1 #cd /tmp/mysqldata/node1
出現問題1 沒有libaio.so.1庫
/tmp/mysql55/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
解決:查詢libaio庫並按照libaio.so.1 查詢
aiapple@aiapple:/tmp$ sudo apt-cache search libaio libaio-dev - Linux kernel AIO access library - development files libaio1 - Linux kernel AIO access library - shared library libaio1-dbg - Linux kernel AIO access library - debugging symbols安裝
aiapple@aiapple:/tmp$ sudo apt-get install libaio1
初始化成功:
aiapple@aiapple:/usr/local/mysql/data$ ls -l /tmp/mysqldata/node1/ total 12 drwx------ 2 aiapple aiapple 4096 7月 27 21:27 mysql drwx------ 2 aiapple aiapple 4096 7月 27 21:27 performance_schema drwx------ 2 aiapple aiapple 4096 7月 27 21:25 test
拷貝配置文件:support-files中拷貝my-medium.cnf
cp /usr/local/mysql56/support-files/my-medium.cnf /tmp/mysqldata
3)使用mysqld_safe腳本,啟動
/usr/local/mysql55/bin/mysqld_safe --defaults-file=/tmp/mysqldata/my.cnf &
問題2:沒有/usr/local/mysql/data 目錄
aiapple@aiapple:/usr/local/mysql55/support-files$ /usr/local/mysql55/bin/mysqld_safe --defaults-file=/tmp/mysqldata/my.cnf & [1] 3349 aiapple@aiapple:/usr/local/mysql55/support-files$ 160727 21:33:26 mysqld_safe Logging to '/usr/local/mysql/data/aiapple.err'. touch: cannot touch ‘/usr/local/mysql/data/aiapple.err’: No such file or directory chmod: cannot access ‘/usr/local/mysql/data/aiapple.err’: No such file or directory 160727 21:33:26 mysqld_safe The file /usr/local/mysql/bin/mysqld does not exist or is not executable. Please cd to the mysql installation directory and restart this script from there as follows: ./bin/mysqld_safe& See http://dev.mysql.com/doc/mysql/en/mysqld-safe.html for more information /usr/local/mysql55/bin/mysqld_safe: 128: /usr/local/mysql55/bin/mysqld_safe: cannot create /usr/local/mysql/data/aiapple.err: Directory nonexistent ^C [1]+ Exit 1 /usr/local/mysql55/bin/mysqld_safe --defaults-file=/tmp/mysqldata/my.cnfView Code
解決:創建目錄並賦權
aiapple@aiapple:/usr/local$ sudo mkdir -p mysql/data aiapple@aiapple:/usr/local/mysql$ sudo chown -R aiapple . aiapple@aiapple:/usr/local/mysql$ sudo chgrp -R aiapple .
問題3:不存/usr/local/mysql/bin/mysqld 文件
aiapple@aiapple:/usr/local/mysql$ /usr/local/mysql55/bin/mysqld_safe --defaults-file=/tmp/mysqldata/my.cnf & [1] 3773 aiapple@aiapple:/usr/local/mysql$ 160727 21:37:32 mysqld_safe Logging to '/usr/local/mysql/data/aiapple.err'. 160727 21:37:32 mysqld_safe The file /usr/local/mysql/bin/mysqld does not exist or is not executable. Please cd to the mysql installation directory and restart this script from there as follows: ./bin/mysqld_safe& See http://dev.mysql.com/doc/mysql/en/mysqld-safe.html for more information ^C [1]+ Exit 1 /usr/local/mysql55/bin/mysqld_safe --defaults-file=/tmp/mysqldata/my.cnfView Code
懷疑 Ubuntu定死了basedir 在/usr/local/mysql 下 解決:拷貝mysql55中內容到該目錄中
aiapple@aiapple:/usr/local/mysql$ cp -ar ../mysql55/* .
問題4:mysqld從 /usr/local/mysql/data中啟動庫文件;
aiapple@aiapple:/usr/local/mysql$ /usr/local/mysql55/bin/mysqld_safe --defaults-file=/tmp/mysqldata/my.cnf & [1] 3983 aiapple@aiapple:/usr/local/mysql$ 160727 21:39:05 mysqld_safe Logging to '/usr/local/mysql/data/aiapple.err'. 160727 21:39:05 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data 160727 21:39:07 mysqld_safe mysqld from pid file /usr/local/mysql/data/aiapple.pid ended [1]+ Done /usr/local/mysql55/bin/mysqld_safe --defaults-file=/tmp/mysqldata/my.cnf 且mysqld沒有啟動 aiapple@aiapple:~$ ps -ef | grep mysqld aiapple 2129 2097 0 19:35 pts/2 00:00:00 grep --color=auto mysqldView Code
查看aiapple.err:需要的一些表不在/usr/local/mysql/data
160727 21:41:59 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data 160727 21:41:59 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.5.50-log) starting as process 4500 ... 160727 21:41:59 [Note] Plugin 'FEDERATED' is disabled. /usr/local/mysql/bin/mysqld: Table 'mysql.plugin' doesn't exist 160727 21:41:59 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it. 160727 21:41:59 InnoDB: The InnoDB memory heap is disabled 160727 21:41:59 InnoDB: Mutexes and rw_locks use GCC atomic builtins 160727 21:41:59 InnoDB: Compressed tables use zlib 1.2.3 160727 21:41:59 InnoDB: Using Linux native AIO 160727 21:41:59 InnoDB: Initializing buffer pool, size = 128.0M 160727 21:41:59 InnoDB: Completed initialization of buffer pool 160727 21:41:59 InnoDB: highest supported file format is Barracuda. InnoDB: Log scan progressed past the checkpoint lsn 48941 160727 21:41:59 InnoDB: Database was not shut down normally! InnoDB: Starting crash recovery. InnoDB: Reading tablespace information from the .ibd files... InnoDB: Restoring possible half-written data pages from the doublewrite InnoDB: buffer... InnoDB: Doing recovery: scanned up to log sequence number 1595675 160727 21:41:59 InnoDB: Starting an apply batch of log records to the database... InnoDB: Progress in percents: 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 InnoDB: Apply batch completed 160727 21:41:59 InnoDB: Waiting for the background threads to start 160727 21:42:00 InnoDB: 5.5.50 started; log sequence number 1595675 160727 21:42:00 [Note] Recovering after a crash using mysql-bin 160727 21:42:00 [Note] Starting crash recovery... 160727 21:42:00 [Note] Crash recovery finished. 160727 21:42:00 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306 160727 21:42:00 [Note] - '0.0.0.0' resolves to '0.0.0.0'; 160727 21:42:00 [Note] Server socket created on IP: '0.0.0.0'. 160727 21:42:00 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist 160727 21:42:00 mysqld_safe mysqld from pid file /usr/local/mysql/data/aiapple.pid endedView Code 解決:將原初始化的datadir目錄中所有拷貝過去
aiapple@aiapple:/usr/local/mysql/data$ rm -rf test/ aiapple@aiapple:/usr/local/mysql/data$ cp -ar /tmp/mysqldata/node1/* .成功安裝:
aiapple@aiapple:/usr/local/mysql/data$ ps -ef | grep mysqld aiapple 4536 2146 0 21:43 pts/0 00:00:00 /bin/sh /usr/local/mysql55/bin/mysqld_safe --defaults-file=/tmp/mysqldata/my.cnf aiapple 4773 4536 1 21:43 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/tmp/mysqldata/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/aiapple.err --pid-file=/usr/local/mysql/data/aiapple.pid --socket=/tmp/mysql.sock --port=3306 aiapple 4792 2097 0 21:44 pts/2 00:00:00 grep --color=auto mysqldView Code
疑問: 不知是Ubuntu指定了basedir=/usr/local/mysql,datadir=/usr/local/mysql/data;還是最開始的初始化沒有管用,還是有什麼配置沒有寫在配置文件中。 當然是沒有寫在配置文件中了,可以查看一下my-medium.cnf內容
[client] #password = your_password port = 3306 socket = /tmp/mysql.sock # Here follows entries for some specific programs # The MySQL server [mysqld] port = 3306 socket = /tmp/mysql.sock skip-external-locking key_buffer_size = 16M max_allowed_packet = 1M table_open_cache = 64 sort_buffer_size = 512K net_buffer_length = 8K read_buffer_size = 256K read_rnd_buffer_size = 512K myisam_sort_buffer_size = 8MView Code 沒有相關目錄的配置; 增加啟動配置參考:mysqld_safe — MySQL Server Startup Script 在my.cnf 增加一下配置
my.cnf port = 3306 socket = /tmp/mysqldata/node1/mysql.sock [mysqld_safe] user=aiapple # Here follows entries for some specific programs # The MySQL server [mysqld] port = 3306 socket = /tmp/mysqldata/node1/mysql.sock pid-file = /tmp/mysqldata/node1/mysqld.pid basedir = /usr/local/mysql55 datadir = /tmp/mysqldata/node1 innodb_data_home_dir = /tmp/mysqldata/node1 innodb_log_group_home_dir =/tmp/mysqldata/node1 tmpdir = /tmp/mysqldata/node1 log-error = /tmp/mysqldata/node1/aiapple.log log-bin = /tmp/mysqldata/node1/mysql-bin.logView Code
重新啟動 問題一:配置文件許可權太大
aiapple@aiapple:/usr/local/mysql$ /usr/local/mysql55/bin/mysqld_safe --defaults-file=/tmp/mysqldata/my.cnf & Warning: World-writable config file '/tmp/mysqldata/my.cnf' is ignored解決:降權
aiapple@aiapple:sudo chmod 600 /tmp/mysqldata/my.cnf
問題二:bin目錄未指定
aiapple@aiapple:/usr/local/mysql$ /usr/local/mysql55/bin/mysqld_safe --defaults-file=/tmp/mysqldata/my.cnf & 160806 16:07:29 mysqld_safe The file /usr/local/mysql/bin/mysqld does not exist or is not executable. Please cd to the mysql installation directory and restart this script from there as follows: ./bin/mysqld_safe&
解決:在配置文件中通過ledir指定bin目錄
#my.cnf增加
ledir = /usr/local/mysql55
問題三:aiapple.err報錯,插件被禁止
160806 16:48:46 [Note] Plugin 'FEDERATED' is disabled. 160806 16:48:46 InnoDB: The InnoDB memory heap is disabled 160806 16:48:46 InnoDB: Mutexes and rw_locks use GCC atomic builtins 160806 16:48:46 InnoDB: Compressed tables use zlib 1.2.3 160806 16:48:46 InnoDB: Using Linux native AIO 160806 16:48:46 InnoDB: Initializing buffer pool, size = 128.0MView Code 解決:在配置文件中啟用,nice=0
#my.cnf增加 nice = 0
4)賬號安全設置 #刪除空用戶
mysql -uroot --socket=/tmp/mysql.sock select user,host,password from mysql.user; delete from mysql.user where user=''; delete from mysql.user where host<>'localhost'; mysql> select user,host,password from mysql.user; +------+-----------+----------+ | user | host | password | +------+-----------+----------+ | root | localhost | | +------+-----------+----------+ 1 row in set (0.00 sec)
#設置root密碼
mysql> set password for root@'localhost'=password('000000'); Query OK, 0 rows affected (0.00 sec)#使設置生效
flush privileges;#刪除test庫 看看只讀許可權的test用戶能用test庫做什麼
創建test用戶 #mysql -uroot -p123456--socket=/tmp/mysql.sock grant select on *.* to test@'localhost' identified by '123'; 使用test用戶,在test庫中建表 #mysql -utest -p123 --socket=/tmp/mysql.sock use test create table t1(id int); mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | t1 | +----------------+ 1 row in set (0.01 sec)
刪除test庫
mysql> drop database test; Query OK, 1 row affected (0.01 sec)
mysql升級
從5.5升級到5.6
- 下載mysql5.6安裝包並配置mysql5.6安裝包安裝路徑
- 關閉mysql5.5的實例,修改部分參數,使用mysql5.6軟體啟動
- 執行mysql5.6路徑下mysql_upgrade腳本
- 驗證是否成功升級
aiapple@aiapple:/tmp/mysqldata$ ps -ef | grep mysqld aiapple 4931 2146 0 22:32 pts/0 00:00:00 /bin/sh /usr/local/mysql55/bin/mysqld_safe --defaults-file=/tmp/mysqldata/my.cnf aiapple 5168 4931 0 22:32 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/tmp/mysqldata/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/aiapple.err --pid-file=/usr/local/mysql/data/aiapple.pid --socket=/tmp/mysql.sock --port=3306 aiapple 5187 4889 0 22:33 pts/13 00:00:00 grep --color=auto mysqld
使用mysqladmin工具關閉mysql服務
aiapple@aiapple:/usr/local/mysql$ mysqladmin -uroot --socket=/tmp/mysql.sock shutdown -p Enter password: 160727 22:28:46 mysqld_safe mysqld from pid file /usr/local/mysql/data/aiapple.pid ended [1]+ Done /usr/local/mysql55/bin/mysqld_safe --defaults-file=/tmp/mysqldata/my.cnf (wd: /usr/local/mysql/data) (wd now: /usr/local/mysql)
檢查是否關閉
aiapple@aiapple:/usr/local/mysql$ ps -ef | grep mysqld aiapple 4918 2146 0 22:28 pts/0 00:00:00 grep --color=auto mysqld
2)更改my.cnf
#vim my.cnf(把55目錄都改成56的)
3)使用mysql5.6啟動軟體; #使用5.6拉起實例
aiapple@aiapple:/tmp/mysqldata$ /usr/local/mysql56/bin/mysqld_safe --defaults-file=/tmp/mysqldata/my.cnf & [1] 9121 aiapple@aiapple:/tmp/mysqldata$ 160806 21:29:11 mysqld_safe Logging to '/tmp/mysqldata/node1/aiapple.log'. 160806 21:29:11 mysqld_safe Starting mysqld daemon with databases from /tmp/mysqldata/node1
#查看mysqld進程有沒有起來
aiapple@aiapple:/tmp/mysqldata$ ps -ef | grep mysql aiapple 9121 8547 0 21:29 pts/2 00:00:00 /bin/sh /usr/local/mysql56/bin/mysqld_safe --defaults-file=/tmp/mysqldata/my.cnf aiapple 9489 9121 0 21:29 pts/2 00:00:00 /usr/local/mysql56/bin/mysqld --defaults-file=/tmp/mysqldata/my.cnf --basedir=/usr/local/mysql56 --datadir=/tmp/mysqldata/node1 --plugin-dir=/usr/local/mysql56/lib/plugin --log-error=/tmp/mysqldata/node1/aiapple.log --pid-file=/tmp/mysqldata/node1/mysqld.pid --socket=/tmp/mysqldata/node1/mysql.sock --port=3306 aiapple 9564 8547 0 21:35 pts/2 00:00:00 grep --color=auto mysql
#登陸後查看mysql狀態
mysql> status -------------- mysql Ver 14.14 Distrib 5.6.32, for linux-glibc2.5 (x86_64) using EditLine wrapper Connection id: 3 Current database: Current user: root@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.6.32-log MySQL Community Server (GPL) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: latin1 Db characterset: latin1 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /tmp/mysqldata/node1/mysql.sock Uptime: 7 min 27 sec Threads: 1 Questions: 5 Slow queries: 0 Opens: 32 Flush tables: 1 Open tables: 25 Queries per second avg: 0.011 --------------View Code
4)升級庫mysql_upgrade
aiapple@aiapple:/tmp/mysqldata$ /usr/local/mysql56/bin/mysql_upgrade -uroot --socket=/tmp/mysqldata/node1/mysql.sock -p Enter password: Looking for 'mysql' as: /usr/local/mysql56/bin/mysql Looking for 'mysqlcheck' as: /usr/local/mysql56/bin/mysqlcheck Running 'mysqlcheck' with connection arguments: '--socket=/tmp/mysqldata/node1/mysql.sock' Warning: Using a password on the command line interface can be insecure. Running 'mysqlcheck' with connection arguments: '--socket=/tmp/mysqldata/node1/mysql.sock' Warning: Using a password on the command line interface can be insecure. mysql.columns_priv OK mysql.db OK mysql.event OK mysql.func OK mysql.general_log OK mysql.help_category OK mysql.help_keyword OK mysql.help_relation OK mysql.help_topic OK mysql.host OK mysql.ndb_binlog_index OK mysql.plugin OK mysql.proc OK mysql.procs_priv OK mysql.proxies_priv OK mysql.servers OK mysql.slow_log OK mysql.tables_priv OK mysql.time_zone OK mysql.time_zone_leap_second OK mysql.time_zone_name OK mysql.time_zone_transition OK mysql.time_zone_transition_type OK mysql.user OK Running 'mysql_fix_privilege_tables'... Warning: Using a password on the command line interface can be insecure. Running 'mysqlcheck' with connection arguments: '--socket=/tmp/mysqldata/node1/mysql.sock' Warning: Using a password on the command line interface can be insecure. Running 'mysqlcheck' with connection arguments: '--socket=/tmp/mysqldata/node1/mysql.sock' Warning: Using a password on the command line interface can be insecure. OKView Code
#驗證庫有沒有升級成功
aiapple@aiapple:/tmp/mysqldata$ /usr/local/mysql56/bin/mysql_upgrade -uroot --socket=/tmp/mysqldata/node1/mysql.sock -p Enter password: Looking for 'mysql' as: /usr/local/mysql56/bin/mysql Looking for 'mysqlcheck' as: /usr/local/mysql56/bin/mysqlcheck This installation of MySQL is already upgraded to 5.6.32, use --force if you still need to run mysql_upgrademysql多實例安裝
- 部署好mysql軟體
- 編輯多個配置文件,初始化多個實例
- 啟動mysql實例
- 充分利用系統資源
- 資源隔離
- 業務,模塊隔離
#mkdir -p /mysqldata/node3
a) 初始化實例 使用腳本mysql_install_db
aiapple@aiapple:/tmp/mysqldata/node1$ /usr/local/mysql56/scripts/mysql_install_db --user=aiapple --basedir=/usr/local/mysql56 --datadir=/tmp/mysqldata/node3 #查看 aiapple@aiapple:/tmp/mysqldata/node1$ ls ../node3 ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema test
b) 給node3配置文件
cp my2.cnf my3.cnf vim my3.cnf(修改相關參數) [client] #password = your_password port = 3308 socket = /tmp/mysqldata/node3/mysql.sock [mysqld_safe] user=aiapple ledir = /usr/local/mysql56/bin #ledir = /usr/local/mysql55/bin nice = 0 # Here follows entries for some specific programs # The MySQL server [mysqld] port = 3308 socket = /tmp/mysqldata/node3/mysql.sock pid-file = /tmp/mysqldata/node3/mysqld.pid basedir = /usr/local/mysql56 #basedir = /usr/local/mysql55 datadir = /tmp/mysqldata/node3 innodb_data_home_dir = /tmp/mysqldata/node3 innodb_log_group_home_dir = /tmp/mysqldata/node3 tmpdir = /tmp/mysqldata/node3 log-error = /tmp/mysqldata/node3/aiapple.log log-bin = /tmp/mysqldata/node3/mysql-bin.logView Code
c) 使用mysqld_safe,及my3.cnf拉起實例
aiapple@aiapple:/tmp/mysqldata/node3$ /usr/local/mysql56/bin/mysqld_safe --defaults-file=/tmp/mysqldata/node3/my3.cnf & [2] 9861 aiapple@aiapple:/tmp/mysqldata/node3$ 160806 22:03:17 mysqld_safe Logging to '/tmp/mysqldata/node3/aiapple.log'. 160806 22:03:17 mysqld_safe Starting mysqld daemon with databases from /tmp/mysqldata/node3
d)查看
aiapple@aiapple:/tmp/mysqldata/node3$ ps -ef | grep mysqld aiapple 9121 8547 0 21:29 pts/2 00:00:00 /bin/sh /usr/local/mysql56/bin/mysqld_safe --defaults-file=/tmp/mysqldata/my.cnf aiapple 9489 9121 0 21:29 pts/2 00:00:02 /usr/local/mysql56/bin/mysqld --defaults-file=/tmp/mysqldata/my.cnf --basedir=/usr/local/mysql56 --datadir=/tmp/mysqldata/node1 --plugin-dir=/usr/local/mysql56/lib/plugin --log-error=/tmp/mysqldata/node1/aiapple.log --pid-file=/tmp/mysqldata/node1/mysqld.pid --socket=/tmp/mysqldata/node1/mysql.sock --port=3306 aiapple 9861 8547